...

View Full Version : How to find out the current pages file name



rustybones
07-18-2007, 12:29 PM
Hi guys,
this is probably very basic, though from my search for the past couple of hours I can't seem to dig anything up... I'm positive I must've missed something. Any way the question is:

How do I find out what the current pages file name is?

My current scenario is I have a header.php file that is included on every one of my pages and inside this header.php file I want some code that looks at the current URL and checks to see if the user is currently on contact.php or not.

If the true then I include some files, if false I do nothing.

I hope this makes sense.
Cheers :)

Nightfire
07-18-2007, 12:34 PM
echo $_SERVER["SCRIPT_NAME"];

rustybones
07-18-2007, 01:29 PM
Okay cool, I've got that far. Now what if these files are in a sub directory, the info brought back then is /subdir/contact.php

Thing is though the script is only checking for contact.php so it fails everytime. I know I'm being picky and I thank you for your help so far. I would just prefer the script to return contact.php instead of /subdir/contact.php

Is there any way to remove the parent directories?

rafiki
07-18-2007, 02:22 PM
$_SERVER['PHP_SELF']?
http://php.net/reserved.variables

CFMaBiSmAd
07-18-2007, 02:33 PM
It sounds like a better solution would be to remove the unique/special "contact.php" processing from the header.php file and put it into the contact.php file before the include statement for the header.php file and then just use the results of the special processing in the header.php code.

If you have special processing for one file now, just make it generic so that if you expand this to have unique/special processing for other files in the future, you won't need to keep changing the code in header.php

rustybones
07-19-2007, 12:19 AM
$_SERVER['PHP_SELF'] is the document relative to the document root, so it still returns /subdir/contact.php, or from what I've tested and read it does.

CFMaBiSmAd that's a good suggestion, in doing that I can assign the page name to a variable and just use the variable in the header.php, this is though this method still will give me the result of /subdir/contact.php

Is there a way to just get the information after the last slash / ?

cheers again guys, I really appreciate your help :)

_Aerospace_Eng_
07-19-2007, 12:54 AM
Using basename($_SERVER['PHP_SELF']) should get what you want. You can even return just the filename without the extension.

rustybones
07-19-2007, 09:04 AM
Using basename($_SERVER['PHP_SELF']) should get what you want. You can even return just the filename without the extension.

Bang on! that's exactly what I wanted, thank you very much :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum