Last active
August 17, 2017 16:38
-
-
Save i5ar/01c79ff066a59b666812 to your computer and use it in GitHub Desktop.
Get last URL visited
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// strrpos(); finds the position of / from the end of the string; | |
// substr(); extracts a sub string till the position of / found in previous step. | |
$url = $_SERVER['HTTP_REFERER']; | |
echo $url . '<br />'; // http://localhost/mydomain/first/second/ | |
$link = substr($url, 0, strrpos($url, "/")); | |
echo $link; // http://localhost/mydomain/first/second | |
?> | |
<?php | |
// Alternative basename(); | |
$url = $_SERVER['HTTP_REFERER']; | |
$base = basename( $url ); | |
echo $base . '<br />'; | |
$second_url = substr( $url, 0, strrpos( $url, $base ) ); | |
echo $second_url . '<br />'; | |
$penultimate_base = basename( $second_url ); | |
echo $penultimate_base . '<br />'; | |
$third_url = substr( $second_url, 0, strrpos( $second_url, $penultimate_base ) ); | |
echo $third_url; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment