Forum Moderators: coopster

Message Too Old, No Replies

DYNAMIC Metas - (Title, description, content, etc)

How to make dynamic meta title, description, etc?

         

DarkEden Genesis

4:51 am on May 2, 2012 (gmt 0)

10+ Year Member



Hello,

My website uses dynamic system.

For Meta Title I use:
Index.php
<?php
include("conf.php");
?>
<html>
<head>
<title><?php echo $title;?></title>



conf.php
<?php
@session_start();
$url= "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if ($url="http://www.url.com/index.php") echo $title="Index Title";
if ($url="http://www.url.com/signup") echo $title="Signup Title";



It's actually a poor code, what could I do to improve it?
I will also need a rewrite rule to rewrite url.com/index.php to url.com.

g1smd

7:22 am on May 2, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need code to redirect, not rewrite, that request. Luckily, there's literally several thousand code examples for that in the Apache forum here.

For the metadata, I would use an array for title and another array for meta description, both using the requested URL as the array key.

rocknbil

4:07 pm on May 2, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using G's example,

$pages = array(
'home' => array('My Home Page Title','My Home Meta Title','My Home Meta Description'),
'about' => array('My About Page Title','My About Meta Title','My About Meta Description')
);

$metas = $pages['home'];

<title><php echo $metas[1]; ?></title>
<meta name="description" content="<?php echo $metas[2]; ?>">
</head>
<body>
<h1><php echo $metas[0]; ?></h1>

.... that's "one way to do it."

eelixduppy

5:48 pm on May 2, 2012 (gmt 0)



Extending the example above, it would make sense to name the items in the sub-arrays like so:


$pages = array(
'home' => array(
'header' => 'My Home Page Title',
'title' => 'My Home Meta Title',
'description' => 'My Home Meta Description'
),
'about' => array(
'header' => 'My About Page Title',
'title' => 'My About Meta Title',
'description' => 'My About Meta Description'
)
);


That way you can access the values in a human-readable form:


<title><?php echo $pages['home']['title']; ?></title>

g1smd

6:33 pm on May 2, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I use two separate arrays, one for title and one for description, $title[$page] and $meta[$page]. They live in a separate include file.

The array key is the requested extensionless URL (using the name 'home' or 'root' for the site root).

DarkEden Genesis

9:07 pm on May 2, 2012 (gmt 0)

10+ Year Member



g1smd, I wasn't successfully with that code of my first post.
That isn't working.

How should I do that?

I forgot to say, ALL my content is shown in the INDEX.PHP.
This is the file where all content is shown.

So, how would I do this?
<title> <?php $pages['home']['title']['about']['title']['signup']['title']; ?>

rocknbil

3:44 pm on May 3, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a look at the arrays [php.net] page from the manual, particularly example #7 and the ones that follow to get an idea of how to construct and dereference multidimensional arrays (arrays inside other arrays) to achieve the result you need. The example you posted infers an array inside an array inside an array inside an array ..... etc.

DarkEden Genesis

4:46 pm on May 3, 2012 (gmt 0)

10+ Year Member



Have a look at the arrays [php.net] page from the manual, particularly example #7 and the ones that follow to get an idea of how to construct and dereference multidimensional arrays (arrays inside other arrays) to achieve the result you need. The example you posted infers an array inside an array inside an array inside an array ..... etc.


rocknbil, I'm currently doing this:

CONF.PHP
@session_start();
$metas = array(
'index.php' => array(
'header' => 'Home Page Title',
'title' => 'Home Meta Title',
'description' => 'Home Meta Description'
),
'signup' => array(
'header' => 'Signup Title',
'title' => 'Signup Meta Title',
'description' => 'Signup Meta Description'
),
'about' => array(
'header' => 'About Title',
'title' => 'About Meta Title',
'description' => 'About Meta Description'
)
);
$title = $metas['index.php'];
$title = $metas['signup'];
$title = $metas['about'];



INDEX.PHP
<TITLE><?php echo $title['title']; ?></TITLE>

modserv

6:32 pm on May 4, 2012 (gmt 0)

10+ Year Member



You need to change index.php in the array and make it "index", Then in INDEX.PHP do this:

<title>$metas['index']['title']</title>

DarkEden Genesis

7:18 pm on May 4, 2012 (gmt 0)

10+ Year Member



modserv, all my pages are opened inside the index.php

Signup
About
Articles
Updates
Etc...


All pages open inside index.php.

So how can I make it clear to my system which meta it should take for every page?
I was thinking to use something like

$url = $_GET['REMOTE_ADDR'];
if ($url= "example.com/signup) echo $title"Signup Title"

g1smd

8:14 pm on May 4, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Link from the pages of your site to
href="/about"
with a leading slash.

Set up a RewriteRule that rewrites that request.

RewriteRule ^([^/.]+)$ /index.php?page=$1 [L]


In index.php look at
$_SERVER['page']
to find what page was requested.

Use the value of
$page
to pull the right record from the array.