Forum Moderators: coopster

Message Too Old, No Replies

Redirect not working

even though no HTML sent yet

         

Purple Martin

12:33 am on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, I know this is a common problem but I still can't make it work. I want to redirect a PHP page if the MYSQL database is not available (I'm stopping the MYSQL service to test this).

I know I have to do the redirect before any HTML is sent, even before any whitespace is sent. But even though I've got the code right at the start of the file, I still get the error message "Warning: Cannot modify header information - headers already sent..."

<?php
$connection = mysql_connect("host", "username", "password")
or die(header("Location: other_file.html"));
?>

ergophobe

12:43 am on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Bizarre. I know you've already answered this generally, but for lack of anything better to offer..
- That's the top of the only (or first) file being opened?
- There are no sessions being started or cookies set or anything like that?
- If you just output a message on die() and view source, there's no space?

Tom

Purple Martin

1:27 am on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



- That's the top of the only (or first) file being opened? - Yes
- There are no sessions being started or cookies set or anything like that? - No
- If you just output a message on die() and view source, there's no space? - Source is as follows, with no space or line before this (outputting "foo" on die):
<br />
<b>Warning</b>: mysql_connect(): Can't connect to MySQL server on 'localhost' (10061) in <b>c:\program files\apache group\apache\htdocs\index.php</b> on line <b>2</b><br />
foo

ZibingsPrez

1:30 am on Jul 2, 2004 (gmt 0)

10+ Year Member



Uh...instead of using a die(), do something like this...


$string = "SELECT *....";

if (!$query = @mysql_query($string) )
{
header ( "Location: blah.php" );
exit;
}

[EDIT]
Btw, if you want to know why it's like this, die() is basically just an echo. It stops the page execution and then sends headers so that it can write the message you want to "die" with. Therefore, you're trying to change the headers after they've already been sent.

Purple Martin

1:41 am on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Doing it that way gives me this:

Warning: mysql_connect(): Can't connect to MySQL server on 'localhost' (10061) in c:\program files\apache group\apache\htdocs\index.php on line 3

ZibingsPrez

1:45 am on Jul 2, 2004 (gmt 0)

10+ Year Member



o_O


if (!@mysql_connect("localhost", "user", "pass") )
{
header ( "Location: redirect.php" );
exit;
}

You have it like that?

Purple Martin

4:47 am on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, doing it like that works!

But it doesn't give me a reference to the connection that I can use to close it later, so is it OK to connect twice like this?

<?php
// Test connection
if (!@mysql_connect("localhost", "user", "pass") )
{
header ( "Location: file.php" );
exit;
}
// Connect
$connection = mysql_connect("localhost", "user", "pass") or die();
?>

So that later on I can do this:

<?php
mysql_close($connection);
?>

RonPK

10:10 am on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mysql_connect() returns false on failure, so you could maybe use something like

$connection = @mysql_connect("localhost", "user", "pass");
if (!$connection) // redirect
else // carry on

Purple Martin

3:14 am on Jul 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the suggestion but that doesn't work either: I still get the same errors.

fmaz

6:46 pm on Jul 5, 2004 (gmt 0)

10+ Year Member



the $connection parameter is optional, if you only use 1 connection, it will be used as default in something like this:

$query = "SELECT test FROM test2 WHERE 1=1;";
$result = mysql_query($query);

^^^ This should work.

SO, you don't need to have your ressource #...

Howerver, try something like this:

if(!$connection=@mysql_connect(<...>)) {
die("header("location:<...>"));
}
$query = "<...>";
$result = mysql_query($query,$connection);

Hope this help.