Php and Mysql

In this tutorial, you will become free source code to create a simple login course in PHP and MySQL. It is developed with consummate validation. It volition be very useful to develop the best login system. So, y'all can easily integrate it into your projects.

The login form is an integral part of a web application. It is used to access individual information from the websites. Many popular websites  Such every bit Facebook, Gmail, LinkedIn, Twitter & more have login features. Fifty-fifty you tin use it to create an admin login panel for managing the front-cease content dynamically.

Simple login Form in PHP

Contents

  • How to create a Simple Login Form Using PHP
    • 1. Connect PHP login Script to MySQL Database
    • 2. Create Login Grade Using HTML
    • 4. Create a Login Script Using PHP and MySQL
    • 5. Create Dashboard Using HTML and PHP
    • 6. Create Logout Script Using PHP
    • My Suggestion

How to create a Simple Login Course Using PHP

The login grade is created with 2 required fields similar electronic mail & password. These fields have a backend validation to accept only valid& registered email address & password

Features –

  • Using PHP, It can ship login data through the POST method.
  • Using MySQL, It can check registered users.
  • The login class does not allow invalid information and protects from the hacking
  • Login can catechumen illegal user input into legal input data.
  • You can't get into the dashboard until you volition not log in with valid credentials.
  • You can log out after login.

Before creating a login form in your projection, you must have to create a signup/registration grade. Because users can't log in without signup/ register. If you don't know to create a registration form, then there is no need to worry, you lot can besides larn it on this website past clicking the following link.

Read Also

Create Registration/signup Form using PHP

PHP Form Validation

Folder Structure

You lot should create the following binder structure. Otherwise, you can use the given login script directly in your projection.

login-system/     |__database.php     |__login-form.php     |__login-script.php     |__dashboard.php     |__logout.php     |__style.css          

1. Connect PHP login Script to MySQL Database

First of all, write the post-obit MySQL database connection script to connect PHP login Organization with Database.

File Name – database.php

            <?php  $hostname     = "localhost"; // enter your hostname $username     = "root";  // enter your tabular array username $password     = "";   // enter your countersign $databasename = "codingstatus.com";  // enter your database // Create connection  $conn = new mysqli($hostname, $username, $password,$databasename);  // Check connection  if ($conn->connect_error) {  die("Unable to Connect database: " . $conn->connect_error);  } ?>

two. Create Login Grade Using HTML

Now, configure the post-obit steps to create a login course using HTML

  •  Include login-script.php using the following script. Don't worry it will explain it in the next pace.
  • Include external style.css
  • Also, include the following bootstrap4 libraries to create a responsive login course.
  • Write the HTML lawmaking to create a login class

File Proper name – login-form.php

            <?php  include('login-script.php'); ?>  <!DOCTYPE html> <html lang="en"> <caput>   <championship>PHP Login Course</championship>   <meta charset="utf-8">   <meta name="viewport" content="width=device-width, initial-calibration=1">    <!--bootstrap4 library linked-->   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <fashion type="text/css">   .registration-form{      background: #f7f7f7;      padding: 20px;      edge: 1px solid orange;      margin: 50px 0px;    }    .err-msg{      color:cerise;    }    .registration-course form{      border: 1px solid #e8e8e8;      padding: 10px;      background: #f3f3f3;    } </style> </head> <body>  <div class="container-fluid">  <div class="row">    <div grade="col-sm-4">    </div>    <div class="col-sm-four">          <!--====registration class====-->     <div form="registration-form">       <h4 form="text-center">PHP Login Course</h4>       <p class="text-success text-middle"><xmp>&lt;?php <xmp>repeat $login; ?> </p> <course activeness="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="mail service">                   <!--// Email//-->         <div class="form-group">             <label for="e-mail">Email:</label>             <input blazon="text" class="form-control" id="email" placeholder="Enter email" name="email">             <p class="err-msg">                  <?php if($emailErr!=ane){ echo $emailErr; } ?>              </p>         </div>                  <!--//Password//-->         <div class="form-group">             <characterization for="pwd">Password:</label>             <input type="password" class="class-control"  placeholder="Enter password" name="countersign">             <p grade="err-msg">                  <?php if($passErr!=1){ repeat $passErr; } ?>              </p>         </div>                   <button type="submit" grade="btn btn-danger" value="login" name="annals">Login</button>       </grade>     </div>    </div>    <div class="col-sm-iv">    </div>  </div>    </div>   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.ane/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/one.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.four.1/js/bootstrap.min.js"></script> </trunk> </html>

four. Create a Login Script Using PHP and MySQL

Before writing a login script, you should understand the following points.

  • Include database connexion file usingrequire_once('database.php').
  • Assign a connection variable$conn to a new variable$db.
  • Cheque registration information are prepare usingisset($_POST['submit']), if these are fix then validate email & countersign using legal_input() part.
  • If both email & password  are validated successfully, call a custom function login() to procedure to login by checking valid credentials
  • If given credentials are already registered, information technology will redirect to the dashboard folio.

File Proper noun – login-script.php

<?php  require('database.php'); $db= $conn; // assign  your connection varibale  // by default, error messages are empty $login=$emailErr=$passErr='';     extract($_POST); if(isset($_POST['submit'])) {        //input fields are Validated with regular expression    $validName="/^[a-zA-Z ]*$/";    $validEmail="/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{two,three})+$/";       //Email Accost Validation if(empty($email)){   $emailErr="Email is Required";  } else if (!preg_match($validEmail,$e-mail)) {   $emailErr="Invalid Email Address"; } else{   $emailErr=truthful; }      // password validation  if(empty($countersign)){   $passErr="Password is Required";  }  else{    $passErr=true; }  // check all fields are valid or non if( $emailErr==1 && $passErr==1) {      //legal input values    $email=     legal_input($e-mail);    $countersign=  legal_input(md5($password));        // call login function    $login=login($e-mail,$password);  }  }  // catechumen illegal input value to ligal value formate function legal_input($value) {   $value = trim($value);   $value = stripslashes($value);   $value = htmlspecialchars($value);   return $value; }  // function to insert user information into database table function login($email,$password){    global $db;     // checking valid email   $sql="SELECT email FROM users WHERE email= ?";   $query = $db->set up($sql);   $query->bind_param('due south',$email);    $query->execute();   $exec=$query->get_result();   if($exec)   {   if($exec->num_rows>0){      // checking email and countersign     $loginSql="SELECT e-mail, countersign FROM users WHERE electronic mail=? AND password=?";     $loginQuery = $db->prepare($loginSql);     $loginQuery->bind_param('ss',$email, $countersign);      $loginQuery->execute();     $execQuery=$loginQuery->get_result();     if($execQuery)     {     if($execQuery->num_rows>0){        session_start();       $_SESSION['email']=$email;       header("location:dashboard.php");     }else{       return "Your Password is wrong";     }    }else{   render $db->error;   }     }   else   {     return $email." is not registered";   } }else{   render $db->error; }           } ?>

5. Create Dashboard Using HTML and PHP

  • To create a dashboard, you have to exercise the following things –
  • Start session using session_start()
  • Assign session login email $_SESSION['email'] to a new variable $email_address
  • If the session login email is empty, information technology will redirect to the login form page. means that you can't straight open the dashboard folio without login.

File Name – dashboard.php

<?php  session_start(); $email_address= $_SESSION['email']; if(empty($email_address)) {   header("location:login-grade.php"); } ?>  <!DOCTYPE html> <html lang="en"> <head>   <championship>Login Dashboard</championship>   <meta charset="utf-viii">   <meta proper name="viewport" content="width=device-width, initial-scale=1">  </head> <torso>  <div form="header"> <h1>Welcome to Dashboard  <a href="logout.php">Logout</a></h1> </div> <div class="user-box">     <div class="user-detail">       <p>Your Email Address</p>       <h3>        <?php          repeat $email_address;        ?>      </h3>     </div>   </div>  </body> </html>

vi. Create Logout Script Using PHP

You can speedily log out by writing the following few lines of PHP code.

  • Starting time of all, start the session using session_start()
  • Destroy session using session_destroy()
  • Redirect to the login page afterward logout.

File Proper noun – logout.php

<?php  session_start(); session_destroy(); header("location:login-course.php"); ?>

My Suggestion

Beloved Developers, I hope you have learned to create a uncomplicated login course using PHP. If y'all have any doubts or questions related to this tutorial, you can inquire me through the below comment box. I will respond as soon every bit possible.

Thanks For giving time to thistutorial…

shortridgeoursend.blogspot.com

Source: https://codingstatus.com/php-login-form/

0 Response to "Php and Mysql"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel