Skip to content

Instantly share code, notes, and snippets.

@i5ar
Last active August 17, 2017 16:38
Show Gist options
  • Save i5ar/01c79ff066a59b666812 to your computer and use it in GitHub Desktop.
Save i5ar/01c79ff066a59b666812 to your computer and use it in GitHub Desktop.
Get last URL visited
<?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