Log in

View Full Version : Need PHP help



darkchicken101
11-02-2008, 09:51 PM
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.

Zedtu
11-02-2008, 09:53 PM
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
$bla = "bla";
include "header.php";
?>


In header.php



<?php
echo $header;
?>


If your using a function in header.php your going to need to define that variable as a global.

darkchicken101
11-02-2008, 09:57 PM
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.

Zedtu
11-02-2008, 09:59 PM
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
// BLA.php

$name = "bla";
include "header.php";
?>


header.php



<?php
// Header

echo "This page is being called from: $name";

?>

RYANNNNN
11-02-2008, 10:17 PM
<?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

Zedtu
11-02-2008, 10:19 PM
Totally forgot about REQUEST_URI, even though I used it in my script less then an hour ago.

QuickScriptz
11-02-2008, 10:45 PM
1.php

<?php
include('header.php?num=1');
?>

header.php

<?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 :)

Zedtu
11-02-2008, 10:48 PM
You can't include files with variables.. it can only be direct filenames.

darkchicken101
11-02-2008, 11:46 PM
I'll try Ryan's way and post back with any problems. Thanks a lot guys, +rep to all

Want to hide these adverts? Register an account for free!