Re: Redirecting in a PHP script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



As other people have already said, use the header() function. However, be sure to include exit; after you call it. I've run into problems where it did not redirect properly if I didn't have the exit call. This also prevents anymore of the page loading (and maybe providing sensitive information that you don't want others to see).

Also use output buffering if you have already displayed HTML. So, an example page:

--------------------------------------
<? session_start(); ob_start(); ?>
<html>
<head><title>Hi</title></head>
<body>
  <h1>Hi</h1>
<?
if (!$_SESSION["logged_in"]) {
  header ("location: login.php?redirect=hi.php");
  exit;
}
?>
...
</body>
</html>
--------------------------------------

~Philip


On Mar 12, 2007, at 3:02 PM, Larry Bradley wrote:

I need to "goto" different PHP pages in my web site depending on what happens within some PHP code.

For example, if the user is not logged in when he goes to a page, I want to send him to a LOGIN page.

I've have everything working fine, using the following Javascript code:
            $location = 'login.php';
                echo "<script language='javascript'>\n";
echo "document.location.href = '" . $location . "'; \n";
                echo "</script>\n";

I also played around with using the header("location: ...") function.

I understand that the header() function must be issued before any HMTL is output.

But I'm not sure about the Javascript code. In every instance in my code, I use the Javascript before any HTML - this type of action normally occurs in PHP code called via a form POST.

I presume that the Javascript code really does the same as the PHP stuff, and thus must obey the same rules, but I'm not sure.
Comments?

Larry Bradley
Orleans (Ottawa), Ontario, CANADA

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux