Home > other >  php login and logout using session
php login and logout using session

Time:07-12

I'm trying to create a login page but I'm facing an issue with the session variable My code : login.php

 <?php 
 require('DBConnect.php'); 
 session_start();
 if(isset($_SESSION['status'])){
     header("location: index.php");
     exit(0);
 }
?>

index.php

<?php
   session_start();
   $user = $_SESSION['user'];
   if(!isset($_SESSION['status'])){header("location: login.php");}
?>

logout.php

<?php
session_unset();     
session_destroy();
header("location: login.php");
exit(0);
?>

When I go to the logout page, the page is redirected to login.php but it redirects also again to index.php whcih means that $_SESSION['status'] is set I'm only setting these Session variables when the POST request is sent, is there anything wrong here? Thanks in advance

CodePudding user response:

You need to add session_start() to the beginning of your logout.php code.

CodePudding user response:

You have a logic flaw in your code. logout.php ends the session and redirects you to login.php, but login.php starts a new session which is why it then redirects you again to index.php.

  • Related