I have a Login script in which the login form, the script the authenticates the login and password and the landing page once the user is authenticated are all part of the same file. I have been managing these modules using sessions and they work perfect for me. But now I want to use a "remember me" cookie to remember the user that checks that option. But I am having troubles setting the cookie as I know I have to set the cookie before any header info is passed but cannot do it that way
This is my code
////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////**************SIGN IN**************////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
if ($action=="sign-in") {
if (session_is_registered("uid")) {
?>
<script LANGUAGE="JavaScript">
<!--
location='account.php?action=manage';
// -->
</script>
<?
} else { ?>
<form name='myform' action="account.php?action=check" method="post" onSubmit="return validateForm1(this)">
<table cellpadding=4 cellspacing=0 width="100%">
<tr>
<td width="28%" align="right">Username</td>
<td width="72%"> <input name="u_login" type="text" class="txtbox" size="30" />
</td>
</tr>
<tr>
<td align="right">Password</td>
<td><input name="u_pwd" type="password" class="txtbox" size="30" />
</td>
</tr>
<tr>
<td> </td>
<td><input type="checkbox" name="setcookie" value="setcookie" />Remember me unless I sign out</td>
</tr>
<tr>
<td> </td>
<td><input name="submit" type="submit" class="btn" onMouseOver="this.className='btn btnhov'" onMouseOut="this.className='btn'" value="Sign In" /></td>
</tr>
<tr>
<td> </td>
<td align="left">Not a member? <a href="signup.php?page=signup1" class="text">Sign up Today</a> | <a href="account.php?action=pwdrecover" class="text">Forgot your Password?</a></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
<?
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////**************CHECK LOGIN**************///////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
else if ($action=="check") {
$u_login=$_POST['u_login'];
$u_pwd=$_POST['u_pwd'];
$check=$_POST['setcookie'];
$sql = "SELECT u_id,u_login,u_pwd FROM user_info where u_login='".$u_login."' AND u_pwd='".$u_pwd."'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if ($count==1) {
for ($i=0; $i<$count; $i++) {
$row = mysql_fetch_array($result);
$uid=$row[0];
$u_login=$row[1];
$u_pwd=$row[2];
if($check) {
setcookie("uid", $uid, time() + 31536000); // THIS LINE IS NOT WORKING
} else {
session_register("uid");
}
$url=$_SERVER['HTTP_REFERER'];
?>
<script language="JavaScript">
<!--
location='<? echo $url; ?>';
// -->
</script>
<?
}
} else { ?>
<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="6%" align="center" bgcolor="#FFFFFF"><img src="images/warning_icon.gif" width="35" height="32"></td>
<td width="94%" bgcolor="#FFFFFF" class="system_msg" align="left">Invalid username and password.</td>
</tr>
</table>
</td>
</tr>
</table>
<?
}
}
Any help will be appreciated. Thanks
Sulen