Login and Registration Form in PHP using Mysqli
Contents
Today, We want to share with you Login and Registration Form in PHP using Mysqli.
In this post we will show you Login and Registration Form in PHP using Mysqli, hear for Login and Registration Form in PHP using Mysqli we will give you demo and example for implement.
In this post, we will learn about Login and Registration Form in PHP using Mysqli with an example.
Session.php
<?php error_reporting(0); include('includes/config.php'); // Connect to the database $mysqli = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME); session_start(); $check = $_SESSION['login_username']; if(!isset($check)) { header("Location:index.php"); } ?>
Login and Registration Form in PHP using Mysqli
Login using Mysqli with PHP
// Login to system if($action == 'login') { // output any connection error display in mysqil if ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); } session_start(); extract($_POST); $username = mysqli_real_escape_string($mysqli,$_POST['username']); $pass_encrypt = md5(mysqli_real_escape_string($mysqli,$_POST['password'])); $query = "SELECT * FROM `users` WHERE username='$username' AND `password` = '$pass_encrypt'"; $results = mysqli_query($mysqli,$query) or die (mysqli_error()); $count = mysqli_num_rows($results); if($count!="") { $row = $results->fetch_assoc(); $_SESSION['login_username'] = $row['username']; $_SESSION['userid'] = $row['id']; // processing remember me option and setting cookie with long expiry date if (isset($_POST['remember'])) { session_set_cookie_params('604800'); //one week (value in seconds) session_regenerate_id(true); } echo json_encode(array( 'status' => 'Success', 'message'=> 'Login was a success! Transfering you to the system now, hold tight!' )); } else { echo json_encode(array( 'status' => 'Error', //'message'=> 'There has been an error, please try again.' 'message' => 'Login incorrect, does not exist or simply a problem! Try again!' )); } }
Logout in PHP using mysqli
<?php //your all the session_destroy here in mysqil session_start(); session_destroy(); header("Location: index.php"); ?>
login.php
<?php include('header.php'); // simple Connect to the database display in mysqil $mysqli = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME); // here output any connection error display in mysqil if ($mysqli->connect_error) { die('Error : ('.$mysqli->connect_errno .') '. $mysqli->connect_error); } session_start(); //check condition display in mysqil if($_POST['username'] != "" && $_POST['password'] != "") { extract($_POST); $username = mysqli_real_escape_string($mysqli,$_POST['username']); $pass_encrypt = mysqli_real_escape_string($mysqli,$_POST['password']); $fetch = $mysqli->query("SELECT * FROM `users` WHERE username='$username' AND `password` = '$pass_encrypt'"); $row = mysqli_fetch_array($fetch); if (password_verify($pass_encrypt, $row['passowrd'])) { $_SESSION['login_username'] = $row['username']; echo 1; } else { echo 0; } } else { header("Location:index.php"); } ?> <?php include('footer.php'); ?>
We hope you get an idea about Login and Registration Form in PHP using Mysqli
We would like to have feedback on my Information blog .
Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
If you liked this post, Please donโt forget to share this as Well as Like FaceBook Page.
We hope This Post can help you…….Good Luck!.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.