Reputation: 2603
In PHP 5.6, if I am on a page A, and if page A launch page B, is there a way to get the URL of a page A from page B?
Please note that I do not want to use query parameters to pass the values of a URL A when I launch page B from page A.
Upvotes: 2
Views: 83
Reputation: 785
You can check it using $_SERVER['HTTP_REFERER']
. The HTTP referer header will return a string. If you don't know how to work with this you can try:
echo $_SERVER['HTTP_REFERER'];
If your user came by google page, $_SERVER['HTTP_REFERER']
will give you http://www.google.com
And, of course, check the documentation: http://php.net/manual/en/reserved.variables.server.php
Upvotes: 3
Reputation: 156
If the browser (user agent) supports it, you could use:
$_SERVER['HTTP_REFERER']
'HTTP_REFERER'
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
From: http://php.net/manual/en/reserved.variables.server.php
Upvotes: 4
Reputation: 7103
If launch
means include
or require
, then you can get URL with this (printing example):
echo $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
$_SERVER['HTTP_HOST']
will print base URL and $_SERVER['SCRIPT_NAME']
will print the remaining part (subfolders and file name), like:
http://example/web/pageb.php
I have tested this to make sure it works. But be sure to escape as there could be vulnerabilities here. It also works if $_SERVER['HTTP_REFERER']
is null
in your case.
Upvotes: 2