News:

Alert!!! HTML Evaluation 01- Quiz

Main Menu

PHP Login: 015

Started by Cohort Master, Apr 17, 2024, 03:29 AM

Previous topic - Next topic

Cohort Master

MySQL is a popular choice as a backend database for PHP powered web applications. In this chapter, we shall learn to develop a login page for a PHP application that authenticates the given username and password.

You should have a web server having PHP and MySQL installed for experimenting with the example discussed in this chapter. The bundled binaries of Apache, PHP and MySQL (MariaDB) in the form of XAMPP for your operating system can be easily installed.


The first part of PHP login application is to establish database connection object. We use myqli API to obtain connection object. Save following code as "config.php"
<?php
define
('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME''demos');
$con mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
 echo 
"Failed to connect to MySQL: " mysqli_connect_error();
}
?>

This PHP script is called inside the login script. It presents the user with a HTML form to enter username and password. In case the form is submitted, PHP runs a SELECT query to retrieve a row in the admin table where the username and passcode matches with the user inputs.

If the row count is one, it indicates that the username and the password entered matches. The username is save to the $_SESSION variable and the browser is directed to welcome.php script

Login.php
Save the following code as "login.php" −
<?php

session_start
();

include_once(
"config.php");

if(isset(
$_POST['login']))

{
$email=$_POST['email'];

$password=md5($_POST['password']);

$retmysqli_query($con,"SELECT * FROM userregistration WHERE email='$email' and password='$password'");
$num=mysqli_fetch_array($ret);
$status=$num['status'];
if(
$num>0)
{

if(
$status==0)
{
$_SESSION['action1']="Verify  your Email Id by clicking  the link In your mailbox";
}
else {
$_SESSION['login']=$email;
$_SESSION['id']=$num['id'];
$_SESSION['name']=$num['name'];
$extra="welcome.php";
$host=$_SERVER['HTTP_HOST'];
$uri=rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
header("location:http://$host$uri/$extra");
exit();
}
}
else
{
$_SESSION['action1']="Invalid username or password";
$extra="login.php";
$host  $_SERVER['HTTP_HOST'];
$uri  rtrim(dirname($_SERVER['PHP_SELF']),'/\\');

header("location:http://$host$uri/$extra");

exit();

}

}

 
?>

To echo failed session attempts if login fails due to incorrect information:
<?php echo $_SESSION['action1']; ?><?php echo $_SESSION['action1']="";?>
Welcome.php or Dashboard.php
The "welcome.php" script gets invoked when the user is authenticated. It reads the session variable to display a welcome message.
<?php
session_start
();
include_once(
"config.php");
if(
$_SESSION['login'])
{
 
?>

Insert the above code to secure the welcome or dashboard page. It can only be opened to login users.

Session is used to store user's information to PHP hence the browser closes. To kill session is to either log out or close browser
<?php echo $_SESSION['name']; ?>
To echo login user name etc.

Logout.php
Finally, the logout script removes the destroys the session and redirects the user to the login page.
<?php
session_start
();
$_SESSION['login']=="";
session_unset();
$_SESSION['action1']="You have logged out successfully..!";
?>

<script language="javascript">
document.location="login.php";
</script>

Password Encryption
To encrypt your password to secure it from being seen or hacked. It is advised to encrypt all passwords in the registration page before sending it to the database.
$password=md5($_POST['password']);

Good luck!!