Hey Guys,
I want to add a remember me function to my site and I'm having a little trouble..
I set it up so if the checkbox is checked it will give a value of "yes". Here is the code:
Login Bar:
<div id="login"><b>Login:</b><br />
<form id="loginform" name="login" method="post" action="scripts/login.php">
<label>Username:
<input type="text" name="username" id="username" />
</label>
<label>Password:
<input type="password" name="pass" id="pass" />
</label><input name="loginquery" type="submit" />
<label>
<span class="arial10pxgrey">Remember</span> <input type="checkbox" name="remember" id="remember" value="yes" />
</label>
|| <a href="/register.php">Register</a> || <a href="http://www.wowevocation.com/forgot_pass.php">Forgot Password</a> ' . $error_msg . '
</form>
</div>'
Login Script:
<?php
if ($_POST['username'] != "") {
include_once "connect_to_mysql.php";
$username = $_POST['username'];
$pass = $_POST['pass'];
$remember = $_POST['remember']; // Added for the remember me feature
$username = strip_tags($username);
$pass = strip_tags($pass);
$username = mysqli_real_escape_string($myConnection,$username);
$pass = mysqli_real_escape_string($myConnection,$pass);
$username = eregi_replace("`", "", $username);
$pass = eregi_replace("`", "", $pass);
$pass = md5($pass);
//make query
$sql = mysqli_query($myConnection,"SELECT * FROM members WHERE username='$username' AND password='$pass' AND email_activated='1'");
$login_check = mysqli_num_rows($sql);
if($login_check > 0){
while($row = mysqli_fetch_array($sql)){
$id = $row["id"];
session_register('id');
$_SESSION['id'] = $id;
$type = $row["account_type"];
session_register('type');
$_SESSION['type'] = $type;
$username = $row["username"];
session_register('username');
$_SESSION['username'] = $username;
mysqli_query($myConnection,"UPDATE members SET lastlogged=now() WHERE id='$id'");
header( 'Location: http://www.wowevocation.com/profile.php' ) ;
exit();
} // close while
// Remember Me Section Addition... if member has chosen to be remembered in the system
if($remember == "yes"){
setcookie("idCookie", $id, time()+60*24*60*60, "/"); // 60 days; 24 hours; 60 mins; 60secs
setcookie("firstnameCookie", $firstname, time()+60*24*60*60, "/"); // 60 days; 24 hours; 60 mins; 60secs
setcookie("emailCookie", $email, time()+60*24*60*60, "/"); // 60 days; 24 hours; 60 mins; 60secs
setcookie("passCookie", $pass, time()+60*24*60*60, "/"); // 60 days; 24 hours; 60 mins; 60sec
echo 'IT WORKED';
} else {
header( 'Location: http://www.wowevocation.com/index.php' ) ;
exit();
}
}
}// close if post
?>
Logout Script:
<?php
session_start();
// Destroy sessions and cookies
if ($_POST['post_code'] == "log_out") {
// Unset all of the session variables.
$_SESSION = array();
// Added for the Remember Me feature, to set the cookies to a time in the past
if (isset($_COOKIE['idCookie'])) {
setcookie("idCookie", '', time()-42000, '/');
setcookie("firstnameCookie", '', time()-42000, '/');
setcookie("emailCookie", '', time()-42000, '/');
setcookie("passCookie", '', time()-42000, '/');
}
// Note: This will destroy the sessions, and not just the session variable data that the sessions hold
session_destroy();
}
if(!session_is_registered('id')){
print "replyMsg=success";
exit();
} else {
print "replyMsg=failure";
exit();
}
?>