Login PHP Script with MYSQL Database and Validation

This is php development tutorial a simple login program using a MYSQL database and HTML5 Validation. Here i have write a code by file name easily you can understand after login give a authorization  to the user you can assess the page if password or email is valid.you can use this code to check the authorization for the users.

logn in php 2 harshit.info

Database Details

CREATE TABLE `login` (
  `id` int(11) NOT NULL,
  `email` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- Dumping data for table `login`
INSERT INTO `login` (`id`, `email`, `password`) VALUES
(1, 'admin@gmail.com', 'admin123');

-- Indexes for dumped tables
-- Indexes for table `login`
ALTER TABLE `login`
  ADD PRIMARY KEY (`id`);

-- AUTO_INCREMENT for dumped tables
-- AUTO_INCREMENT for table `login`
--
ALTER TABLE `login`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

index.php

<?php 

session_start(); // session start here

if (isset($_POST['submit']))
{
	extract($_POST);  // Extract the posted data $email $password

    $db = new mysqli("localhost", "root","", "test"); // Enter your databse details hostname, useer, password , dbname

     	if(mysqli_connect_errno())
		{
    		echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
		}   

	    $query = $db->query("SELECT email,password from login where email='$email' AND password='$password'");

	    if( $query->num_rows > 0 )
	    {
	    	echo "Login Sucess";
	    	// put your redirection code here or set session 
	    	$_SESSION['auth'] = TURE ;
	    }
	    else
	    {
	    	 $error = "Invalid Details";
	    }
}

?>
<html>
<head> <link rel="stylesheet" type="text/css" href="https://bootswatch.com/cerulean/bootstrap.min.css"> 
<title>Login Tutotrial | Harshit.info</title>
</head>
<body>
<form method="post">
<div style="margin-left:450px; margin-top:70px; margin-right:590px;">
<form class="form-horizontal" >
  <fieldset >
    <legend>Login System With Remember me harshit.info</legend>
    <div class="form-group">
      <label for="inputEmail" class="col-lg-2 control-label">Email</label>
      <div class="col-lg-10">
        <input type="email" name="email" class="form-control" id="inputEmail" placeholder="Email" required>
      </div>
    </div>
    <div class="form-group">
      <label for="inputPassword" class="col-lg-2 control-label">Password</label>
      <div class="col-lg-10">
        <input type="password" name="password" minlength="4" class="form-control" id="inputPassword" placeholder="Password" required>
        <div class="checkbox">
          <label>
          <?php if(isset($error)){ echo $error; } ?>
<!--             <input type="checkbox">Remember
 -->          </label>
        </div>
      </div>
    </div>   
    <div class="form-group">
      <div class="col-lg-10 col-lg-offset-2">
        <button type="reset" class="btn btn-default">Cancel</button>
        <button type="submit"name="submit" class="btn btn-primary">Submit</button>
      </div>
    </div>
  </fieldset>
</form>
</div>
	
</body>
</html>

 

session_start();

 session start here before all code you should start a session

if ( isset ( $_POST [ 'submit' ] ) )

 here checking the submit button set or not

extract($_POST);

here extract the all posted data this function convert values to the variables like you field name is email its give you a that field value in $email variable.

database connection

$db = new mysqli("localhost", "root","", "test"); // Enter your databse details hostname, useer,password , dbname

mention your data base details here i have make a MYSQLI Connection ‘Localhost’ is a host name if you are use this code enter your own database host name or IP address if having some problem in connection call to your hosting service sport to connect a database.get more detail about databse connection – Php Database Connection

if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if having some problem in connection its show the error what problem heaving when you try it.

$query = $db->query("SELECT email,password from login where email='$email' AND password='$password'");

here i have execute a select query if email or password available in the login table or not  if email password is return a object that have some details or reference  details

if( $query->num_rows > 0 )
{
echo "Login Success";
// put your redirection code here or set session 
$_SESSION['auth'] = TRUE ;
}
else
{
$error = "Invalid Details";
}
}

num_rows ( )  MYSQLI function that is give a number of rows that you find or select in your query. check the number in rows in 0 its means now record in your login table. if now record its means invalid login details user enter and set a session is true for authorization for chek all pages that oyu want a authorized user.

<?php if(isset($error)){ echo $error; } ?>

here check the error message set or not is message is set the print the error message.

Leave a Reply

Your email address will not be published. Required fields are marked *