How can i logout single account?

177 Views Asked by At

I'm just implementing a login and logout system using PHP and experiencing problems with logout. The system outline is as follows:

  1. When the user logs in, a session is created with a session variable "user" and "stud" as i'm creating it for student and admin.

  2. After the session is set up, the user is redirected to home.php file.

  3. In that file, a logout button is placed. When the user clicks the logout button session destroyed, but it destroyed both account. I try login both account, student and admin, but when i try logout for admin, it'll destroyed both account.

Anyone can help me with this problem?

here is my coding for admin logout:

session_start(); if(!isset($_SESSION['user'])) { header("Location: index.php"); } else if(isset($_SESSION['user'])!="") { header("Location: homeAdmin.php"); }
if(isset($_GET['adminLogout'])) { session_destroy(); unset($_SESSION['user']); header("Location: index.php"); }

3

There are 3 best solutions below

1
On

You have maintain the sessions for user and admin separately like $_SESSION['user'] and $_SESSION['stud'] and destroy the respective session on logout process. Means if logout the user than destroy only $_SESSION['user']

0
On
if(isset($_GET['adminLogout'])) { session_destroy(); unset($_SESSION['user']); header("Location: index.php"); }

Here you are taking the parameter admin logout and destroying user session as well. I assume you should first check which session is set and destroy that particular session. Also don't use session destroy, just unset will work for you. session_destroy destroys all the session, you can check here for more reference how it works

http://php.net/manual/en/function.session-destroy.php

0
On

thank you guys i've got my answer. here am sharing teh answer of the problems:

<?php
session_start();

if(!isset($_SESSION['user']))
{
    header("Location: index.php");
}
else if(isset($_SESSION['user'])!="")
{
    header("Location: homeAdmin.php");
}

if(isset($_GET['adminLogout']))
{
    unset($_SESSION['user']);
    header("Location: index.php");
}
?>

just delete the session_destroy()