HOME | PHP Tutorial | CSS
facebook icon
AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP
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
<?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: 

<< 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.
https://tutorialehtml.com/en/php-tutorial-for-loop/

2033. PHP Tutorial - Introduction
Learn the basics of programming in PHP with Tizag.com
http://www.tizag.com/phpT/

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
https://www.agernic.com/php-tutorial/function-eregi_replace()-is-deprecated-in-php-how-to-fix.html

3351. PHP Arrays
Learn about PHP arrays with this PHP tutorial.
https://www.quackit.com/php/tutorial/php_arrays.cfm

1936. PHP if, else and elseif Statements, php tutorial
PHP if, else and elseif Statements php tutorial
http://www.agernic.com/php-tutorial/php-if-else-and-elseif-statements.html

695. PHP Tutorials for beginners - w3resource

https://www.w3resource.com/php/php-home.php

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