PDA

View Full Version : "Remember Me" option on login page?


FethrdWlf
06-17-2011, 05:09 AM
I'm currently piecing together a blog - http://web3.x10.mx - that allows users to sign up and login. Right now, you can sign up as much as you want. Then, you can login too. Well, by logging in, all you do is enter your email or username and password, and it tells you who you logged in as. I also have a "Remember Me" checkbox below the textboxes, and that's where my problem is. I don't know how to start making it work to where it will remember the username (only the username, I don't wanna' be responsible if passwords get figured out). I tried setting cookies if the box was checked, but that didn't work.

In short, how do I make the checkbox actually store the users' username, and then populate the textbox with that username?;-)

Corrosive
06-17-2011, 06:49 AM
I think that cookies is the way to do it. What have you tried so far?

FethrdWlf
06-17-2011, 02:28 PM
Well, I have a login script. Inside that script, I have an if statement finding whether or not there was an error. Otherwise, continue. In the }else{ part of it all, I have another if statement. If the value of my checkbox equals yes (I set the checked value to "yes") then I want to set a cookie.

THEN, back to my login page, I have an if statement finding whether or not the cookie has been set. If so, echo the value of it within the textbox value.
:-P

loginscript.php
[<?php
if($remember == "yes"){
$expire = time()+60*60*24*365;
setcookie("remuser",$entlogin,$expire);
}
session_start();
$_SESSION['accessthepage']=1;
include("mysql.php");

$entlogin = $_POST['username'];
$entpass = $_POST['password'];
$remember = $_POST['remember'];

$query = "SELECT * FROM users WHERE password = '$entpass' AND username = '$entlogin' OR email = '$entlogin'";
$results = mysqli_query($cnx,$query);
$loginrows = mysqli_num_rows($results);

if(!$loginrows > 0){
$_SESSION['wronglogin']="• The email/username and password combination you entered does not exist.";
$_SESSION['loginerror']=1;
header("Location: ../wrong/error.php");
}else{

$expire = time()+60*60*24*365;
if($remember = "yes"){
setcookie("remuser",$entlogin,$expire);
}
while($row = mysqli_fetch_assoc($results)){
$_SESSION['loggedin']=1;
$_SESSION['username']=$row['username'];
header("Location: ../index.php");
}
}
?>
Login form:
<?php session_start(); ?>...<form action="../scripts/login.php" method="post" enctype="multipart/form-data" name="signup" id="signup">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="28%" style="text-align:right">&nbsp;</td>
<td width="46%"><span style="font-size:12px"><em>(All fields are required...)</em></span></td>
<td width="26%">&nbsp;</td>
</tr>
<tr>
<td height="30" style="text-align:right"><label for="username">Username or Email:</label></td>
<td><input name="username" type="text" id="username" maxlength="42" value="<?php if(isset($_COOKIE['remuser'])){
echo $_COOKIE['remuser'];
} ?>" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="30" style="text-align:right"><label for="password">Password:</label></td>
<td><input name="password" type="password" id="password" maxlength="15" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td style="text-align:right"><label for="remember" title="NOTE: Only saves your username!">Remember My Info:</label></td>
<td><input name="remember" type="checkbox" id="remember" value="yes" title="NOTE: Only saves your username!" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td style="text-align:right">&nbsp;</td>
<td><label for="submit"></label>
<input name="submit" type="submit" id="submit" onclick="MM_validateForm('username','','R','password','','R ');return document.MM_returnValue" value="Login" /></td>
<td>&nbsp;</td>
</tr>
</table>...

Thanks for any help guys!