0

The purpose of this code, is to disconnect from a database and then redirect to a new permanent address:

mysql_close($con);
header("HTTP/1.1 301 Moved Permanently");
header('Location: http://www.example.com/');
exit();

Instead of redirecting, though, it just displays a blank page.

However, if I move the line mysql_close($con); down under the header functions, the code does succeed in redirecting:

header( "HTTP/1.1 301 Moved Permanently" );
header('Location: http://www.example.com/');
mysql_close($con);
exit();
  1. Why does putting mysql_close($con); before the header functions prevent the redirect?
  2. Will mysql_close($con); still be executed in the 2nd arrangement?

UPDATE

To help those that come across this later. Here is the answer to my question.

Answer 1: The reason putting mysql_close($con); before the header functions prevented the redirect, was because the connection was already closed. And, instead of doing the most sensible thing (silently accepting this fact), PHP threw an error that prevented the redirect from happening.

Answer 2: Yes, the code after the redirect does get executed in the 2nd arrangement.

To close the connection only if its open, you can use is_resource:

if (is_resource($con))
{
    mysql_close($con);
}
header( "HTTP/1.1 301 Moved Permanently" );
header('Location: http://www.example.com/');
exit();

Lastly, here's a function that will close the connection without throwing an error if the connection is already closed:

function mysql_close_with_no_backtalk($con)
{
    if (is_resource($con))
    {
        mysql_close($con);
    }
}

So, using the function above, my edit looked like this:

mysql_close_with_no_backtalk($con);
header( "HTTP/1.1 301 Moved Permanently" );
header('Location: http://www.example.com/');
exit();
Lonnie Best
  • 9,936
  • 10
  • 57
  • 97
  • If the connection is already closed when `mysql_close($con);` is executed, would that throw an error, or would it silently say to itself "Well, that's already closed, but no harm done"? – Lonnie Best Aug 25 '22 at 20:45
  • 1
    If you really mean [mysql_close](https://www.php.net/mysql_close) and not [mysqli_close](https://www.php.net/mysqli_close), the answer is probably that it's giving you an error that the function no longer exists, since it was removed in PHP 7.0, which itself last received a security update 3.5 years ago. – IMSoP Aug 25 '22 at 20:55
  • 1
    Either way, the answer to "why is my PHP script returning a blank page" is nearly always "because you've got error display turned off", so you need to find your error logs, or *if the site is not visible to the public*, turn on `display_errors`. – IMSoP Aug 25 '22 at 20:57
  • 1
    `mysql_close()` may be causing an error message. If you produce output before `header()`, it fails with a "Headers already sent" warning. – Barmar Aug 25 '22 at 21:03
  • @IMSoP : It really is `mysql_close($con)`, because this is an old server that hasn't been updated in years. I know the security ramifications. I'm just trying to figure out what's going on with it as it is. – Lonnie Best Aug 25 '22 at 21:04
  • Perhaps, I need to write a function that checks if a connection is already closed, and this function would close the connection if it is open. – Lonnie Best Aug 25 '22 at 22:01
  • As I said earlier, your next step is to **find or configure your error logs**. Don't try to debug an error message you can't see, or waste your time fixing things based on guesswork. – IMSoP Aug 25 '22 at 22:38
  • @IMSoP I agree with your advise. See updates to question. – Lonnie Best Aug 25 '22 at 22:40
  • 1
    If you have found the error message, you should edit *that* into your question. If you then can't find any existing questions mentioning the same error message, you could ask for the question to be re-opened and post your answer in the Answer section, where it belongs. – IMSoP Aug 25 '22 at 22:46

0 Answers0