简体   繁体   中英

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 ie blist.php from the link, not the 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

basename(parse_url($_SERVER['HTTP_REFERER'],PHP_URL_PATH)

这将只为您提供文件名而不是任何子目录或查询字符串

/[\w-]+.php/

您可以使用正则表达式仅获取文件的名称。

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

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;

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM