Andy
Andy

Reputation: 3021

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 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?

Upvotes: 2

Views: 16066

Answers (8)

Aidin Azari
Aidin Azari

Reputation: 57

with extension:

pathinfo($_SERVER['HTTP_REFERER'], PATHINFO_BASENAME);

without extension:

pathinfo($_SERVER['HTTP_REFERER'], PATHINFO_FILENAME);

Upvotes: 0

Aba
Aba

Reputation: 614

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

This will give you just file name not any sub-directories or the query string

Upvotes: 0

xkeshav
xkeshav

Reputation: 54016

try with

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

Note: this variable doesn't exist in mobile devices.

Upvotes: 8

Thammasak
Thammasak

Reputation: 71

Try this one:

basename($_SERVER['HTTP_REFERER']);

REF: http://www.php.net/manual/en/function.basename.php

Upvotes: 7

kharles
kharles

Reputation: 326

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

Upvotes: 0

S L
S L

Reputation: 14318

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;

Upvotes: 0

Hal
Hal

Reputation: 1293

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

Upvotes: 0

Headshota
Headshota

Reputation: 21449

/[\w-]+.php/

you can use the regular expression to get only name of the file.

Upvotes: 0

Related Questions