Skip to content Skip to sidebar Skip to footer

Find Redirected Img Source

I have a URL to the latest Webcam shot http://cam.hackerspace.sg/last.jpg and it redirects to its source filename for example. 1364124301.jpg I want to read the Epoch '1364124301'

Solution 1:

Using JavaScript alone it seems impossible to get the redirect location: A XMLHTTPRequest hides the redirect transparently and an iframe does not allow to access it's URL if it's from another domain.

If you have a PHP enabled website the following will allow to get the redirect. (All code is without any capture of error conditions for simplicity, one will need to add error handling before using it).

First, to get the redirect location, some PHP, the file is called getRedirect.php:

<?php$redirect = '';
$urlToQuery = $_GET['url'];
$urlParts = parse_url($urlToQuery);
$host = $urlParts['host'];
$path = $urlParts['path'];
$address = gethostbyname ($host);
$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect ($socket, $address, 80);
$request = 'GET '.$path.' HTTP/1.1'."\r\n";
$request .= 'Host: '.$host."\r\n";
$request .= 'Connection: Close'."\r\n\r\n";
socket_write($socket, $request);
$answer = socket_read($socket, 8192);
socket_close($socket);
$answerLines = explode("\r\n", $answer);
foreach ($answerLinesas$line) {
    $lineParts = explode(':', $line, 2);
    if($lineParts[0] == 'Location') {
    $redirect=trim($lineParts[1]);
    break;
    }
}
header('Content-Type: text/plain; charset=us-ascii');
header('Content-Length: '.strlen($redirect));
echo$redirect;
?>

Second, the image display page which uses JavaScript to set up the image and to query the redirect location (via XMLHTTPRequest to getRedirect.php):

<!DOCTYPE html><html><head><title></title><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><scripttype="text/javascript">functionupdateImage() {
    var imgParent = document.getElementById("imgContainer");
    // delete existing image if existsvar imgChild = imgParent.firstChild;
    if(imgChild!=null) imgParent.removeChild(imgChild);
    // insert new image with actual redirectvar newImg = document.createElement("img");
    newImg.src = "http://cam.hackerspace.sg/last.jpg";
    imgParent.appendChild(newImg);
    var redirect = getRedirect("http://cam.hackerspace.sg/last.jpg");
    var timestamp = redirect.substring(redirect.lastIndexOf("/") + 1, redirect.lastIndexOf("."));
    document.getElementById("imageDate").innerHTML="Timestamp="+timestamp;

    }
    functiongetRedirect(url) {
    var redirect = "";
    var req = newXMLHttpRequest();
    req.open('GET', 'getRedirect.php?url='+url, false);
    req.onreadystatechange = function () {
        if (req.readyState == 4) {
        redirect = req.responseText;
        }
    };
    req.send(null);
    return redirect;
    }
</script></head><body><buttononclick="updateImage();">update image</button><divid="imgContainer"></div><pid="imageDate"></p></body></html>

Solution 2:

Using XMLHTTPRequest, you can read the newUrl after being redirected in XMLHTTPRequest.responseURL property. By comparing the newUrl with the originUrl, you can find out if it's a redirect.

Post a Comment for "Find Redirected Img Source"