【问题标题】:Redirect when page isn't exactly found未准确找到页面时重定向
【发布时间】:2014-04-16 18:14:49
【问题描述】:

好的,前几天我在这里发帖,帮了很多忙,所以我决定回来。我对相同的代码有另一个问题。我想向它添加另一个我使用的源 = "$location_url2" 然后我添加了第二个 "$content" 以匹配代码。两个来源之间的区别是“$location_url”如果没有找到则返回一个空白页面,但“$lcation_url2”如果没有找到则重定向到 index.php,但保留 url 上的扩展名......我的代码使用:

<?php
    if(isset($_POST['search'])) {
        $search_text = $_POST['search'];
        $location_url = "http://cydia.saurik.com/package/{$search_text}/"; 
        $location_url2 = "http://rpetri.ch/cydia/{$search_text}/";
        $content = @file_get_contents($location_url);
        $content2 = @file_get_contents($location_url2);

        if($content) {
            header("Location: {$location_url}");
        } else if($content2) {
            header("Location: {$location_url2}");
        } else {
            header("Location: suggest.php");
        }
    }
?>

HTML 代码:

<div id="content">
  <div id="search">
    <form method="POST" name="search" action="search.php">
      <input type="text" name="search" name="placeholder" placeholder="Search Tweaks..."/>
      <input type="submit" name="submit" value="Search"/>
    </form>
  </div>
</div>

【问题讨论】:

    标签: php html search redirect location


    【解决方案1】:

    这可能很难做到,除非您绝对确定这两个链接在未找到任何内容时不会返回任何内容。如果两个位置中的任何一个返回类似 404 的内容,您的脚本会将 404 解释为实际内容并重定向到那里...您可能需要寻找另一种方法来确定网站是否“有内容”

    您可以像这样检查 200 的标题:

    $header=get_headers($url,true);
    

    然后测试 $header[0] 以在其中包含“200”...虽然它使它成为狡猾的代码。如果在任何时候 URL 的任何一个返回任何带有 HTTP/1.1 200 OK 的意外输出,您的脚本都会认为一切正常

    您的代码中还有很多冗余。现在即使 $location_url 中有内容也会下载 $location_url2,这只会消耗资源并使您的脚本编写速度比需要的慢......

    我会按照这样的思路去某个地方:(请记住,您在很大程度上取决于 url 的返回值):

    <?
    
    $search_text=filter_input(INPUT_POST,"search",FILTER_SANITIZE_ENCODED);
    
    if($search_text!=""){
        $location_url = "http://cydia.saurik.com/package/{$search_text}/";
        $location_url2 = "http://rpetri.ch/cydia/{$search_text}/";
    
        $content=@file_header($location_url,true);
        if(strpos($content,"200 OK")!==false) 
            header("Location: {$location_url}");
        else{
            $content=@file_header($location_url2,true);
            if(strpos($content,"200 OK")!==false)
                header("Location: {$location_url2}");
            else
                header("Location: http://www.amazingjokes.com");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 2011-08-11
      • 2018-06-08
      • 2013-06-28
      相关资源
      最近更新 更多