• support@answerspoint.com

How to create logout page in php?

2848

I create login.php page, and on this page unset my session. but without login we entry my welcome.php page through url.

But i want to withaut login no one enter my welcome.php page.

1Answer


0

At first we have to create login page with username and password input field. By using the input box, we can get the username and password using php post method which is secure after submitting the form. Now we have create session on welcome page, and put user name in my session. Then we create logout page and unset my session

Config.php

Config.php file is having information about MySQL Data base configuration.

<?php
	$dbhost = 'localhost';
	$dbuser = 'root';
	$dbpass = '';
	$dbname = 'login_test';
	$con = mysql_connect($dbhost, $dbuser, $dbpass);
	if(! $con ) 
	{
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db($dbname);
	//echo 'Connected successfully';
?>

Login.php

Login PHP is having information about php script and HTML to do login.

<?php
include("config.php");
session_start();
$lmsg="";
if($_POST) 
{
	  $user_id = $_POST['user_id'];
	  $user_pass = $_POST['user_pass']; 
	  
	  $sql = "SELECT * FROM login WHERE user_id = '$user_id'";
	  $result = mysql_query($sql) or die(mysql_error());
	  $count = mysql_num_rows($result);
	  //echo $count;
		
	  if($count == 1) 
	  {
		  $info=mysql_fetch_array($result);
		  if($info['user_pass']==$user_pass)
		  {
			  $_SESSION['rmg']= "$user_id";
			 header("location:welcome.php"); 
		  }
		  else
		  {
			  $lmsg = "password not match";  
		  }
	  }
	  else
	  {
		$lmsg= "Invalid User";  
	  }
   }
?>
<html>
   <head>
      <title>Login Page</title>
      
      <style type = "text/css">
         body {
            font-family:Arial, Helvetica, sans-serif;
            font-size:14px;
         }
         label {
            font-weight:bold;
            width:100px;
            font-size:14px;
         }
         .box {
            border:#666666 solid 1px;
			padding:5px;
         }
      </style>
      
   </head>
   <body>
	
      <div align = "center">
         <div style = "width:300px; border: solid 1px #000; " align = "left">
            <div style = "background-color:#ccc; color:#000; padding:10px;"><b>Login</b></div>
            <div style = "margin:30px">
            	<form action ="" method = "post">
                  <label><?php echo $lmsg;?> <br /><br />
                  <label>UserName  :</label><input type = "text" name = "user_id" class = "box"/><br /><br />
                  <label>Password  :</label><input type = "password" name = "user_pass" class = "box" /><br/><br />
                  <input type = "submit" value = " Submit "/><br />
               </form>
					
            </div>
         </div>
      </div>

   </body>
</html>

welcome.php

welcome.php it will display After successful login.

<?php
   session_start();
   if(isset($_SESSION['rmg']))
   {
	   $rmg=$_SESSION['rmg'];
   }
   else
   {
   	$rmg=null;
   }
	if(empty($rmg))
	{
		header('Location: index.php');
	}
?>
<html">
   <head>
      <title>Welcome page </title>
   </head>
   
   <body>
   	<h1>Welcome <?php echo $rmg ?></h1> 
    <a href="logout.php">Lougout</a>
   </body>
</html>

logout.php

logout.php it will unser your session and redirect your login page.

<?php
session_start();
// Delete certain session
unset($_SESSION['rmg']);
header('Location: login.php');
?>

 

  • answered 6 years ago
  • Community  wiki

Your Answer

    Facebook Share        
       
  • asked 6 years ago
  • viewed 2848 times
  • active 6 years ago

Best Rated Questions