Welcome, We will complete view on how to create admin login and logout page using session in PHP and MySQL Database. Show
I will suggest you watch the video, It will surely help you. Admin Login Page Here, we will have our admin login page. Where the admin already knows it's username and password. So, our main aim here is to make sure admin enter correct data. So, for that, we will take help of the database. Where we set a query if Admin enters the username and password that is already set in Database, then only admin can go to the main page of the admin panel. Else, be on the same page. Don't forget to add the links. Here is the link Link page<!-- Latest compiled and minified CSS --> <!-- jQuery library --> <!-- Popper JS --> <!-- Latest compiled JavaScript --> Admin Page Code<?php session_start(); $con = mysqli_connect('localhost', 'root' ); if($con){ // echo "conenction successful"; }else{ echo "no connection"; } ?> <!DOCTYPE html> <html> <head> <title></title> <link rel="stylesheet" type="text/css" href="style.css"> <?php include 'links.php' ?> </head> <body> <header> <div class="container center-div shadow "> <div class="heading text-center mb-5 text-uppercase text-white"> ADMIN LOGIN PAGE </div> <div class="container row d-flex flex-row justify-content-center mb-5"> <div class="admin-form shadow p-2 "> <form action="logincheck.php" method="POST"> <div class="form-group"> <label>Email ID</label> <input type="text" name="user" value="" class="form-control" autocomplete="off"> </div> <div class="form-group"> <label>Password</label> <input type="text" name="pass" value="" class="form-control" autocomplete="off"> </div> <input type="submit" class="btn btn-success" name="submit" > </form> </div> </div> </div> </header> </body> </html> PHP page for checking: Login Check PageHere we are also, setting the Session. Using, the session we can use the admin name throughout our pages. Also, when admin login into the page then we will use to the username to be shown on the main page of admin and that is possible by using session. <?php session_start(); $con = mysqli_connect('localhost', 'root' ); if($con){ echo "conenction successful"; }else{ echo "no connection"; } $db = mysqli_select_db($con, 'youtubeadmin'); if(isset($_POST['submit'])){ $username = $_POST['user']; $password = $_POST['pass']; $sql = " select * from admintable where user='$username' and pass='$password' "; $query = mysqli_query($con,$sql); $row = mysqli_num_rows($query); if($row == 1){ echo "login successful"; $_SESSION['user'] = $username; header('location:adminmainpage.php'); }else{ echo "login failed"; header('location:adminlogin.php'); } } ?> Admin Main Page:<?php Logout Page:<?php Style.css*{ What is a session in PHP?A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.
How many types of sessions are there in PHP?In the PHP session lifecycle, there are different stages like open, read, write, and close. Additionally, there are two more stages: destroy and garbage collection.
Where sessions are used in PHP?PHP session is used to store and pass information from one page to another temporarily (until user close the website). PHP session technique is widely used in shopping websites where we need to store and pass cart information e.g. username, product code, product name, product price etc from one page to another.
How many sessions can PHP handle?1000+ sessions can still be perfectly handled by standard PHP file based sessions. If you notice that is getting a problem, you can exchange the session backend easily. There are pluggable session handlers for memcached or other memory or database based storage systems.
|