Jump to content

Recommended Posts

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.