Grabbing Session variables and header redirects
| From: | dwalker | Date: | Wed, 16 Apr 2003 07:36:44 +0000 |
| Subject: | Grabbing Session variables and header redirects | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-144082@lists.php.net to get a copy of this message | ||
I was able to get my login page to 'properly' authenticate my users. However, I do have
one resulting
problem with giving other pages the ability to check for an 'authentication'
variable AND then redirect with a "header('Location:
http://www.someplaceelse.com/dir1/nextpage.php?avariable=astatus');"
I receive the following warning message:
Warning: Cannot add header information - headers already sent by (output
started at /export/.../here_is_your_info.php:9) in /export/.../here_is_your_info.php on line
13
This is my code in page 1 (the login page)Name: login.php
(after truly authenticating the user from information contained within the database)...
<?php
$redirectLoginSuccess="http://www.mysite.com/here_is_your_info.php";
$redirectLoginFailed="http://www.mysite.com/login.php";
?>
<?php
note://user gets the green light
IF user gets the green light....
...
session_register("mysecurityvariable");
$login_failed = false;
$_SESSION["mysecurityvariable"] =$login_failed;
header ("Location: $redirectLoginSuccess");
exit;
.......
......
note://user gets the red light
session_register("mysecurityvariable");
$login_failed = true;
$_SESSION["mysecurityvariable"] =$login_failed;
header ("Location: $redirectLoginFailed");
exit;
....
?>
My code reads in page 2 (page needing to test for authentication):Name: here_is_your_info.php
<?php
note:// redirect to login if invalid user authorization
session_start(); note:// causes problems with session_cache_limiter
if ((!isset($_SESSION['mysecurityvariable'])) ||
($_SESSION['mysecurityvariable'] != false )){
header( 'Location: http://www.mysite.com/login.php' );
}
?>
Ultimately, it looks as though it's complaining about having already
received a redirection instruction, even though another page initiated it.
I have several other pages that will have a need to check for authentication
and, initially, I thought I'd try to incorporate this authentication and
redirection code into my top nav bar which happens to be INCLUDED on every
page. I changed the code to see to if I could generate different results.
NOTE: The code for page 2 works perfectly within one other page on the site and the code is
IDENTICAL.
Any suggestions would be appreciative.