Login logout session in php
In this login logout example in PHP we used 3 file Show
login_user.sqlCREATE TABLE `login_user` ( `id` int(11) NOT NULL, `name` varchar(60) NOT NULL, `user_name` varchar(20) NOT NULL, `password` varchar(20) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; login.php0) { $con = mysqli_connect('127.0.0.1:3306','root','','admin') or die('Unable To connect'); $result = mysqli_query($con,"SELECT * FROM login_user WHERE user_name='" . $_POST["user_name"] . "' and password = '". $_POST["password"]."'"); $row = mysqli_fetch_array($result); if(is_array($row)) { $_SESSION["id"] = $row['id']; $_SESSION["name"] = $row['name']; } else { $message = "Invalid Username or Password!"; } } if(isset($_SESSION["id"])) { header("Location:index.php"); } ?> index.php
logout.phpIntroduction This article explains login and logout with session in PHP. You will first create a database and a table named login and then create a login form with simply two fields, username and password. Then you will make a connection with your MySQL table "login" and enter some PHP code. I will use a session for authentication purposes in login and logout. Example First of all, create the "index.php" file as in the following: a{ float:left; text-decoration:none; padding:0px 2px 0px; } h2{ color:#008844; font-size:20px; text-align:center; }input{ background-color:#33FFFF; color:#000; }
"signup.php">"submit"
name="submit" value="Signup"> "Login.php">"submit" name="submit" value="login">
Output This is your "login.php" file. $error=""; mysql_connect("localhost","root","")or dir(mysql_error()); mysql_select_db('demo'); session_start(); if(isset($_POST['submit'])){ $username = $_POST['username']; $password = $_POST['password']; if(isset($username) && isset($password)){ $query="SELECT id FROM login WHERE username='$username' and password='$password'"; $result=mysql_query($query); $row=mysql_fetch_array($result); $id=$row['id']; $count=mysql_num_rows($result); if($count==1) { //session_register("username"); $_SESSION['name']=$username; header("location: welcome.php"); }else{ $error = 'please enter username and password'; } } } ?> "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> "http://www.w3.org/1999/xhtml"> "Content-Type" content="text/html; charset=iso-8859-1" /> input{ color:#4A0000; border:1px solid #4A0000; }
action="" method="post"> Output This is your "secure.php" file. mysql_connect("localhost","root","")or dir(mysql_error()); mysql_select_db('demo'); session_start(); $check=$_SESSION['name']; $query=mysql_query("select username from login where username='$check' "); $data=mysql_fetch_array($query); $user=$data['username']; if(!isset($user)) { header("Location: login.php"); } ?> This is your "welcome.php" file. a{ float:left; text-decoration:none; }h2{ color:#008844; font-size:20px; text-align:center; } Output When you will click the on "logout.php" button your session will be clean and redirect to the login page. session_start(); if(session_destroy()) { header("Location: login.php"); } ?>Output How can I logout of function in PHP?The process is: - Click Log In button on index. php - Enter username and password to access authenticate index file. - Click log out button, which references the logout. php file - it SHOULD clear the cache and return the user to the top level index.
How do I keep a user logged in PHP?Hence the user can log in without having to enter the Username and Password again until the life of that cookie expires. The example code given below is the way how to remember password checkbox works through PHP. $name = mysqli_real_escape_string( $connect , $_POST [ "user_name" ]);
How do I create a logout button?Create a Logout button on the form. Right-click the form, and select Create a New Field > Button.
...
To create a logout button.. How do we check if a user is logged in in PHP?session_start(); Check if $_SESSION["loggedIn" ] (is not) true - If not, redirect them to the login page.
|