...

View Full Version : How to get the php filename?



skcin7
03-02-2010, 07:40 PM
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?

mlseim
03-02-2010, 07:45 PM
$url = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

echo $url;

MattF
03-02-2010, 07:53 PM
http://uk2.php.net/manual/en/function.basename.php

Fou-Lu
03-02-2010, 08:04 PM
$basename = basename(__FILE__);


Use __FILE__ to control relativity, SCRIPT_NAME to control executor.

skcin7
03-02-2010, 08:18 PM
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/file.php?test=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.

MattF
03-02-2010, 08:20 PM
The referrer header is set by the client, and can also be disabled.

skcin7
03-02-2010, 08:22 PM
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.

kbluhm
03-02-2010, 08:26 PM
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/file.php?test=yes&crud=yes. What is PHP code to get JUST the 'file.php?test=yes&crud=yes' part out of the URL?



$uri = basename( $_SERVER['SCRIPT_NAME'] );

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

Fou-Lu
03-02-2010, 08:27 PM
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.


Hah, well beaten by kbluhm :P

skcin7
03-02-2010, 08:37 PM
Thanks, you guys rule.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum