Multiple instances of header() + die() in single code line
Asked Answered
I

5

7

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).

Ilyse answered 4/6, 2011 at 21:14 Comment(1)
please tell me you're sanitizing your inputs there. D:Danuloff
C
4

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.)

Chan answered 4/6, 2011 at 21:22 Comment(1)
This keeps it nicely on a single line, and works wonderfully. I had to add in the 301 and ended up with $qs = mysql_query(...) or die(header('Location: http://www.example.com',TRUE,301)); It looks like I didn't need another occurrence of header() just for the 301; the $http_response_code parameter does the same. Thanks!Ilyse
E
3

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());
}
Enwind answered 4/6, 2011 at 21:17 Comment(2)
mysql_query does not use exceptions; that would be PDO with ERRMODE_EXCEPTION in use.Danuloff
How is this going to work? mysql_query does not generate any exceptions.Nautilus
K
3
$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());
}
Karol answered 4/6, 2011 at 21:21 Comment(0)
T
2

You could always:

$qs = mysql_query("blah");
if (!$qs) {
   //headers
   header('Location: blah');
   die('MySQL error: '.mysql_error());
}
Truax answered 4/6, 2011 at 21:17 Comment(3)
You need to suppress the error from mysql_query; otherwise, it's going to throw an error.Enwind
@mc10 that's dependent on php configuration settings, but might be a valid point.Truax
Ugh, didn't see that this was mysql_query, not mysql_connect. Forget my comment.Enwind
W
0

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/');
Would answered 4/6, 2011 at 21:27 Comment(1)
fyi, die() is an alias of exit() and behaves exactly the same: us3.php.net/manual/en/function.die.php -- you can just use die() and it won't echo anything to the buffer either.Danuloff

© 2022 - 2024 — McMap. All rights reserved.