Hi guys,
I have created a login system where user can enter their username and password which when validated will take user to new page with his or her name displayed on the screen, now I want to create a "Remember me" feature in my login screen so that when user select remember me it will remember user name everytime he or she opens the browser and remained logged in until they click on logout. I know it can be accomplished by using Session and Cookies but I have no idea how and where should I use them in my code. Here is my code for your reference:
Login.php
Loginsucess.php
Please help me with this...I found couple of scripts on net but to use them I have to make lot of changes to my script and I would prefer if someone can please update my existing script.Thanks guys and also letting you know that I am using PHP Version 5.2.3.
I have created a login system where user can enter their username and password which when validated will take user to new page with his or her name displayed on the screen, now I want to create a "Remember me" feature in my login screen so that when user select remember me it will remember user name everytime he or she opens the browser and remained logged in until they click on logout. I know it can be accomplished by using Session and Cookies but I have no idea how and where should I use them in my code. Here is my code for your reference:
Login.php
Code:
<?php
session_start();
if (isset($_POST['submit'])){
include 'form-validation.php';
include 'connect.php';
$Name=addslashes($_POST['Name']);
$Username=addslashes($_POST['Username']);
$Password=md5($_POST['Password']);
$result = form_validation_validate($_POST, "
Username Password : empty;
Username Password : len >= 3;
Username Password : chnum_; ");
?>
<tr>
<td colspan="2">
<?php
if ($result === true){
$query = mysql_query("SELECT id FROM `login_tbl` WHERE `Username` = '$Username' AND `Password` = '$Password'");
list($user_id) = mysql_fetch_row($query);
if(empty($user_id)){
echo '<span style="color: #A71930"> No such login in the system. Please try again</span>';
}
else{
$_SESSION['user_id'] = $user_id;
header('location: loginsucess.php');
$_POST = Array();
}
}
else echo '<span style="color: #A71930">' . $result . '</span>';
}
?>
</td></tr>
<tr>
<td style="padding-bottom: 10px;" colspan="2" class="heading1"><b>Login Required</b></td>
</tr>
<tr>
<td height="30">Username:</td>
<td><input type="text" name="Username" style="width:15em;" value="<?php echo $_POST['Username']; ?>">
</td>
</tr>
<tr>
<td height="30">Password:</td>
<td><input type="password" name="Password" style="width:15em;" value="<?php echo $_POST['Password'];?>">
</td>
</tr>
<tr><td> </td>
<td><input name="submit" type="submit" value="Log-in" class="submit">
<input type="checkbox" name="remember" /><span style="color:#006f99;">Remember me</span><br /><br />
<a href="register.php">Register</a> | <a href="resetpassword.php">Forgot your password?</a></td>
</tr></table></form>
Code:
<?php
session_start();
include_once 'connect.php';
if(isset($_SESSION['user_id'])) {
$query = mysql_query("SELECT Name FROM login_tbl
WHERE ID = " . $_SESSION['user_id'] . " LIMIT 1")
or die(mysql_error());
list($Name) = mysql_fetch_row($query);
echo '<span class="username">Hello, '. $Name . '! <a href="logout.php" style="color:#FFFFFF">( Logout )</a></span>';
} else {
echo 'Please login before opening the user panel.';
}
?>
Comment