Creating a login script in php

      10 Comments on Creating a login script in php

Creating a login script in php:


Firstly let us create a simple bootstrap form to login.

  <form action="action_page.php">
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
    </div>
    <div class="form-group">
      <label for="pwd">Password:</label>
      <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
  </form>

It looks something like this:




Creating a database:

To create a database you can follow this script

 CREATE table `adminlogin`(`email` VARCHAR(50) PRIMARY KEY,`password` VARCHAR(32));

Assuming at any instant my database is:

Creating a login script in php

Creating a login script in php

<?php
  if(isset($_POST['submit'])) //avoid error:undefined index
  {
      $email=$_POST['email']; //getting values from the form
      $password=$_POST['password']; 
  }
  // establishing a database connection

  define("HOST", "localhost");     // The host you want to connect to.
  define("USER", "user_db");    // The database username. 
  define("PASSWORD", "123$%bb^7aqq!2");    // The database password. 
  define("DATABASE", "sample_db");    // The database name.
  $connection = mysqli_connect(HOST, USER, PASSWORD, DATABASE);

  // Check connection

  if (mysqli_connect_errno())
   {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
  // checking for empty variables

  if($email="" ||$password="")
  {
    echo "Please enter a valid value to each input";
  }
  else
  {
    //building a query
    $sql="SELECT * FROM `adminlogin` WHERE `password`='$password' WHERE `email`='$email'";
    //running query
    $run=mysqli_query($connection,$sql);
    //checks is a row is selected
    $count=mysqli_mun_rows($run);
    //success message
    if($count>=0)
    { 
        echo "Login successful";
    }
    else
    {
        echo "Login unsuccessful";
    }
 } 
?>

10 comments on “Creating a login script in php

    1. admin Post author

      Thanks for your feedback on post Creating a login script in php.This comment means a lot for us.

      Reply
  1. lelachaffey3

    I blog quite often and I really thank you for your information. Your article has really peaked my interest. I am going to book mark your blog and keep checking for new information about once per week. I subscribed to your Feed too.

    Reply
  2. jude03n5399

    Hey There. I found your blog the use of msn. This is an extremely neatly written article. I’ll be sure to bookmark it and return to read extra of your helpful info. Thanks for the post. I’ll certainly comeback.

    Reply
  3. bryce1978018

    Thank you for some other wonderful post. The place else may anyone get that type of info in such an ideal means of writing? I have a presentation subsequent week, and I’m at the search for such information.

    Reply
  4. shane31960612719

    Admiring the time and energy you put into your site and in depth information you provide. It’s good to come across a blog every once in a while that isn’t the same outdated rehashed information. Wonderful read! I’ve saved your site and I’m including your RSS feeds to my Google account.

    Reply
  5. randallstrope

    Valuable information. Fortunate me I found your website accidentally, and I am stunned why this twist of fate didn’t came about in advance! I bookmarked it.

    Reply

Leave a Reply

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