HOME | PHP Tutorial | CSS
facebook icon
AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP
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
<?php
$con = mysqli_connect("localhost","DataBase-username","password","DataBase");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

?>

Login.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
require('db.php');
session_start();
// 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);
if($rows==1){
$_SESSION['username'] = $username;
header("Location: welcome.php"); // Redirect user to index.php
}else{
echo "<div class='form'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
<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" />
</form>
<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>
</div>
<?php } ?>

</body>
</html>

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
<?php
include('session.php');
?>
<html>
<head>
<title>
Welcome to secure area</title>
</head>
<body>
<h1>Welcome to secure area <?php echo $login_session; ?></h1>
<h2><a href = "logout.php">Sign Out</a></h2>
</body>
</html>

Logout page php script is having information about how to logout from login session.
Example 3: logout.php
<?php
session_start();
if(session_destroy())
{
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
<?php
include('config.php');
session_start();
$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");
}
?>
Note: 

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

2594. PHP Tutorial - An Ultimate Guide for Beginners
PHP Tutorials for Beginners - agernic.com is a free interactive PHP tutorial for people who want to learn PHP, programming language in simple and easy steps
http://www.agernic.com/map-links.php

1297. PHP MySQL Free tutorials & scripts to download
Site Map and links to different PHP tutorials on basic PHP scripts and articles
https://www.plus2net.com/php_tutorial/site_map.php

3225. PHP Variables, php tutorial
PHP Variables php tutorial
https://www.agernic.com/php-tutorial/php-variables.html

3220. PHP Install, php tutorial
PHP Install php tutorial
https://www.agernic.com/php-tutorial/php-install.html

2816. PHP Tutorial
The PHP tutorial helps you learn how to build interactive web sites and web applications using PHP.
http://www.zentut.com/php-tutorial/

3221. PHP Operator Types, php tutorial
PHP Operator Types php tutorial
https://www.agernic.com/php-tutorial/php-operator-types.html

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 BETWEEN Operator
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 CREATE DATABASE - example
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