-
Need PHP help
I have a header.php and I need the file to change depending on which page includes it.
Basically, say I open a page called 1.php. This page includes header.php. I need header.php to change because it is being called by 1.php. Then, I need it to change again if it is called by 2.php etc etc.
I don't know if this is possible. I asked my IT teacher who told me it would probably be done using sessions, but I have no idea how to do that.
-
Your IT teacher is crazy then.
Let me get this correct.
1.php
-> Header.php
2.php
-> Header.php
3.php
-> Header.php
You could just set a variable?
in:
PHP Code:
<?php
$bla = "bla";
include "header.php";
?>
In header.php
PHP Code:
<?php
echo $header;
?>
If your using a function in header.php your going to need to define that variable as a global.
-
Here's some psuedo code for people to help them understand.
If header.php is being called from 1.php, I need a certain part of the document to echo 1.
If header.php is being called from 2.php, I need a certain part of the document to echo 2.
Etc.
1.php and 2.php aren't their real filenames though.
-
Well the easiest way I can think of right now is the way I said.
if 1.php was named bla.php:
bla.php:
PHP Code:
<?php
// BLA.php
$name = "bla";
include "header.php";
?>
header.php
PHP Code:
<?php
// Header
echo "This page is being called from: $name";
?>
-
PHP Code:
<?php
if($_SERVER['REQUEST_URI'] == '/1.php') {
echo '1';
} elseif($_SERVER['REQUEST_URI'] == '/2.php') {
} else {
}
?>
Not complete, but you get the idea, check out if the reserved variable I used is the correct one and make sure it uses the / and not just 1.php
-
Totally forgot about REQUEST_URI, even though I used it in my script less then an hour ago.
-
1.php
PHP Code:
<?php
include('header.php?num=1');
?>
header.php
PHP Code:
<?php
echo $_GET['num']; // It will echo whatever '?num=' is
?>
That would be another way to do it.... maybe not exactly what you're looking for but it can be modified for your exact needs :)
-
You can't include files with variables.. it can only be direct filenames.
-
I'll try Ryan's way and post back with any problems. Thanks a lot guys, +rep to all