Get the previous page name in PHP (not the entire URL)
I have a URL like this;
http://www.mydomain.co.uk/blist.php?prodCodes=NC023-NC022-NC024-NCB33&customerID=NHFGR
Which i grab using HTTP Referrer. The trouble is i only need the page name i.e. blist.php from the link, not t开发者_JAVA百科he entire URL as is default using:
$_SERVER['HTTP_REFERER']
Can anyone give me an idea on how to grab that part of the URL?
try with
parse_url($_SERVER['HTTP_REFERER'],PHP_URL_PATH);
Note: this variable doesn't exist in mobile devices.
Try this one:
basename($_SERVER['HTTP_REFERER']);
REF: http://www.php.net/manual/en/function.basename.php
This seems to work, but there might be other elegant url functions.
$url = 'http://www.mydomain.co.uk/blist.php?prodCodes=NC023-NC022-NC024-NCB33&customerID=NHFGR';
preg_match('/\/[a-z0-9]+.php/', $url, $match);
$page = array_shift($match);
echo $page;
/[\w-]+.php/
you can use the regular expression to get only name of the file.
I think you are looking for $_SERVER['REQUEST_URI'] which just gives you the requested page.
You can review all of the $_SERVER variables here:
http://php.net/manual/en/reserved.variables.server.php
check out
http://ca.php.net/manual/en/function.parse-url.php
focus in on the path ($_SERVER['REQUEST_URL']) portion, then do something like substr($path,strrpos($path,"/")+1);
basename(parse_url($_SERVER['HTTP_REFERER'],PHP_URL_PATH)
This will give you just file name not any sub-directories or the query string
精彩评论