article

Thursday, February 24, 2022

PHP Mysqli Simple Login Session with Validation

PHP Mysqli Simple Login Session with Validation

CREATE TABLE `userlogin` (
  `userid` int(11) NOT NULL,
  `username` varchar(30) NOT NULL,
  `password` varchar(30) NOT NULL,
  `fullname` varchar(60) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `userlogin` (`userid`, `username`, `password`, `fullname`) VALUES
(1, 'cairocoders', '123456', 'Cairocoders Ednalan');

ALTER TABLE `userlogin`
  ADD PRIMARY KEY (`userid`);

ALTER TABLE `userlogin`
  MODIFY `userid` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

index.php
//index.php
<!DOCTYPE html>
<html>
<head>
<title>PHP Mysqli Simple Login Session with Validation</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />  
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.message {color: #FF0000;}
</style>
</head>
<body>
<?php
$Message = $ErrorUname = $ErrorPass = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  
    $username = check_input($_POST["username"]);
	
    if (!preg_match("/^[a-zA-Z0-9_]*$/",$username)) {
      $ErrorUname = "Space and special characters not allowed but you can use underscore(_)."; 
    }
	else{
		$fusername=$username;
	}
 
	$fpassword = check_input($_POST["password"]);

  if ($ErrorUname!=""){
	$Message = "Login failed! Errors found";
  }
  else{
  include('conn.php');
  
  $query=mysqli_query($conn,"select * from userlogin where username='$fusername' && password='$fpassword'");
  $num_rows=mysqli_num_rows($query);
  $row=mysqli_fetch_array($query);
  
  if ($num_rows>0){
	  $Message = "Login Successful!";
  }
  else{
	$Message = "Login Failed! User not found";
  }
  
  }
}

function check_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
<section class="login">
<div class="container">
	<div class="row justify-content-center">
		<div class="col-md-6 text-center mb-5">
			<h2 class="heading-section">PHP Mysqli Simple Login Session with Validation</h2>
		</div>
	</div>
	<div class="row justify-content-center">
		<div class="col-md-7 col-lg-5">
			<div class="p-4 p-md-5">
			<div class="d-flex">
				<div class="w-100">
				<h3 class="mb-4">Sign In</h3>
				</div>
				<div class="w-100">
				<p class="justify-content-end">
				<a href="#" class="align-items-center justify-content-center"><span class="fa fa-facebook"></span></a>
				<a href="#" class="align-items-center justify-content-center"><span class="fa fa-twitter"></span></a>
				</p>
				</div>
			</div>
			<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
				<p><span class="message">* required field.</span></p>
				<div class="form-group mt-3">
					<input type="text" name="username" class="form-control" required>
					<label class="form-control-placeholder" for="username">Username</label>
					<span class="message">* <?php echo $ErrorUname;?></span>
				</div>
				<div class="form-group">
					<input type="password" name="password" class="form-control" required>
					<label class="form-control-placeholder" for="password">Password</label>
					<span class="message">* <?php echo $ErrorPass;?></span>
					<span class="fa fa-fw fa-eye"></span>
				</div>
				<div class="form-group">
					<button type="submit" class="form-control btn btn-primary rounded submit px-3">Sign In</button>
				</div>
				<span class="message">
				<?php
					if ($Message=="Login Successful!"){
						echo $Message;
						echo 'Welcome, '.$row['fullname'];
					}
					else{
						echo $Message;
					}

				?>
				</span>
				<div class="form-group d-md-flex">
					<div class="w-50 text-left">
					<label class="checkbox-primary mb-0">Remember Me
					<input type="checkbox" checked>
					<span class="checkmark"></span>
					</label>
					</div>
					<div class="w-50 text-md-right">
					<a href="#">Forgot Password</a>
					</div>
				</div>
			</form>
			<p class="text-center">Not a member? <a data-toggle="tab" href="#signup">Sign Up</a></p>
			</div>
		</div>
	</div>
</div>
</section>
</body>
</html>
conn.php
//conn.php
<?php
$conn = mysqli_connect("localhost","root","","testingdb");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

Related Post