login in PHP using Mysql Tutorial makcode.in

 

Today i will share Simple Login Script in PHP and MySQLi, i will explain the basic of login . In this tutorial user can login to user database.

In the tutorial we login form user database from MySQL.

In this example we used 2 file for retrieve data

  • database.php– To connecting database.
  • login.php– For getting access from database for login

Database.php

<?php
$servername='localhost';
$username='root';
$password='';
$dbname = "user_details";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
if(!$conn){
 die('Could not Connect My Sql:' .mysql_error());
}
?>

login.php

<?php
session_start();
$message="";
if(count($_POST)>0) {
include_once 'database.php';
$result = mysqli_query($conn,"SELECT * FROM user WHERE (userid='" . $_POST["userid"] . "') and password = '". $_POST["password"]."'");
$row = mysqli_fetch_array($result);
if(is_array($row)) {
$_SESSION["userid"] = $row[userid];
} else {
$message = "Invalid Userid or email or Password!";
}
}
if(isset($_SESSION["userid"])) {
echo "Success";
}
?>
<html>
<head>
<title>User Login</title>
</head>
<body>
<form method="post" action="" align="center">
<div class="message"><?php if($message!="") { echo $message; } ?></div>
<h3 align="center">Enter Login Details</h3>
Userid or mobile:<br>
<input type="text" name="userid">
<br>
Password:<br>
<input type="password" name="password">
<br><br>
<input type="submit" name="submit" value="Submit">
<input type="reset">
</form>
</body>
</html>