PHPで現在・前のページのURL、アドレスの分解
現在のページ
現在のページ
echo $_SERVER["REQUEST_URI"];
ホスト名 + 現在のページ
echo $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
プロトコル + ホスト名 + 現在のページ
echo (empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
https://hogehoge.com/test.php
前のページ
echo $_SERVER['HTTP_REFERER'] ?? "";
https: から http: へはReferer は送られなかったり meta name="referrer" など、空の場合もあります。
アドレスの分解
$url = parse_url("http://hogehoge.com/test1/test2/test3.php");
print_r($url);
結果
Array
(
[scheme] => http
[host] => hogehoge.com
[path] => /test1/test2/test3.php
)