Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy How to find out the current pages file name

    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

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    PHP Code:
    echo $_SERVER["SCRIPT_NAME"]; 

  • #3
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    $_SERVER['PHP_SELF']?
    http://php.net/reserved.variables

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,961
    Thanks
    2
    Thanked 305 Times in 297 Posts
    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
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $_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

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Using basename($_SERVER['PHP_SELF']) should get what you want. You can even return just the filename without the extension.
    Last edited by _Aerospace_Eng_; 07-18-2007 at 11:56 PM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #8
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by _Aerospace_Eng_ View Post
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •