Sunday, June 10, 2018

How To Make Login Form PHP

<?php
//lets start the session. ok
session_start();
if(isset($_SESSION['uname'])) // ohh the error was here.. $_POST is wrong.. we need to user session
{
header("LOCATION:welcome.php");
exit();
}
else
{
}
// fixed
?>
<br><br>
<h1 align="center"> Welcome To Login Form </h1> 

<form method="post" action="" align="center">
Email : <br><input type="text" name="email" placeholder="Enter Your Email"><br><br>
Password :<br> <input type="password" name="password" placeholder="Enter Your Password"><br><br>
<input type="submit" value="Login" name="login"> 

</form>

<?php

$con = mysqli_connect('localhost','root','','learn'); // database connection successfull.. ok

if(isset($_POST['login']))
{
$email =  $_POST['email'];
$password = $_POST['password'];

//letst make the query

$query = "SELECT * FROM `users` WHERE `email`='$email' AND `password`='$password'";
$run = mysqli_query($con,$query);

$row = mysqli_num_rows($run);
if ($row <1 ) 
{
echo "<script>alert('Username Or Password Wrong')</script>".mysqli_error($con);
}
else
{
$data = mysqli_fetch_assoc($run);
$uname = $data['name'];
$_SESSION['uname'] = $uname;
header("LOCATION:welcome.php"); 

}

}

?>
<br>
<div align="center">

</div>

Use This Code For Welcome.php

<?php
// lets write an code, if user haven't any session then redirecting it to back login form ... ok


session_start();

if(isset($_SESSION['uname'])) // ohh the error was here.. $_POST is wrong.. we need to user session
{
}
else
{
header("LOCATION:login.php");
exit();
}

$name = $_SESSION['uname'];

?>
<h1 align="l">
<a href="logout.php">Logout</a>
<?php
echo "Hello," .$name. "Your Most Welcome";

// let's checking  that can we access login form after login succesful... ohh we find a bug lets fix it
?>

</h1>



Use This Code For Logout.php

<?php
session_start();;
session_destroy();
header("LOCATION:login.php");

?>

No comments:

Post a Comment

How To Make Signup Form .PHP

<?php session_start(); if(isset($_SESSION['uname'])) // ohh the error was here.. $_POST is wrong.. we need to user session {...