Forum Moderators: coopster

Message Too Old, No Replies

Stealing a variable from a later page

         

Will Hamilton

1:32 am on Apr 5, 2006 (gmt 0)

10+ Year Member



Hullo all; previous lurker, first time posting.

I'm trying to call a php variable that is defined later on in another page. The setup I have is the main website pages automatically consisting of header.php, then header2.php, then the php page itself.

What I want to do is pull a nice page name from the actual page, and put it in the <TITLE> tags in the header.php file. I already have $page="tum te tum" in most of my php files for another reason anyway, so I thought it'd be easiest to simply call these in the header.php file.

Is this even possible? I'm beginning to think it's not. Is perhaps a way of quietly calling a page in the background to take its variables (which could be done before the header)?

Apologies if this has been answered elsewhere or is exceedingly obvious. Thanks.

jatar_k

1:49 am on Apr 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Will Hamilton,

it's not possible as is but with a small logic change it could be.

What you could do is to set

$page="tum te tum";

before you include your header. Then in the header you could have a default in case it isn't set. Something like

if (!isset($page) ¦¦ empty($page)) $page="my default pagename";

then have this in your header

echo '<title>',$page,'</title>';

or something similar

NOTE: the WebmasterWorld forum breaks ¦ characters, replace all ¦ in posts with a real pipe character

Will Hamilton

2:00 am on Apr 5, 2006 (gmt 0)

10+ Year Member



Thanks for the quick reply and welcome.

As it stands I really can't set the variable before the header - the variable is in the page, which comes after the header. The header goes in front of every page automatically - it's not that I'm doing an include in my pages. I could do (like I think you're suggesting):

$page="tum te tum";
include "header.php":

..but then I'd have to add that to every page. I'll think about it perhaps; I do already have to specify the page name at the top of my files, it wouldn't be much extra to add. That's got me thinking now.. I'll ponder it.

I could always do it through mySQL; just make a table with two columns: the page URL (e.g.,?p=carrot) and one with the name (Carrots Frolick). Then, in the header, do a $_GET['p'] and tell it to grab the 'name' and put that in the <TITLE>. Should work.

jatar_k

2:09 am on Apr 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the sql route would work too, just keep in mind it adds an extra db call to every page. It might not matter but is always something to watch.

Will Hamilton

4:28 pm on Apr 9, 2006 (gmt 0)

10+ Year Member



I went down the database route in the end - but went for a flat-file php database so it's less intensive. Here's the code in the hope it helps someone else:

(the ¦ are all pipe characters)

<TITLE>Website Title

<?php
$fp = fopen('headers/titles.txt','r');
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}

while (!feof($fp)) {
$line = fgets($fp, 1024); //use 2048 if very long lines
list ($field1, $field2) = split ('\¦', $line);

if ($_GET['p']==$field1)
{
echo "¦ $field2";
$fp++;
}
}
fclose($fp);
?>
</TITLE>

This looks for the file 'titles.txt' in the header folder, which should look something like:

home_test¦Home Test
oxford¦Oxford
trilogy¦The Trilogy
adapt¦Other Adaptations

All the script does is look at the first field before the pipe character, then check to see if it matches the 'p' in the URL (my pages are like?p=trilogy). If it doesn't, it moves to the next line; when it finds a match it simply echoes the second field - the title - into the <TITLE> tags. The extra pipe character in the "echo "¦ $field2";" line is simply because I like my titles in the form "Website ¦ Page".