Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
Online Technical Tutorial
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. Blog & Articles
  3. PHP Login Page Example
Php Tutorials

PHP Login Page Example

By admin
Feb 09, 2019
3 min read

Quick Summary: PHP Login Page Example

PHP Login Page Example

This post for PHP beginners Login Page Example. I want to explain creating a database, posting form values, storing the session value and destroy the session. It is very useful and simple. 
You can also like the tutorial Secure PHP Login Page Example. Please go through this tutorial step by step and let me know in case you got any problem to configure PHP Login Page.

Database
MySQL users table columns id, username, password.

CREATE TABLE users
(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(30) UNIQUE,
password VARCHAR(30)
);

connection.php

Database configuration file.

<?php
define("DB_SERVER", "localhost");
define("DB_USERNAME", "username");
define("DB_PASSWORD", "password");
define("DB_DATABASE", "database");
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

Login.php

Contains PHP and HTML code.

<?php
include("connection.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password sent from Form
$myusername=mysqli_real_escape_string($db,$_POST["username"]); 
$mypassword=mysqli_real_escape_string($db,$_POST["password"]); 
$passwordSecure=md5($mypassword);
$sql="SELECT id FROM users WHERE username=$myusername and password=$passwordSecure";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
$active=$row["active"];
$count=mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
session_register("myusername");
$_SESSION["login_user"]=$myusername;

header("location: welcome.php");
}
else 
{
$error="Your Login Name or Password is invalid";
}
}
?>
<form action="" method="post">
<label>UserName :</label>
<input type="text" name="username"/><br />
<label>Password :</label>
<input type="password" name="password"/><br/>
<input type="submit" value=" Submit "/><br />
</form>

user_session.php

Session verification. If no session value page redirect to login.php

<?php
include("connection.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($login_session))
{
header("Location: login.php");
}
?>

welcome.php

<?php
include("user_session.php");
?>
<body>
<h1>Welcome <?php echo $login_session; ?></h1>
</body>

logout.php

SignOut Destroy the session value.

<?php
session_start();
if(session_destroy())
{
header("Location: login.php");
}
?>

 

Topics: Php Tutorials PHP Login Page Example

Written by admin

Technical contributor at Easy To Learning. Passionate about software development, clean code, and helping developers learn.

Discussion & Feedback

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group
Related Topics:
Php Tutorials PHP Login Page Example

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.