johnmark Posted September 9, 2007 Share Posted September 9, 2007 I have a problem with my code, when I put in the correct username and password I get the error... Warning: Cannot modify header information - headers already sent by (output started at...) in ... on line 13 this is the code i'm using... <html> <body> <?php // valid login credentials $username = 'admin'; $password = 'admin_pass'; // grab current time $time=time(); // handle validation event if ($_POST[user] && $_POST[pass]) { if ($_POST[user]==$username && $_POST[pass]==$password) { setcookie ("user", md5($_POST[user]), $time+3200); setcookie ("pass", md5($_POST[pass]), $time+3200); header("Location: index.php"); } else { $login_error= true; } } // handle the logout event if ($logout == true) { setcookie ("user", md5($_POST[user]), $time-3200); setcookie ("pass", md5($_POST[pass]), $time-3200); header("Location: index.php"); } // handle login event, both successful and erroneous, or show login screen if ($login_error == true) { ?> <table align=center style="font-family:arial; font-size:12; border:1 solid #000000;"> <tr><td align=center bgcolor=#123dd4>LOGIN ERROR</td></tr> <tr><td align=center><b>Invalid Username and/or Password</b><br><br><a href=index.php>Back</a></td></tr> </table> <? } elseif ($_COOKIE[user] == md5($username) && $_COOKIE[pass] == md5($password)) { ?> <table align=center style="font-family:arial; font-size:12; border:1 solid #000000;"> <tr><td align=center bgcolor=#123dd4>SECURE AREA</td></tr> <tr><td align=right><a href=index.php?logout=true>Logout</a></td></tr> <tr><td>You have successfully logged in.<br><br> Encrypted Username: <b><?php $_COOKIE[user] ?></b><br> Encrypted Password: <b><?php $_COOKIE[pass] ?></b><br> </td></tr> </table> <? } else { ?> <form action=index.php method=post> <table align=center style="font-family:arial; font-size:12; border:1 solid #000000;"> <tr><td colspan=2 align=center bgcolor=#123dd4>LOGIN</td></tr> <tr><td align=right>Username: </td><td><input type=text name=user size=15></td></tr> <tr><td align=right>Password: </td><td><input type=password name=pass size=15></td></tr> <tr><td align=center colspan=2><input type=submit value=Login></td></tr> </table> </form> <? } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/68565-solved-secure-login/ Share on other sites More sharing options...
recklessgeneral Posted September 9, 2007 Share Posted September 9, 2007 Hi, A simple fix is to remove the first 2 lines (<html> and <body>) as these will already have been sent by the server before the login checking is carried out. When you then try to send the header info, the headers, with the 2 html tags have already been sent. Cheers, Darren. Link to comment https://forums.phpfreaks.com/topic/68565-solved-secure-login/#findComment-344648 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.