HOME | PHP Tutorial | CSS
facebook icon
HTML Tutorial

CSS Tutorial

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

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");

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

2239. PHP Tutorial - Array functions
PHP has a very comprehensive list of built-in functions to work with arrays. Please take into consideration that working with big and complicated arrays take lot of memory usage. You can recycle that memory just by destroying the array if you do not need

2599. Easy Learning with "Show PHP", example of a simple php script., php tutorial
Easy Learning with

1941. PHP While Loops. Tutorial While Loop, php tutorial
PHP While Loops. Tutorial While Loop php tutorial

php tutorials and introduction. What is php and how to write php

2254. PHP Tutorial - Variables
PHP variables are just a name for a value and you can simply use it like this $var_name = value;. They usually store sting caracters and numeric values for later use in the programming block.

3348. PHP convert array to string comma separated, php tutorial
PHP convert array to string comma separated php tutorial

Your add here
How to get PHP errors to display?
How to prevent sql injection in php mysqli
How do I get PHP errors to display - PHP error_reporting()
mysql_query(): The mysql extension is deprecated and will be removed i
Function eregi_replace() is deprecated in php - How to fix
PHP delete an element from an array
Uncaught Error: Call to undefined function MYSQL_NUM_ROWS() in
Deprecated: mysql_connect() mysql extension is deprecated and will be
mysqli_fetch_array() expects parameter 1 to be mysqli_result boolean g
SQL IN Operator - example
SQL like syntax and example
SQL WHERE AND & OR multiple conditions
SQL create table primary key autoincrement
PHP mysqli_connect () Function
SQL COUNT(), AVG() and SUM() Functions
Display data from SQL database with MySQLi into php - html table
SQL mysql order by columns, by date, by asc, by desc, random with PHP
PHP mysqli_query() Function - mysqli query
SQL delete row - SQL delete table
SQL update - SQL update select
Insert into SQL - Insert into table SQL - Insert data in sql
Login in PHP - simple login script with PHP MYSQL