HOME | PHP Tutorial | CSS
facebook icon
PHP Tutorial

PHP forms
HTML Tutorial

CSS Tutorial

Login in PHP - simple login script with PHP MYSQL - user login - php session

<< Previous Page
PHP Date/Time Functions
Next Page >>
PHP Delete Element

User authentication - login sistem code in php - is very common in modern web application. This area it is a security mechanism that is used to restrict unauthorized access to visitors and it is reserved member-only areas and tools on a site.

Login form in PHP, login using PHP - how to create? - example

In this tutorial we'll create a simple login system using PHP script and MySQL.
We create config.php

Example 1: Config.php
$con = mysqli_connect("localhost","DataBase-username","password","DataBase");
// Check connection
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();



<!DOCTYPE html>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css" />
// If form submitted, insert values into the database.
if (isset($_POST['username'])){

$username = stripslashes($_REQUEST['username']); // removes backslashes
$username = mysqli_real_escape_string($con,$username); //escapes special characters in a string
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);

//Checking is user existing in the database or not
$query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'";
$result = mysqli_query($con,$query) or die(mysqli_error());
$rows = mysqli_num_rows($result);
$_SESSION['username'] = $username;
header("Location: welcome.php"); // Redirect user to index.php
echo "<div class='form'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
<div class="form">
<h1>Log In</h1>
<form action="" method="post" name="login">
<input type="text" name="username" placeholder="Username" required />
<input type="password" name="password" placeholder="Password" required />
<input name="submit" type="submit" value="Login" />
<p>Not registered yet? <a href='registration.php'>Register Here</a></p>

<br /><br />
<a href="https://www.agernic.com/php-tutorial.html/">PHP Tutorial</a> <br /><br />
More Web Development Tutorials: <a href="https://www.agernic.com">AgerNic.com</a>
<?php } ?>


Note: Config.php file is having information about MySQL

Login php script - welcome.php - logout.php page - How to create

Login php script - welcome.php

Example 2: welcome.php
Welcome to secure area</title>
<h1>Welcome to secure area <?php echo $login_session; ?></h1>
<h2><a href = "logout.php">Sign Out</a></h2>

Logout page php script is having information about how to logout from login session.
Example 3: logout.php
header("Location: login.php");

Session.php will verify the session, if there is no session it will redirect to login page.
Example 4: session.php
$user_check = $_SESSION['login_user'];
$ses_sql = mysqli_query($db,"select username from admin where username = '$user_check' ");
$row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$login_session = $row['username'];
if(!isset($_SESSION['login_user'])){ header("location:login.php");

<< Previous Page
PHP Date/Time Functions
Next Page >>
PHP Delete Element

login in php, form login php, login php mysql, php login page, login php code, php user login, php session, login session php, session php login, login page in php, facebook php login, login form in php, login using php, login sistem php, login code in php, admin login php, wordpress login, login php script, php login example, google login php, session in php, simple php login Login in PHP - simple login script with PHP MYSQL - php tutorial

2245. PHP Tutorial - For loop
The PHP for loop is native function that lops throw a piece of code while a given conditional expression is true. It normally executes for a given number of times.

2033. PHP Tutorial - Introduction
Learn the basics of programming in PHP with Tizag.com

3274. Function eregi_replace() is deprecated in php - How to fix, php tutorial
Function eregi_replace() is deprecated in php - How to fix php tutorial

3351. PHP Arrays
Learn about PHP arrays with this PHP tutorial.

1936. PHP if, else and elseif Statements, php tutorial
PHP if, else and elseif Statements php tutorial

695. PHP Tutorials for beginners - w3resource


Your add here
PHP trim() function

PHP similar_text() function

PHP explode() function

PHP strlen() function

PHP wordwrap() function

PHP str_repeat() function

PHP htmlspecialchars_decode() function

PHP substr() function

PHP strstr() function

PHP lcfirst() function

PHP print() function

PHP rtrim() function

PHP isset() Function

PHP str_replace() function

PHP ucfirst() function

PHP htmlentities() function

HTML Text Color
PHP Array - What is an Array?
HTML button action - How to create a HTML button action, size
SQL delete row - SQL delete table
CSS background, Color, Background Image
Home HTML Tutorial