Php 页面不为'时重定向;确实找不到
好吧,前几天我在这里发了帖子,这帮了我很多忙,所以我决定回来。我有另一个问题与相同的代码。我想添加另一个源代码,我使用了=“$location\u url2”,然后添加了第二个“$content”来匹配代码。这两个源之间的区别是,“$location\u url”如果找不到任何内容,将返回一个空白页面;如果找不到任何内容,“$lcation\u url2”将重定向到index.php,但将扩展名保留在url上。。。我正在使用的代码:Php 页面不为'时重定向;确实找不到,php,html,search,redirect,location,Php,Html,Search,Redirect,Location,好吧,前几天我在这里发了帖子,这帮了我很多忙,所以我决定回来。我有另一个问题与相同的代码。我想添加另一个源代码,我使用了=“$location\u url2”,然后添加了第二个“$content”来匹配代码。这两个源之间的区别是,“$location\u url”如果找不到任何内容,将返回一个空白页面;如果找不到任何内容,“$lcation\u url2”将重定向到index.php,但将扩展名保留在url上。。。我正在使用的代码: <?php if(isset($_POST['s
<?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>
这可能很难做到,除非您完全确定这两个链接在未找到任何内容时不会返回任何内容。如果两个位置中的任何一个返回类似404的内容,您的脚本将把404解释为实际内容并重定向到那里。。。您可能需要寻找另一种方法来确定网站是否“有内容” 您可以检查标题中的200,如下所示:
$header=get_headers($url,true);
然后测试$header[0]中是否有“200”。。。不过,这使得它的代码很不可靠。如果在任何时候,URL的其中一个返回任何意外的输出,并且头中有HTTP/1.1 200 OK,那么您的脚本将认为一切正常
代码中还有很多冗余。现在,即使$location\u url中有内容,也会下载$location\u 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");
}
}