header() 的多个实例 +单行代码中的 die()

发布于 2024-11-13 06:28:18 字数 516 浏览 2 评论 0原文

我正在尝试操作 PHP 脚本,以便它重定向到特定的 URL,而不是给我一个 MySQL 错误。所以我从这个......

$qs = mysql_query("SELECT url FROM $table WHERE `id` = $gid;") or die('MySQL error: '.mysql_error());

到这个:

$qs = mysql_query("SELECT url FROM $table WHERE `id` = $gid;") or header("Location: http://www.example.com");

这有效,但有两件事让我担心。首先,它默认为 302 重定向,我更喜欢 301 重定向。其次,我担心通过从这一行删除 die() ,脚本在重定向后无法正确退出。

现在,我在这里做了一些功课,但我不太清楚是否可以将 die() 与该单行代码中的两个 header() 实例结合起来(即不更改该特定行周围的内容) )。

I'm trying to manipulate a PHP script so that it redirects to a particular URL instead of giving me a MySQL error. So I went from this...

$qs = mysql_query("SELECT url FROM $table WHERE `id` = $gid;") or die('MySQL error: '.mysql_error());

...to this:

$qs = mysql_query("SELECT url FROM $table WHERE `id` = $gid;") or header("Location: http://www.example.com");

Which works, but there are two things that concern me. Firstly, it defaults to a 302 redirect, and I would prefer a 301 redirect. Secondly, I'm worried that by removing die() from this line, the script isn't properly exiting after the redirect.

Now, I've done a bit of homework here, but I can't quite figure out if it's possible to combine die() with two instances of header() in that single line of code (i.e. without changing what's around this particular line).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

春夜浅 2024-11-20 06:28:18

除了“你不应该这样做”的注释之外,你还可以这样做:

$qs = mysql_query(...) or (header(...) xor die);

说明:xor 就像 or 一样,不同之处在于它保证两边的表达式都会被求值并且不会被短路。 (好吧,xoror 不同,但就这个答案而言,这并不重要。)

In addition to the "You shouldn't do this" notes, here's how you could do it:

$qs = mysql_query(...) or (header(...) xor die);

Explanation: xor is just like or with the difference that it is guaranteed that the expressions on both sides get evaluated and are not short circuited. (Okay, xor is something different from or, but for the purpose of this answer that doesn't matter.)

无悔心 2024-11-20 06:28:18
$query_string = '';
$location = '';

$qr = mysql_query($query_string);
if (!$qr) {
  header ('HTTP/1.1 301 Moved Permanently');
  header ('Location: '.$location);
  die('MySQL error: '.mysql_error());
}
$query_string = '';
$location = '';

$qr = mysql_query($query_string);
if (!$qr) {
  header ('HTTP/1.1 301 Moved Permanently');
  header ('Location: '.$location);
  die('MySQL error: '.mysql_error());
}
走走停停 2024-11-20 06:28:18

您可以使用 if 块:

$qs = mysql_query("SELECT url FROM $table WHERE `id` = $gid;");
if (!$qs) {
    header("Location: http://www.example.com");
    die('MySQL error: '.mysql_error());
}

You could use an if block:

$qs = mysql_query("SELECT url FROM $table WHERE `id` = $gid;");
if (!$qs) {
    header("Location: http://www.example.com");
    die('MySQL error: '.mysql_error());
}
豆芽 2024-11-20 06:28:18

你总是可以:

$qs = mysql_query("blah");
if (!$qs) {
   //headers
   header('Location: blah');
   die('MySQL error: '.mysql_error());
}

You could always:

$qs = mysql_query("blah");
if (!$qs) {
   //headers
   header('Location: blah');
   die('MySQL error: '.mysql_error());
}
落墨 2024-11-20 06:28:18

显示 die 消息是没有意义的,因为任何浏览器都会捕获 HTTP 状态代码并采取相应的操作,同时忽略文档的内容。在那里,您应该使用 exit() 命令,该命令执行相同的操作,减去将任何内容回显到缓冲区。

您可能正在使用此后备操作,那么为什么不将其设为常见的后备函数:

function fallback($location='/default/path/to/somewhere') {
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: '.$location);
    exit();
}

$query = mysql_query('...') or fallback('/path/to/somewhere/');

There is no purpose to having the die message show up since any browser would catch the HTTP status code and act accordingly while ignoring the content of the document. There you should be using the exit() command which does the same thing minus echo'ing anything to the buffer.

You may be using this fallback operation so why not just make it a common fallback function:

function fallback($location='/default/path/to/somewhere') {
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: '.$location);
    exit();
}

$query = mysql_query('...') or fallback('/path/to/somewhere/');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文