07-12-2009, 02:08 PM
|
#1 (permalink)
|
|
The Visitor
Join Date: Jul 2009
Posts: 2
Thanks: 0
|
How to add remember me feature ?
Hello,
I made this register/login script and all dya ive been trying to add a remember me feature but its not working out.
If someone is bothered to modify this script so it has a working remember me feature i would be grateful.
I have modified the login script that worked as follows (it still works for simply logging in but the user doesnt stay logged in if he checks the checkbox):
Code:
<?php
session_start();
if (isset($_POST['submit'])) {
require_once('db_connect.php');
if (empty($_POST['email'])) {
echo "didnt enter EMAKL<br />";
} else {
$e = trim($_POST['email']);
}
if (empty($_POST['pass'])) {
echo "Didnt enter PASS<br />";
}
else {
$p = trim($_POST['pass']);
}
if ($e && $p) {
$q = "SELECT user_id, first_name, user_level FROM users WHERE email='$e' AND pass=SHA1('$p')";
$r = mysqli_query($dbc, $q) or trigger_error("Couldnt execute query 1");
if (mysqli_num_rows($r) == 1) {
$_SESSION = mysqli_fetch_array($r);
if(isset($_POST['remember'])) {
setcookie("cookid", $_SESSION['user_id'], time()+60*60*24*100);
setcookie("cookname", $_SESSION['first_name'], time()+60*60*24*100);
setcookie("cooklevel", $_SESSION['user_level'], time()+60*60*24*100);
header("Location: index.php");
}
}
else {
echo "Youre not in hte DB<br />";
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style2 {color: #333333}
.style3 {color: #ECE9D8}
-->
</style>
</head>
<body>
<p><a href="register.php">Register</a> <a href="login.php">Login</a> <a href="password.php">Change Password</a> <a href="view_users.php">View Users</a> <a href="private.php">private</a></p>
<h1>Login</h1>
<form action="login.php" method="post">
<input type="text" name="email" />
<input type="text" name="pass" />
<input type="submit" name="submit" value="Login" />
<label>
<input type="checkbox" name="remember" id="remember" />
Remember me:</label>
</form>
</body>
</html>
Also this is the code i use to check if the user is logged in on my index.php page:
Code:
<?php
session_start();
if (isset($_SESSION['first_name'])) {
echo "Hello man<br />";
echo "<a href=\"logout.php\">logout</a>";
}
?>
Please tell me if i should modify this also because of the remember me feature i want to add.
Thanks in advance
|
|
|
|