如果页面为空,则重定向

时间:2014-04-13 21:44:23

标签: php search redirect location

我正在使用某种搜索引擎,它会添加到URL以完成请求。使用标题(位置:http://www.WEBSITENAME.com/'。$ search);虽然有时这会将我发送到空白页面。想知道是否有任何类型的代码会在发生错误时将我重定向到另一个页面。如果页面为空白,我希望else重定向到它。谢谢。
我的代码:

的search.php


    <?php
     $search = $_POST["search"];
 if(isset($_POST['search'])){
    header('Location:http://cydia.saurik.com/package/'.$search);
}else{
    ?>
    <?php
    echo "
        <head>
            <style>
                #header{
                    color:black;
                }
                .header{
                    color:black;
                    font-size:25px;
                }
                #header_minor{
                    margin-top:20px;
                }
                .header_minor{
                    font-size:18px;
                }
                a[class='header_minor']{
                    color:black;
                }
                body{
                    text-align:center;
                    margin-top:14%;
                }
                #idea{
                    margin-top:20px;
                }
                .hidden{
                    display:none;
                    visibility:hidden; 
                }
            </style>
        </head>
    ";
    ?>
    <div id="header">
        <span class="header">
            Sorry, this tweak was not found...
        </span>
    </div>
    <div id="header_minor">
        <span class="header_minor">
            Would you like to suggest it? or <a href="index.php" class="header_minor">Return home</a>
        </span>
    </div>
    <div id="idea">
        <form method="POST" action="idea.php" name="idea">
            <input type="text" name="idea" value="<?php echo $_POST['search']; ?>"/>
            <input type="text" name="hidden" class="hidden"/>
            <input type="submit" value="Submit"/>
        </form>
    </div>
    <?php
    exit();
}

&GT;

1 个答案:

答案 0 :(得分:0)

如果你可以在该网站上调用api,这样可以更好地与外部网站进行交互。

我会做这样的事情:

PHP:

<?php
if(isset($_POST['search'])) {
    $search_text = $_POST['search_field'];
    $location_url = "http://cydia.saurik.com/package/{$search_text}/";
    $content = @file_get_contents($location_url); // use this function to query on site
    if($content) {
        // redirect if it has a result
        header("Location: {$location_url}");
    } else {
        // will go here if query on external site is empty (blank)
        // create your own empty result page that says your search yielded no     results (your own page, redirect the user)
        // sample
        header("Location: http://www.yoursite.com/index.php?noresults=1");
    }
}

?>

HTML:

<form method="POST" action="index.php">
    <input type="text" name="search_field" class="" id="" /><br/>
    <input type="submit" name="search" class="" id="" value="Search" />
</form>

<?php if(isset($_GET['noresults'])): ?>
    <div class="yourdesign" style="display: block; width: 300px; border: 1px solid black;";>
        <h1>No results found</h1>
    </div>
<?php endif; ?>