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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jul 2009
    Posts
    186
    Thanks
    72
    Thanked 2 Times in 2 Posts

    How to get the php filename?

    I have a question which I am assuming is somewhat easy. Let's say I am running a PHP file located at http://mysite.com/stuff/test/more/file.php. What is PHP code to get JUST the 'file.php' part out of the URL?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    $url = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

    echo $url;

  • Users who have thanked mlseim for this post:

    skcin7 (03-02-2010)

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts

  • Users who have thanked MattF for this post:

    skcin7 (03-02-2010)

  • #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
    PHP Code:
    $basename basename(__FILE__); 
    Use __FILE__ to control relativity, SCRIPT_NAME to control executor.
    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 ;)

  • #5
    Regular Coder
    Join Date
    Jul 2009
    Posts
    186
    Thanks
    72
    Thanked 2 Times in 2 Posts
    Ok thanks guys for your help. Now I got another quick question. Let's say I am running a PHP file located at http://mysite.com/stuff/test/more/fi...t=yes&crud=yes. What is PHP code to get JUST the 'file.php?test=yes&crud=yes' part out of the URL?

    I tried using $_SERVER['HTTP_REFERER'] and just appending it to the end of the URL (using mlseim's technique to get the URL), but HTTP_REFERER is always returning null for some reason. Plus I looked up on the internet and people said to stay away from HTTP_REFERER when you can so I would like to stay away from it if possible.

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    The referrer header is set by the client, and can also be disabled.

  • #7
    Regular Coder
    Join Date
    Jul 2009
    Posts
    186
    Thanks
    72
    Thanked 2 Times in 2 Posts
    The weird thing is that HTTP_REFERER was just working for me (i was testing and playing around with it) but now for some reason it is not working.

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by skcin7 View Post
    Ok thanks guys for your help. Now I got another quick question. Let's say I am running a PHP file located at http://mysite.com/stuff/test/more/fi...t=yes&crud=yes. What is PHP code to get JUST the 'file.php?test=yes&crud=yes' part out of the URL?
    PHP Code:
    $uri basename$_SERVER['SCRIPT_NAME'] );

    if ( ! empty( 
    $_SERVER['QUERY_STRING'] ) )
    {
        
    $uri .= '?' $_SERVER['QUERY_STRING'];


  • Users who have thanked kbluhm for this post:

    skcin7 (03-02-2010)

  • #9
    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
    Yes, referrer is no guarentee.
    The file can be retrieve with the __FILE__ or SCRIPT_NAME if you're looking for the executor, query string should be retreivable by $_SERVER['QUERY_STRING']. That should always be set since its the webserver that sets that one up.

    Edit:
    Hah, well beaten by kbluhm :P
    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:

    skcin7 (03-02-2010)

  • #10
    Regular Coder
    Join Date
    Jul 2009
    Posts
    186
    Thanks
    72
    Thanked 2 Times in 2 Posts
    Thanks, you guys rule.


  •  

    Posting Permissions

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