sulentho
asked on
cookie setting issue in php
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
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////// //////////
/////////////////////***** *********S IGN IN**************////////// //////////
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////
if ($action=="sign-in") {
if (session_is_registered("ui d")) {
?>
<script LANGUAGE="JavaScript">
<!--
location='account.php?acti on=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.classNam e='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=sign up1" class="text">Sign up Today</a> | <a href="account.php?action=p wdrecover" class="text">Forgot your Password?</a></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
<?
}
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// /////
/////////////////////***** *********C HECK 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_REFERE R'];
?>
<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.g if" 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
This is my code
//////////////////////////
/////////////////////*****
//////////////////////////
if ($action=="sign-in") {
if (session_is_registered("ui
?>
<script LANGUAGE="JavaScript">
<!--
location='account.php?acti
// -->
</script>
<?
} else { ?>
<form name='myform' action="account.php?action
<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.classNam
</tr>
<tr>
<td> </td>
<td align="left">Not a member? <a href="signup.php?page=sign
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
<?
}
}
//////////////////////////
/////////////////////*****
//////////////////////////
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_REFERE
?>
<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.g
<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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.