PHP Login Script With Remember me.

21
242512

welcome back to shortlearner.com , In this tutorial we will see how to make a login form with remember me feature. In this example we are using PHP cookies for preserving user login and password.
PHP Login Form
This code shows login form with PHP code to pre-populate user login and passsword. this form has a remember me check box. if it is set then the login details entered by the user will preserved for future login attempts.

login with remember me option in php

Index.php

Also Read :
PHP Login Script With Remember me.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

<?php
session_start();
if(!empty($_POST["login"])) {
	$conn = mysqli_connect("localhost", "root", "psw", "dbname");
	$sql = "Select * from user where email = '" . $_POST["email"] . "' and password = '" . ($_POST["password"]) . "'";
	$result = mysqli_query($conn,$sql);
	$user = mysqli_fetch_array($result);
	if($user) {
			$_SESSION["id"]		   = $user["id"];
			
			if(!empty($_POST["remember"])) {
				setcookie ("member_login",$_POST["email"],time()+ (10 * 365 * 24 * 60 * 60));
				setcookie ("password",$_POST["password"],time()+ (10 * 365 * 24 * 60 * 60));
			} else {
				if(isset($_COOKIE["member_login"])) {
					setcookie ("member_login","");
				}
				if(isset($_COOKIE["password"])) {
					setcookie ("password","");
				}
			}
	} else {
		$message = "Invalid Login";
	}
}
?>	
<style>
	#frmLogin {
		padding: 20px 60px;
		background: #B6E0FF;
		color: #555;
		display: inline-block;		
		border-radius: 4px;
	}
	.field-group {
		margin-top:15px;
	}
	.input-field {
		padding: 8px;
		width: 200px;
		border: #A3C3E7 1px solid;
		border-radius: 4px;
	}
	.form-submit-button {
		background: #65C370;
		border: 0;
		padding: 8px 20px;
		border-radius: 4px;
		color: #FFF;
		text-transform: uppercase;
	}
	.member-dashboard {
		padding: 40px;
		background: #D2EDD5;
		color: #555;
		border-radius: 4px;
		display: inline-block;
	}
	.member-dashboard a {
		color: #09F;
		text-decoration:none;
	}
	.error-message {
		text-align:center;
		color:#FF0000;
	}
</style>

<?php if(empty($_SESSION["id"])) { ?>
<form action="" method="post" id="frmLogin">
	<div class="error-message"><?php if(isset($message)) { echo $message; } ?></div>	
	<div class="field-group">
		<div><label for="login">Username</label></div>
		<div><input name="email" type="text" value="<?php if(isset($_COOKIE["member_login"])) { echo $_COOKIE["member_login"]; } ?>" class="input-field">
	</div>
	<div class="field-group">
		<div><label for="password">Password</label></div>
		<div><input name="password" type="password" value="<?php if(isset($_COOKIE["password"])) { echo $_COOKIE["password"]; } ?>" class="input-field"> 
	</div>
	<div class="field-group">
		<div><input type="checkbox" name="remember" id="remember" <?php if(isset($_COOKIE["member_login"])) { ?> checked <?php } ?> />
		<label for="remember-me">Remember me</label>
	</div>
	<div class="field-group">
		<div><input type="submit" name="login" value="Login" class="form-submit-button"></span></div>
	</div>       
</form>
<?php } else { ?>
<div class="member-dashboard">You have Successfully logged in!. <a href="logout.php">Logout</a></div>
<?php } ?>

Logout.php

<?php
session_start();
$_SESSION["member_id"] = "";
session_destroy();
header("Location: ./");
?>

21 COMMENTS

  1. I was curious if you ever considered changing the structure of your blog?
    Its very well written; I love what youve got to
    say. But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having 1 or 2
    images. Maybe you could space it out better?

  2. I loved as much as you will receive carried out
    right here. The sketch is attractive, your authored material stylish.
    nonetheless, you command get bought an impatience over that you wish be delivering the following.

    unwell unquestionably come more formerly again as
    exactly the same nearly a lot often inside case you shield this increase.

  3. This is a great tip especially to those fresh to the blogosphere.
    Simple but very precise information… Appreciate your sharing this one.

    A must read post!

  4. Definitely imagine that which you stated. Your favorite reason appeared
    to be at the net the simplest thing to have in mind of.
    I say to you, I certainly get irked whilst other folks think about worries that they just don’t recognize about.

    You managed to hit the nail upon the highest as neatly as defined out the whole thing without having side-effects , other folks could take
    a signal. Will probably be again to get more.

    Thanks

  5. We are a group of volunteers and starting a new scheme in our community.
    Your web site provided us with valuable information to work on. You have done
    a formidable job and our whole community will be thankful to you.

  6. I’m truly enjoying the design and layout of your website.
    It’s a very easy on the eyes which makes it much more
    pleasant for me to come here and visit more often. Did you hire out a developer to create your theme?
    Great work!

  7. Fitur game sloot ini adalah free spin lebih banyak
    dengan RTP 98%. Kemenangan maksimal game slot Koi Gaate
    ini mencapai 8640x nominal taruhan. Jauh ke kolong,
    merekma sediahya berharap untuk longgar, akan tetapi uang
    yang mereka tukar untuk berperan adalah menarik untuk pesta.
    Jika Anda mengidamkan memainkan Slot apa pun yang Anda rasa, Anda tentu
    harus ikuti perasaan itu! How the slot machine works?
    In the event that a slot just idea about its edge, it will in the long run lose the trust of its supporters.
    Other slots from Another Games software sucxh as Animals Steam, Atomic Mushrooms and Necronomicon support identical online video slot play.

    These games are more exciting than less-volatile games,
    but they also don’t pay as frequently. Some online delivery slots
    aree booked upp to tbree weeks in advance. Given this remarkable advance in acceptance by a European country, the Jewish population in the Netherlands continued tto expand and confidently regard itself as partt
    of the Dutch nation. As the Caldari did not share this enthusiasm for diverrsity with the Gallente, the
    Calfari State at the time oof its formation found itself at a
    relative population deficit comared to its Gallente adversary;
    rather than encourage massive immigration to and diversity wiyhin the State, this population shortage was rectified by a Statewide programme off artificial reproduction, producing a generationn of so-called ‘Tube Children’ rraised by
    thee Caldari State apparatus to enlarge the labour pools available too the megacorporations thqt ruled the State.

    Online casino play real money – RAJAWIN

  8. I’m not sure exactly why but this website is loading extremely slow for
    me. Is anyone else having this problem or is it a issue on my end?
    I’ll check back later and see if the problem still exists.

LEAVE A REPLY

Please enter your comment!
Please enter your name here