Avatar of sulentho
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

////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////**************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>&nbsp;</td>
<td><input type="checkbox" name="setcookie" value="setcookie" />Remember me unless I sign out</td>
</tr>
<tr>
<td>&nbsp;</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>&nbsp;</td>
<td align="left">Not a member? <a href="signup.php?page=signup1" class="text">Sign up Today</a>&nbsp;|&nbsp; <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
PHP

Avatar of undefined
Last Comment
BogoJoker
ASKER CERTIFIED SOLUTION
Avatar of BogoJoker
BogoJoker

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo