9ja gurus forum

How to create a login page in PHP using session?

How to create a login page in PHP using session?
« Reply #1 on: May 28, 2017, 03:55:09 PM »
// Inialize session session_start(); // Check, if user is already login, then jump to
secured page if (isset($_SESSION['name'])) { header('Location: welcome.php'); } ?> <html> <head> <title>Admin Login</title> <link rel="stylesheet" type="text/css"
href="style.css"> </head> <body bgcolor="gray"> <h1>Admin Login</h1> <div class="login"> <form action="check_admin.php"
method="POST"> <input type="text" name="name"
placeholder="username" /> <input type="password" name="password"
placeholder="password" /> <input type="submit" name="submit"
value="Login Here" /> </form> </div> </body> </html>

$con = mysqli_connect
("localhost","root","","i_con"); $name=$_POST['name']; $password=$_POST['password']; $statment = "SELECT * from admin WHERE
name='$name' AND password='$password' "; $res = mysqli_query($con, $statment); if($res) { while($arr = mysqli_fetch_array($res)) { $_SESSION['name']=$name; header('Location:welcome.php'); } } $row= mysqli_num_rows($res); if(!$row) { echo "Try Again!!!!"; header('Location:admin.php'); } ?>

 session_start(); ?>
 <html> <head> <title> Home </title> </head> <body> <?php if(!isset($_SESSION['name'])) // If session is not set
that redirect to Login Page { header("Location:admin.php"); } echo $_SESSION['name']; echo "Login Success"; echo "<a href='logout.php'> Logout[/url] "; ?> </body> </html>

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

If you have any question donn`t fail to ask