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 5 of 5
  1. #1
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    884
    Thanks
    173
    Thanked 101 Times in 101 Posts

    is php_self still ok?

    Hi guys

    I'm having a go at building a basic php navigation for my site and basically it's based on this

    PHP Code:
    basename($_SERVER["PHP_SELF"]) 
    It's working, but a thought came to my head --- is this code reliable?

    ie is this code still used? Is there some other newer way I should be using?

    I just need file name to determine navigation

    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Depends on what you are using it for.
    That will work for the accessed page, but not for inclusions. So the running script is PHP_SELF. You can probably save effort by using SCRIPT_NAME instead, but if you want to operate on a specific file regardless of inclusion levels, use the __FILE__ magic constant.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    884
    Thanks
    173
    Thanked 101 Times in 101 Posts
    Hi Fou-Lu

    Thanks for reply

    Yeh navigation is in an include.

    so I should use this right

    PHP Code:
    basename($_SERVER["SCRIPT_NAME"]); 
    I grab the filename and echo nav content based on that -- just if else stuff

    It's working on localhost, but I don't want to do it all only to find it's not reliable on server.

    My script hinges on that one line really.

    I don't know how to use the __FILE__ magic constant.

    atm my include looks like this at start

    PHP Code:
    $currentFile basename($_SERVER["PHP_SELF"]);
    $parts Explode('/'$currentFile);
    $show $parts[count($parts) - 1]; 
    with rest of nav based on $show

    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    SCRIPT_NAME should always be available in a web environment.
    I'm not really sure what you are doing here. The $currentFile created using the basename will just be myscript.php, without any path, so the parts and show variables don't have any value.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    low tech (02-12-2013)

  • #5
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    884
    Thanks
    173
    Thanked 101 Times in 101 Posts
    Hi Fou-Lu

    The $currentFile created using the basename will just be myscript.php,
    Yeh, I saw that eventually. I've changed it now and removed the code that was being used to parse php_self.

    Just learning as I go:-)

    Thanks for yr help

    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.


  •  

    Posting Permissions

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