Viewed   78 times

After calling the redirect function header, should I call exit or not?

<?php // fileA
$urlFailToGoTo = '/formerror.php';

if (sth)
{
   header(sprintf("Location: %s", $urlFailToGoTo));
   exit(); //should I call exit() here? or return?
}

?>

Thank you

 Answers

4

You definitely should. Otherwise the script execution is not terminated. Setting another header alone is not enough to redirect.

Friday, September 23, 2022
5

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

[email protected]:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022
5

The general principle is you cannot use echo before header(). So, this will never work:

echo "this is my header";
header("Location: account.php");
echo "this is my footer";

However, if you sent the headers first, everything works fine:

header("Location: account.php");
echo "this is my header";
echo "this is my footer";

In your case, you should do the check before you include the header:

require_once('models/init.php');  // db connection and other functions

if ($user_is_logged_in) { // Do your check here
    header("Location: account.php");
}

include('header.php');  // some html code for the header, with one line php-function to check if user is logged in, if so show "home" tab instead of "login"
echo "<form>" // display the login form
include("footer"); // including footer, some html/js code.
Thursday, November 24, 2022
 
onur
 
4

could the code after the header-location call be effectively executed?

Yes, always. The header is only a line of data asking the browser to redirect. The rest of the page will still be served by PHP and can be looked at by the client by simply preventing the header command from executing.

That is easy enough to do with a command-line client like wget, for example, by simply telling it not to follow redirects.

Bottom line: If you don't prevent it, PHP will send out the whole body even after a header call. That body is fully available to the recipient without any special hacking skills.

Wednesday, December 7, 2022
4

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :