-1

我目前在一个股票报价网站上工作,当我使用报价符号搜索报价信息时,我的页面中有这种情况。如果在数据库中找不到符号,我需要将用户重定向到“查找符号”页面。这是条件语句。

if($result)
{ }
else
{
//page redirect
}

我需要知道如何将某人重定向到我的 else 语句中的页面,有什么建议吗?

先感谢您!

4

6 回答 6

2

利用

header("Location: http://www.example.com/"); /* Redirect */
exit; // important to make sure nothing else gets executed after this line
于 2012-05-30T19:09:14.267 回答
1
header( "Location: http://your-target-site.com/yourpage" );
exit;

重要的是要知道header()如果生成了任何其他输出,该语句将失败。确保您的页面在此语句之前不产生任何输出。

于 2012-05-30T19:08:20.850 回答
1

输出位置标头

{
    header('Location: http://yourdomain.com/nextpage.php');
    exit;
}
于 2012-05-30T19:09:05.953 回答
1

正如其他人所说,您在 php 中使用 header 函数,但这仅在之前没有输出的情况下才有效。如果您确实有输出,则唯一的选择是使用 javascript。

echo "<script>window.location = 'http://www.example.com';</script>";

这不是一个很好的解决方案,因为如果浏览器没有启用 javascript,它不会 100% 工作。所以只有在你绝对不能使用 header() 时才使用它。

于 2012-05-30T20:23:38.363 回答
0

header()用于发送原始 HTTP 标头。有关 HTTP 标头的更多信息,请参阅» HTTP/1.1 规范

if($result)
{ }
else
{
    header("Location: http://google.com");
}

在此处查看更多示例:http: //ditio.net/2008/10/04/php-redirect-header-location/

于 2012-05-30T19:08:41.630 回答
0

只要还没有一些输出,就使用 header() 函数。

header("Location: http://bleh.com");
于 2012-05-30T19:08:49.860 回答