ULTIMATE SALE Up to 90% OFF Web Hosting + Free Domain ( Free SSL & CloudFlare included ) Start Now
w3 tweaks
  • Effects
    • Scroll Effects
    • Text Effects
    • Shadow
  • Essentials
    • Arrows
    • Buttons
    • Background Patterns
    • Border Examples
    • Cards
    • Color Palettes
    • Dividers
    • Link styles
    • Loaders
    • Modal Windows
    • Notifications
    • Progress bar
    • Quote styles
    • Spinner
    • Tooltips
  • Media
    • Calendars
    • Carousels
    • Clocks
    • Gallery
    • Music Players
    • Sliders
    • Slideshows
    • Tables
    • Thumbnails
  • Navigation
  • Inputs
    • Range Sliders
    • Checkboxes
    • Toggle Switches
  • Scripts
    • Angularjs
    • Backbone.js
    • bootstrap
    • jQuery
    • ReactJs
    • JavaScript
    • Syntax Highlighters
    • tryit editor
    • PHP
  • API’s
    • Facebook
    • Google
    • Indeed
    • Twitter
    • YouTube
  • Web Tools
    • Beautifiers
    • Minifiers
    • Encoders & Decoders
w3 tweaks
  • Effects
    • Scroll Effects
    • Text Effects
    • Shadow
  • Essentials
    • Arrows
    • Buttons
    • Background Patterns
    • Border Examples
    • Cards
    • Color Palettes
    • Dividers
    • Link styles
    • Loaders
    • Modal Windows
    • Notifications
    • Progress bar
    • Quote styles
    • Spinner
    • Tooltips
  • Media
    • Calendars
    • Carousels
    • Clocks
    • Gallery
    • Music Players
    • Sliders
    • Slideshows
    • Tables
    • Thumbnails
  • Navigation
  • Inputs
    • Range Sliders
    • Checkboxes
    • Toggle Switches
  • Scripts
    • Angularjs
    • Backbone.js
    • bootstrap
    • jQuery
    • ReactJs
    • JavaScript
    • Syntax Highlighters
    • tryit editor
    • PHP
  • API’s
    • Facebook
    • Google
    • Indeed
    • Twitter
    • YouTube
  • Web Tools
    • Beautifiers
    • Minifiers
    • Encoders & Decoders
w3 tweaks
No Result
View All Result

Simple php login and logout script using php session and database using MySQL

by CV
November 27, 2019
in php

This tutorial will explain how to login and logout the web page using php session and database using MySql. By using this login script, we can show the secure pages after the login page successfully logged in. For authentication process we have to create login page with username and password input field. By using the input box, we can get the username and password using php post method which is secure after submitting the form.

RelatedPosts

13 Best PHP Login system

8 PHP Libraries for sending and parsing email

8 PHP Content Management Systems (CMS)

Login the App using facebook oauth in PHP

List of all PHP Operators

How to integrate the login using Google+ plus API in PHP

First we have to create a database and enter some dummy user information’s. In tutorial I’m using the WAMP Server 2.0 which has the MySQL and apache services which will run the php files.

STEP1: Create Database and Table using MySQL

Find the below image which show how to Open the phpmyadmin (Php my admin is for creating the MySQL database)

open phpmyadmin

Once the browser opens the phpmyadmin page, Enter the database name inside “create new database” field and click create button. For this tutorial we using database name as “user”. Find the image below

Create MySQL database

After the database created, click the SQL link from the top menu and paste the following sql query into the text area and click go button below the textarea. Please find the image below

CREATE TABLE IF NOT EXISTS `login` (
  `user_id` int(11) NOT NULL auto_increment,
  `username` varchar(30) NOT NULL,
  `password` varchar(30) NOT NULL,
  PRIMARY KEY  (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6;

Create user and login Table

Click the insert link for login table and give sample user name and password and click go button

Insert user details

After the sample user data inserted, table will show the data like below image.

Insert user details

STEP2: Connect the MySQL database using PHP

Create config.php file and paste the following code to the file.

<?php
	$hostname='localhost';
	$user = 'root';
	$password = ' ';
	$mysql_database = 'user';
	$db = mysql_connect($hostname, $user, $password,$mysql_database);
	mysql_select_db("user", $db);
?>
  1. Hostname : It specifies system host name or system ip address. The default value is localhost
  2. User: mysql username. Default value is root.
  3. Password: mysql password
  4. Mysql_Database : mysql database name

STEP 3: Create the Login page

Create login.php file and copy past the below code to the file. Following code has the login form and to get posted value after the form submitted using $_POST.

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <table align="center" bgcolor="#CCCCCC" border="0" cellpadding="0"
    cellspacing="1" width="300">
        <tr>
            <td>
                <form method="post" name="">
                    <table bgcolor="#FFFFFF" border="0" cellpadding="3"
                    cellspacing="1" width="100%">
                        <tr>
                            <td align="center" colspan="3"><strong>User
                            Login</strong></td>
                        </tr>
                        <tr>
                            <td width="78">Username</td>
                            <td width="6">:</td>
                            <td width="294"><input id="username" name=
                            "username" type="text"></td>
                        </tr>
                        <tr>
                            <td>Password</td>
                            <td>:</td>
                            <td><input id="password" name="password" type=
                            "password"></td>
                        </tr>
                        <tr>
                            <td> </td>
                            <td> </td>
                            <td><input name="submit" type="submit" value=
                            "Login"> <input name="reset" type="reset" value=
                            "reset"></td>
                        </tr>
                    </table>
                </form>
            </td>
        </tr>
    </table>
    <?php
    if (isset($_POST['submit']))
        {     
    include("config.php");
    session_start();
    $username=$_POST['username'];
    $password=$_POST['password'];
    $_SESSION['login_user']=$username; 
    $query = mysql_query("SELECT username FROM login WHERE username='$username' and password='$password'");
     if (mysql_num_rows($query) != 0)
    {
     echo "<script language='javascript' type='text/javascript'> location.href='home.php' </script>";   
      }
      else
      {
    echo "<script type='text/javascript'>alert('User Name Or Password Invalid!')</script>";
    }
    }
    ?>
</body>
</html>
  • Post Method: We used post method to get the values, because post method is secure way to pass the username and password. Post method is the best practice when we passing the secure data’s.
  • Include the config.php in login.php to connect the database:Using php “include()” function include the config.php in login.php.
  • Session:Session is used to start the session and store the values in session and get the values anywhere in the web pages. To start the session, call the session_start() function in login.php file.
  • Mysql_Num_Rows: Mysql_Num_Rows function is used to return a number of rows from the table.

login page

Step 4: Create home.php

Create the home.php file which has the script to destroy the session. When a user submit their username and password in login page, php login script will check the given username and password in the database table using MySQL select query, if the query return the value, script will redirect to home.php page. If username or password is wrong system will show error message as “User Name Or Password Invalid”.

Simple php login and logout script using php session and database using MySQL 1

Step 5: Destroy the session once the user clicks the logout link from home.php page

Create the logout.php file and paste the below code in to the logout.php file. Logout will destroy the session and redirect to login.php page again.

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

Download

Tags: databaseexampleloginlogin pagelogoutmysqlpasswordphpphpmyadminscriptsessionusernameusing
Previous Post

Resize div box horizontally using jquery draggable

Next Post

Update and create the iframe element with content using JavaScript

Related Posts

PHP Login system
php

13 Best PHP Login system

January 14, 2021
Collection of PHP Libraries for sending and parsing email
Libraries

8 PHP Libraries for sending and parsing email

May 9, 2020
PHP Content Management Systems
CMS

8 PHP Content Management Systems (CMS)

May 9, 2020
Login the App using facebook oauth in PHP 2
API's

Login the App using facebook oauth in PHP

November 29, 2019
List of all PHP Operators 3
php

List of all PHP Operators

August 14, 2019
How to integrate the login using Google+ plus API in PHP 4
API's

How to integrate the login using Google+ plus API in PHP

November 29, 2019
Next Post
Update and create the iframe element with content using JavaScript 5

Update and create the iframe element with content using JavaScript

Discussion about this post

Popular Posts

41 Multi step HTML forms

92 CSS Text styling and Effects

99 Hand-picked CSS Card Collections

20 HTML & CSS pricing tables

11 CSS Shopping Cart UI/UX

76 Hand Picked CSS Music Players

14 CSS Divider Collections

Simple php login and logout script using php session and database using MySQL

Tags

Angularjs (20) AngularJS Tutorials (16) animation (70) animation examples (14) beautiful (12) Button (24) button hover effect (15) Buttons (24) Calendar (38) calendars (38) cards (24) click animation (12) click buttons (19) CSS (128) css3 (20) css buttons (54) css calendar (36) css calendars (37) css effects (22) css hover effects (31) demo (18) effect (33) effects (41) essentials (48) Free Tool (13) hover (23) hover animation (31) Hover effects (40) html (86) inputs (14) Javascript (68) jquery (26) js (18) loaders (14) menu (13) mouse hover effects (36) navigation (14) pure (13) simple (13) text effects (24) Toggle Switches (13) tool (12) tutorial (32) tutorials (14) YouTube (13)
No Result
View All Result
  • Effects
    • Scroll Effects
    • Text Effects
    • Shadow
  • Essentials
    • Arrows
    • Buttons
    • Background Patterns
    • Border Examples
    • Cards
    • Color Palettes
    • Dividers
    • Link styles
    • Loaders
    • Modal Windows
    • Notifications
    • Progress bar
    • Quote styles
    • Spinner
    • Tooltips
  • Media
    • Calendars
    • Carousels
    • Clocks
    • Gallery
    • Music Players
    • Sliders
    • Slideshows
    • Tables
    • Thumbnails
  • Navigation
  • Inputs
    • Range Sliders
    • Checkboxes
    • Toggle Switches
  • Scripts
    • Angularjs
    • Backbone.js
    • bootstrap
    • jQuery
    • ReactJs
    • JavaScript
    • Syntax Highlighters
    • tryit editor
    • PHP
  • API’s
    • Facebook
    • Google
    • Indeed
    • Twitter
    • YouTube
  • Web Tools
    • Beautifiers
    • Minifiers
    • Encoders & Decoders

© 2020 w3tweaks

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist