PDA

View Full Version : including a function from another file.



killerchutney
11-11-2007, 04:46 PM
hey all, I am trying to make a simple script that detects the URL, and changes some CSS attributes accordingly. I have done all the script, but I have encountered a problem. I could call the function that gets the page file name in the include page, but then it only detects the file name of the include page, but for the script to operate correctly it needs to detect the main file name.

Heres a layout of how it works:



file.php [

include to header.php - for cosmetical reasons the div tag im editing needs to be in this file. This is also the file that contains the script that needs the function.

actual function here.

]


I have tried it and I get this error: Fatal error: Call to undefined function: curpagename() in /home/killerch/public_html/templates/header.php on line 61

I know what it means, I just cant see any logical way to get past it :S


Thanks

thetestingsite
11-11-2007, 04:52 PM
What I usually do is centralize all of my functions in one file (like functions.php), then include that file with whatever other files I need those functions on; if that makes any sense. Why not try that?

Hope this helps.

killerchutney
11-11-2007, 04:53 PM
EDIT: ok, solved but came into a new problem...

lol.php (the file name I need to detect). header.php is included. header.php contains the script, and it needs to be here for asthetical reasons, and the function. I need to get the page name of lol.php - is this possible?

thetestingsite
11-11-2007, 05:34 PM
Try this to see if it is something like what you are talking about:

test.php


<?php
include('test2.php');

echo '<BR>';

echo 'This is '.getpage();

?>


test2.php


<?php

echo 'Testing from test2.php';

function getpage() {

$this_page = $_SERVER['PHP_SELF'];

return $this_page;
}
?>


Working Demo: http://thetestingsite.net/test.php

Hope this helps.

killerchutney
11-11-2007, 05:46 PM
yes, thats it pretty much - but I need to use the output from that in footer.php

thetestingsite
11-11-2007, 05:53 PM
k...check the link again to see if this works for you:

test.php


<?php
include('test2.php');

echo '<BR> This is some text from the actual page test.php <BR>';

include('footer.php');
?>


test2.php


<?php

echo 'Testing from test2.php';

function getpage() {

$this_page = $_SERVER['PHP_SELF'];

return $this_page;
}
?>


footer.php


<?php
echo 'The current page is: '.getpage();
?>


Hope this helps.

killerchutney
11-11-2007, 06:06 PM
footer.php gives me the same error.

but I have made an online example.

http://www.killerchutney.com/lol.php (aptly named :p) heres the page I need to detect through header.php.

heres the code in header.php
<div <?php
function getpage() {
$this_page = $_SERVER['PHP_SELF'];
return $this_page;}

if (getpage() == 'index.php'){
echo ('id="content"');}

elseif (getpage() == 'lol.php'){
echo ('style="background-color:#ff00f6;"');}

elseif (getpage() == '/templates/header.php'){
echo ('style="background-color:#3cff00;"');}

else{ echo ('style="background-color:#000000;"'); }?>>

if the script is working correctly, the background color of lol.php should be pink. If it is working incorrectly then it should be green (as it is now).

you can see in the left corner of the green div /templates/header.php - the include file name. That is using the same function as the one you posted - but in the address bar it says http://www.killerch.../lol.php I need it to detect the URL shown in the address bar, instead the URL of the include.

Twey
11-11-2007, 06:35 PM
Check $_SERVER['REQUEST_URI'].

killerchutney
11-11-2007, 07:06 PM
I have tried changing

$this_page = $_SERVER['PHP_SELF'];

to $_SERVER['REQUEST_URI'];

but the same result.

EDIT: after looking up REQUEST_URI on the php documentation, I think PHP_SELF is a better solution for me - i dont want the vairables to be included.

Twey
11-11-2007, 08:17 PM
It sounds to me as if you're including it via HTTP (i.e. include 'http://...';).

killerchutney
11-11-2007, 08:54 PM
would that make any difference? I have heard that it might not work on some PHP installations, but everything in the header shows fine.

thetestingsite
11-11-2007, 09:18 PM
would that make any difference? I have heard that it might not work on some PHP installations, but everything in the header shows fine.

Yes, it would make a big difference. If it was including the file using the full url (ex: http://domain.tld/blah.php) instead of a relative or absolute url (ex: ../test.php or /home/blah/public_html/test.php), it would; to my understanding, just include the outputted text (the text outputted to the browser, whether it be html, javascript, etc.).

Hope this helps.

killerchutney
11-12-2007, 05:04 PM
ah!!! thank you very much. this just made it work!! (after changing a few things another forum said would work better). Thank you again thetestingsite and twey :D