Image result for how to create simple login form in php

Login is a very important part of any website, from securing contents from prying eyes to tracking visitors on your website. This tutorial is a simple login form for a website.

Firstly you create "login.php" like this

Code:
<?php
session_start
();
// This starts the session which is like a cookie,
but it isn't saved on your hdd and is much more secure.
mysql_connect("localhost","DATABASE USER HERE","PASSWORD");
// Connect to the MySQL server
mysql_select_db("login");
 // Select your Database
if(isset($_SESSION['loggedin']))
{
    die(
"You are already logged in!");
 // That bit of code checks if you are logged in or not,
and if you are logged, you can't log in again!
}

if(isset($_POST['submit']))
{
   
$name mysql_real_escape_string($_POST['username']); 
// The function mysql_real_escape_string() stops hackers!
   
   $pass mysql_real_escape_string($_POST['password']); 
   $mysql mysql_query("SELECT * FROM users WHERE name = '{$name}
   AND password = '{$pass}'");

//get all users in the database with that username and password.
   
if(mysql_num_rows($mysql) < 1)
   {
     die(
"Password was probably incorrect!");
   } 
//check number of rows the MySQL query was less than 1,
so if it couldn't find a row, 
the password is incorrect or the user doesn't exist!
   
   $_SESSION['loggedin'] = "YES";
// Set it so the user is logged in!
   $_SESSION['name'] = $name
// Make it so the username can be called by $_SESSION['name']
   die("You are now logged in!"); 
// it doesn't show the login form after you are logged in!

echo "<form type='login.php' method='POST'>
Username: <br>
<input type='text' name='username'><br>
Password: <br>
<input type='password' name='password'><br>
<input type='submit' name='submit' value='Login'>
</form>"
//the form to enter your password and username to login.
?>

After you have put that code, run this query on your database:

Code:

CREATE TABLE `users` (
`id` BIGINT( 60 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 50 ) NOT NULL ,
`password` VARCHAR( 32 ) NOT NULL ,
`date` INT( 32 ) NOT NULL ,
`email` VARCHAR( 80 ) NOT NULL
);

And then add all of the users you wish to the table!
--------------
To integrate this within your site, you can do the following at the top of your site.
This Code Like Lock.php or This Code Using to Lock Your Webpages

Code:
<?php
session_start(); // NEVER forget this!
if(!isset($_SESSION['loggedin']))
{
    die("To access this page, you need to <a href='login.php'>LOGIN</a> "); 

// Make sure they are logged in!
} 
// What the !isset() code does, is check to see if the variable $_SESSION['loggedin'] is there, and if it isn't it kills the script telling the user to log in!
?>

If you want display your user name on your site use this code:
Code
<?php echo "Hello there, {$_SESSION['name']}! Welcome to my site!"; ?>

Logout.php:

Code
<?php
session_start();
if(isset($_SESSION['name']))
  unset($_SESSION['loggedin']);
  header('Location: index.php');
?>
or
<?php
    session_start();
    $_SESSION = array();
    session_destroy();
?>
Share To:

Post A Comment:

0 comments so far,add yours