Remember me function

How to login with remember me function in php

Source code

config.php

								
									<?php
									define('DB_SERVER','localhost');

									define('DB_USER','root');

									define('DB_PASS' ,'');

									define('DB_NAME', 'remdb');

									$conn = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);

									// Check connection

									if (mysqli_connect_errno())

									{
										echo "Failed to connect to MySQL: " . mysqli_connect_error();

									}

									?>
								
							

index.php

								
									<?php
									error_reporting(0);
									session_start();

									include('config.php');

									if(isset($_POST["login"])) 

									{
										$username=$_POST['username'];

										$password=md5($_POST['password']);

										$sql = "Select * from tbluser where userName ='$username' and userPassword ='$password'";

										$result = mysqli_query($conn,$sql);

										$row = mysqli_fetch_array($result);

										if($row) {

											$_SESSION["userid"]= $row["id"];
											if(!empty($_POST["remember"])) {

												setcookie ("user_login",$_POST["username"],time()+ (10 * 365 * 24 * 60 * 60));

												setcookie ("userpassword",$_POST["password"],time()+ (10 * 365 * 24 * 60 * 60));

											} else {

												if(isset($_COOKIE["user_login"])) {
													setcookie ("user_login","");
												}
												if(isset($_COOKIE["userpassword"])) {
													setcookie ("userpassword","");
												}
											}

											header('location:welcome.php');

										} else {
											echo "<script>alert('Invalid Username or Password');</script>";
										}
									}

									?>

									<!DOCTYPE html>

									<html>

									<head>
										<title>PHP Login with remember me Function</title>
										<!--Bootstrap -->
										<link rel="stylesheet" href="css/bootstrap.css" type="text/css">
										<meta name="viewport" content="width=device-width, initial-scale=1">
									</head>
									<body>
										<div class="container" >
											<form action="" method="post" id="login">
												<h4> PHP login with remember me function</h4>
												<p>Username(admin), Password(1234)</p>
												<div class="row ">
													<div class="form-group col-md-6">
														<div>
															<label for="login">Username</label>
														</div>
														<div>
															<input name="username" class="form-control" type="text" value="<?php if(isset($_COOKIE["user_login"])) { echo $_COOKIE["user_login"]; } ?>" class="input-field">

														</div>
													</div >
												</div>
												<div class="row">
													<div class="form-group col-md-6">
														<div>
															<label for="password">Password</label>
														</div>
														<div>
															<input name="password" class="form-control" type="password" value="<?php if(isset($_COOKIE["userpassword"])) { echo $_COOKIE["userpassword"]; } ?>" class="input-field">
														</div>
													</div>
												</div>
												<div class="row">
													<div class="form-group col-md-6">
														<div>
															<input type="checkbox" name="remember" id="remember" <?php if(isset($_COOKIE["user_login"])) { ?> checked <?php } ?> />
															<label for="remember-me">Remember me</label>
														</div>
														<div class="form-group">
															<div>
																<input type="submit" name="login"  value="Login" class="btn btn-primary"></span>
															</div>
														</div>       
													</div>
												</div>
											</form>
										</div>
									</body>
									</html>
								
							

welcome.php

								
									<?php
									session_start();
									if(!isset($_SESSION["userid"])){
										?>
										<script>
											window.location.href='index.php';
										</script>
										<?php
									}else{
										?>
										<!DOCTYPE html>
										<html lang="en">
										<head>
											<meta charset="utf-8">
											<meta name="robots" content="noindex, nofollow">
											<title>Login Form</title>
											<meta name="viewport" content="width=device-width, initial-scale=1">
											<link href="css/bootstrap.css" rel='stylesheet' type='text/css' />

										</head>
										<body>
											<div class="container">
												<div> </div>
												<div class="  d-flex flex-row align-items-center justify-content-between">
													<div class="">
														<a href="logout.php"><button type="button" class="btn btn-sm btn-primary"><i class="fas fa-plus" ></i>Logout
														</button></a>
													</div>
												</div>
												<div> </div>
												<div class="row ">
													<table id="" class="table table-bordered">
														<thead>
															<tr>
																<th class="text-center">No</th>
																<th>Username</th>
																<th>Password</th>
															</tr>
														</thead>
														<tbody>
															<tr>
																<td class="text-center">1</td>
																<td>admin</td>
																<td class="text-center">1234</td>
															</tr>
														</tbody>
													</table>
												</div>
											</div>
										</body>
										<?php
									}
									?>
								
							
Download

Comments

Andrea Emily

Nice code thank you code4berry, how can I donate

Reagan Ontonia

Keep up with that good heart of teaching world

Leave a Comment:

You Might Also Like

hostel booking

Hostel booking management system
Learn More
.

car rental

Car rental management system in Php
Learn More
.

student details

Student details management system
Learn More
.

Tourism

Tourism management system in Php and Mysql
Learn More

Latest Tutorial

sweet alerts

How to delete table row using sweet alert2
Learn More

piechart

How to create piechart with Mysql data
Learn More

register and login

How to register and login in php
Learn More

edit data

How to edit Mysql data in modal using php
Learn More