Latest Posts
  • How to Advertise Your Vape Business on Social Media: A Complete Guide
  • How To Create Custom Module Form with Menu Link in Drupal
  • Some Common Mistakes to Avoid While Doing SEO for Shopify
  • iPhone 8 & iPhone X Features Prices and Specifications
  • Make Money Easily to Automatically Link Keywords with Affiliate Links in WordPress
  • Yoast SEO Premium License Key Cracked or Patched
  • 6 Reasons To Choose WordPress For Blogging
  • Most Popular Coding Language In 2018
  • Facebook
  • Twitter
  • Instagram
  • RSS
  • YouTube
Developer Desks Developer Desks
  • Home
  • Codes
    • WordPress
    • PHP
    • Jquery
    • Javascript
    • Guest
  • Demos
  • blog
  • Tech News
  • Submit Your Articles
  • Contact
Php tutorials

Php code for login page with database using session

May 21, 2015
Share on Facebook Share on Twitter Google+ Pinterest LinkedIn Tumblr Email
Php Registration Form with email verification

For Previous chapter  we learn how to create login-page-using-session-without-stored-details-in-database,  Here We go to lear how we use login page with session using mysql database

php code for login page with database

First you have a knowledge in mysql with CURD(Create, Update, Read, Delete) Syntax. If you had then Go.

Step 1:
Run Xampp Server, Create Database ‘Developers’ in Localhost/phpmydamin.
step 2:
Create table (copy and paste )

CREATE TABLE IF NOT EXISTS `admin` (
`Sid` int(11) NOT NULL,
`User` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`Password` varchar(20) NOT NULL,
`phone` varchar(20) NOT NULL,
`Aid` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;         // create table

INSERT INTO `admin` (`Sid`, `User`, `email`, `Password`, `phone`, `Aid`) VALUES
(1, 'Admin', 'developerdesks@gmail.com', 'admin@123', '9876543210', 1);         // values inserted in db

 

Step 3:
Create Config file named as config.php

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";                         // default username in localhost is "root"
$mysql_password = "";                        //password as null
$mysql_database = "developer";               // Db name

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");

?>

 

Step 4:

Create login page and named as Index.php

<?php
session_start();                              // Session start
include 'config.php';                         // Config for database connection
if(isset($_POST['check']))                    // Check form submit with IF Isset function
{
$username =$_POST['username'];                // set username to variable
$password =$_POST['password'];                //set password to variable
$result = mysql_query("SELECT * FROM `admin` WHERE `User`=’$username’ AND Password=’$password'");               //check from database
if(mysql_num_rows ($result) > 0 )
{
$_SESSION['username'] = $username;            // set session name
$_SESSION['password'] = $password;
header('location:mainpage.php');
}
else
{
$err="Authentication Failed Try again!";
}
}
?>
<html>
<head>
<title>Main Page</title>
</head>
<body>
<Center>
<?php if(isset($err)){ echo $err; } ?> <!– Print Error –>

<form method="POST" name="loginauth" target="_self">

User Name: <input name="username" size="20" type="text" />
<br/><br/>
Pass Word: <input name="password" size="20" type="password" />
<br/><br/>
<input name="check" type="submit" value="Authenticate" />
</form>
</center>
</body>
</html>

 

Step 5:

Create mainpage.php

Code

<?php
session_start();
if((!isset ($_SESSION['username']) == true) and (!isset ($_SESSION['password']) == true))             // check session condition
{
unset($_SESSION['username']);                                
unset($_SESSION['password']);
header('location:index.php');
}
$logged = $_SESSION['username'];
?>
<html>
<head>
</head>
<body>
<center>
<h2>Welcome to Main page</h2><h3> Here we go with <a href="Secondpage.php">Second page </a></h3>
<a href=”logout.php”>logout</a>
</center>
</body>
</html>

Explanation

This index.php page will set username and password as “Admin” and “admin@123”.
Check whether given user name and password is correct or wrong.
If the given username and password is correct. Set $_SESSION variable as username and redirect to mainpage.php

HINT : We can Use many number of pages with session variables.

<?php
session_start();
if((!isset ($_SESSION['username']) == true) and (!isset ($_SESSION['password']) == true))
{
unset($_SESSION['username']);
unset($_SESSION['password']);
header('location:login.php');
}
$logged = $_SESSION['username'];
?>

simply place this in the top of the page.

Step 6:
Create logout.php page for destroy the session or unset the session

<?php
session_start();
if(!isset($_SESSION['username']))
{
header('location:index.php');
}
unset($_SESSION['username']);
session_destroy();
header('location:login.php');
?>

Explanation

When you click Logout.php its check the session and unset the variable values and destroy the session

Step 7 :
Create Secondpage.php
Code

<?php

session_start();
if((!isset ($_SESSION['username']) == true) and (!isset ($_SESSION['password']) == true))
{
unset($_SESSION['username']);
unset($_SESSION['password']);
header('location:login.php');
}
$logged = $_SESSION['username'];
?>

<html>
<head>
</head>
<body>
<center>
<h2>Welcome to Second page</h2>

<p>This page is the second page having session name "<?php echo $logged; ?>"
<a href=”logout.php”>logout</a>
</center>
</body>
</html>

Explanation:

The Secondpage.php is the another page with same session. It is used to learn how the session stored the variables in multiple page

This is the simple login form with stored and retrived session data’s from server database.
Enjoy the code.

Php session with database – Full code download here

Was this article helpful?

   

Awesome, share it:

Share Tweet Google Plus LinkedIn

Thanks!

Thanks for getting in touch with us.

code for loginfull codelogin with databasephp database sessionphp sessionregister with login
Raja sekar

I'm Rajasekar - Web developer, Freelancer, Blogger and Owner of DeveloperDesks. From India lives in Bahrain. I love to do coding, Creating websites and trying different with code and designs. You Can Hire Me

  • Website

Related Posts

Most Popular Coding Language in 2017

Most Popular Coding Language In 2018

February 18, 2017
How to Create Like & Unlike code like FB uisng PHP, MySQL and jQuery

How to Create Like & Unlike code like FB using PHP, MySQL and jQuery

November 25, 2016
live-search-in-php-and-mysql-using-jquery

Live search in PHP and Mysql using Jquery

October 13, 2016
  • Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

    Thank you for subscribing.

    Something went wrong.

    we respect your privacy and take protecting it seriously

  • Help Me To Improve My Desk
    Buy Me A Coffee
  • About

    Developer Desks is Teach Coding and updates latest technology news in our blog. We have Highly professional web designer and Developer. We do Website developing work as a freelancer. Founded in 2015, This blog focus on the latest trends, tutorials, opinion articles as well as tips and tricks to empower our readers to become better web developers.

    Read More
    Facebook Twitter Instagram LinkedIn

    Copyrights © 2014 - 2022 Developer desks. All rights reserved. Designed by Developer desks.

  • Latest Posts
    • guest-blog-post
      How to Advertise Your Vape Business on Social Media: A Complete Guide
      Guest Blog May 30, 2018
    • How To Create Custom Module Form with Menu Link in Drupal
      Drupal April 4, 2018
    • Shopify Mistakes
      Some Common Mistakes to Avoid While Doing SEO for Shopify
      SEO September 18, 2017
    • iphone 8 features
      iPhone 8 & iPhone X Features Prices and Specifications
      Android updates September 13, 2017
  • Downloads
    Icon
    Multi select filter ajax 1750 downloads 3.43 KB
    Download
    Icon
    Export excel from mysql table data 338 downloads 3.55 KB
    Download
    Icon
    Check Existing username and match password using ajax 333 downloads 3.51 KB
    Download
    Icon
    php session without database 325 downloads 1.64 KB
    Download

Powered by Developer Desks.

Top
Developer Desks