Sukrit Jaie Sukrit Jaie - 3 years ago 115
PHP Question

logout/redirection issue in php?

I have a login page for my form where the user fills in their username and password. The user then gets redirected to lets say page 1, and the user can go to page 2 via a submit button on page 1. Both page 1 and page 2 also contain a logout button. I am able to log out from page 1, and if I'm not logged in, I cannot go to the page by typing its url in(empty session, redirect). I want to do both of those functions on page 2 but I keep getting a warning, that the header cannot be modified.

I have only been working on php for 2 months and this can be a very easy question for some of you so try not to waste both of our times telling me the same. I'd appreciate some real input though!

session_start();
// The button to go from page 1 to 2
<input type = "submit" id = "id2" name = "id2" Value = "View Call Log"/>
if(isset($_POST['id2'])){
echo "<script> window.open('page2.php', '_blank'); </script>";
}
// Everything below is in page 2.php as well.
if(isset($_POST['logout'])){
unset ($_SESSION['userid']);
header("Location: login.php");
}

if(empty($_SESSION['userid'])){
header("Location: login.php");
exit;
}


The same code works for page 1 but doesn't for page 2

Answer Source

Once you sent something to user you can't modify headers. In your code you are sending html and after that you try to send headers. You can't even send single space like in that example:

 <?php /* there is a space before <?php */ ?>

Move your php code up, before html.

PS. You would know that if you tried to google "headers already sent".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download