Jump to content

PHP Session Variable Getting Unset


Recommended Posts

I have a file that keeps track of ID's and how you want that ID group sorted.  Essentially, I have a $_GET variable that contains a value such as: 1000.name

 

I then take this value and explode it, to create $orgID and $sortBy. I then create a session like so:  $_SESSION[$orgID] = $sortBy;

 

It appears as if only the most recent session that was set is being displayed, as doing a dump only shows the last one set.

 

Below is a brief overview of what I've got going.

 

session_start();

   if(isset($_GET["setorder"]))
   {
      
      $sortContents = explode(".", $_GET["setorder"]);
      
      $orgID = $sortContents[0];
      $sortBy = $sortContents[1];
      
      $_SESSION[$orgID] = $sortBy;
      

   }

   echo $_SESSION[1000];
   echo $_SESSION[1001];

 

Assuming we go to the page with 1000.name first, and then 1001.title second..  first glance will show "name", which is correct.  going to the page a second time will only show "title", ignoring the 1000.

 

Suggestions?

Link to comment
https://forums.phpfreaks.com/topic/105830-php-session-variable-getting-unset/
Share on other sites

Made a quick file to essentially do.. what I'm doing in a nutshell and yielding the same results.

 

<?php

session_start();

if(isset($_GET["create"]))
{
   
   $sortContents = explode(".", $_GET["create"]);
   
   $orgID = $sortContents[0];
   $sortBy = $sortContents[1];
   
   $_SESSION[$orgID] = $sortBy;
   

}

echo "created sessions: <br />";
print_r($_SESSION);

?>

 

tested via: filename.php?create=a_number.a_word

Solution: I had to tack on text to the variable in the Session initialization for it to work.. do you HAVE to have text in the session index for some reason?

 

E.g.

 

$_SESSION[$orgID] = "name"; // this wont work

 

$_SESSION['id_'.$orgID] = "name"; // this works.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.