PHP / JS - Get a list of all redirects
I'm looking for a way to get a list of all re-directs once a user clicks a link on a webpage. I wa开发者_运维知识库nt to basically reference what was originally displayed in the address bar at the time the user clicked the link. The issue is, after the user clicks the link the website proceeds to redirect the user several times in the background.
I tried storing the referring URL in a session variable but it's simply returning the last visited re-direct. The one i'm interested in is likely several layers back. So I want to generate an array to go through.
Here is what i tried so far:
<?php session_start();
if (!isset($_SESSION["origURL"]))
$_SESSION["origURL"] = $_SERVER["HTTP_REFERER"];
$redirects = array("1" => $_SESSION["origURL"]);
?>
solution should be in either PHP or JS or a combo of both.
Save array in session not a last URL string then.
session_start();
if( !isset($_SESSION['redirects']) ) $_SESSION['redirects'] = array();
$_SESSION['redirects'] [] = $_SERVER["HTTP_REFERER"];
Then just check your $_SESSION['redirects']
array.
A hacky javascript solution can be the following
(if you can run code on the page load)
var Beacon = new Image();
Beacon.src = MY_URL.php+'?r='+window.location;
then in your server side code have an array stored in the session
$_SESSION['myRedirects'].push( $_GET['r'] );
If you can only insert code later ( JS ), things get trickier... You will have to add a listener to most clicks, and once they 're clicked prevent the default action (preventDefault) - create the beacon / ajax request whichever you like, and then change the window.location.href to the clicked elements href value.
this solution can work - but youwill have to determine which links produce redirects and not a javascript function that might be stopped when preventing the links default behavior etc.
精彩评论