PHP $_SESSION for multiple users at once

Viewed 25680

I'm wondering about how the $_SESSION array works. If I have a lot of users using my site do I need to set a subarray for each user? For instance right now I have

$_SESSION['userid'] = $userid;
$_SESSION['sessionid'] = $sessionid;
$_SESSION['ipaddress'] = $ipaddress;

but in order to cope with more users do I need to make a multidimensional array?

$_SESSION[$userid]['sessionid'] = $sessionid;
$_SESSION[$userid]['ipaddress'] = $ipaddress;

Is the $_SESSION global handled per client or just overall? Will having $_SESSION['userid'] set on login kick the previous user out and instate the latest logged in user?

2 Answers

well after searching alot and working on session i found my own way. i hope it works great for everyone here

this is the query for login page for my users: here i am storing email as session from input field after matching data from mysql

<?php
include_once("dbcon.php");
$que=mysqli_query($con,"select * from agents where companyemail='$email' AND 
pass='$password' AND post != 'Owner'"); 
$record = mysqli_fetch_assoc($que);
$_SESSION[$email]=$email;
header("Location:/dashboard/woresk/Dashboard_For_Agents/light/index.php? 
&loginid=$agentid");
?>

and then in the dashboard for users there is a logout option where i used this method

<?php
session_start();
include_once("dbcon.php");
$sid=$_GET['loginid'];
$que=mysqli_query($con,"select * from agents where id='$sid'"); 
$recorde = mysqli_fetch_assoc($que);
$email=$recorde['companyemail'];
unset($_SESSION[$email]); 
header('location:/dashboard/woresk/index.php');
?>

and to avoid users to enter dashbboard if they are not login or thier session is not set following code works great for me

<?php
session_start();
include_once("dbcon.php");
$sid=$_GET['loginid'];
$que=mysqli_query($con,"select * from agents where id='$sid'"); 
$recorde = mysqli_fetch_assoc($que);
$email=$recorde['companyemail'];
if(isset($_SESSION[$email]) && isset($_SESSION['alllogout'])){

 }
 else if(!isset($_SESSION[$email])){
    echo 
    "<script>
      window.location.href='/dashboard/woresk/index.php'
    </script>";
  }
  else if (!isset($_SESSION['alllogout'])){
    echo 
    "<script>
      window.location.href='/dashboard/woresk/index.php'
    </script>";
  }
  ?>

i hope this works for others too. if any question please let me know

Related