捕获所有常见类型的重定向,标题,元,JavaScript等

时间:2012-09-12 07:26:13

标签: php javascript redirect http-headers http-redirect

如果通过任何方式重定向,我需要一个测试URL的函数。

到目前为止,我已经使用cURL来捕获标头重定向,但显然有更多方法可以实现重定向。

EG。

<meta http-equiv="refresh" content="0;url=/somewhere/on/this/server" />

或JS脚本

window.location = 'http://melbourne.ag';

我想知道是否有人有解决方案可以涵盖所有人。我将继续努力,并将结果公布在此处。

另外,一种快速解析方法

<meta http-equiv="refresh"... 

在PHP中有人吗?

我认为这将包含在PHP的原生get_meta_tags()中...但我认为错了:/

2 个答案:

答案 0 :(得分:0)

它可以用于标记语言(任何简单的标记解析器都可以),但是对于像JavaScript这样的编程语言通常无法完成。

Web文档中程序的重定向等同于暂停该程序。您要求的程序能够判断另一个任意程序是否会停止。这在计算机科学中被称为halting problem,这是第一个不可判定的问题。

也就是说,无论是否重定向,您都只能告诉正确 资源。

答案 1 :(得分:-1)

在那里,我会在编写时添加JS检查......

 function checkRedirect($url){
    // returns the redirected URL or the original
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    $out = curl_exec($ch);

    $out = str_replace("\r", "", $out);

    $headers_end = strpos($out, "\n\n");
    if( $headers_end !== false ) { 
        $out = substr($out, 0, $headers_end);
    }   

    $headers = explode("\n", $out);
    foreach($headers as $header) {
        if(strtolower(substr($header, 0, 10)) == "location: " ) { 
            $target = substr($header, 10);
            return $target;
        }   
    }   

     return $url;
 }