Jump to content
Larry Ullman's Book Forums

How To Add More Session Values?


Recommended Posts

I'm trying to work with php Sessions. My login script assigns some values to the session array. Now, after user logs in and goes further, I want to add other variables in that session. How can I add other values to an already started session? Is it possible?

Link to comment
Share on other sites

If I remember correctly, sessions in PHP are handled through the $_SESSION superglobal, which is an associative array. That being the case, you can add to it as you please. For example:

 

$_SESSION['newValue'] = 24;

$_SESSION['anotherNewValue'] = 'Hamster';

 

That make sense?

  • Upvote 1
Link to comment
Share on other sites

  • 4 weeks later...

I tried to do as HartleySan suggested. But, as I'm trying more and more with SESSION, it turns out that SESSION is an associate array, but special kind of.

It needs special handling. I understand this now, but still I'm unable to figure out(or search online) how to make/use a SESSION variable for this kind of work:

 

My login page assigns values to SESSION like this : $_SESSION['user_id']="9999"; $_SESSION['lname']="kkkkk" .

 

And after some activity I want to store another array of information for this user. I want a SESSION like this:

$_SESSION("user_id", "name", array("mid", mname, mlastname))...

 

How to add that array? Or how to start a SESSION like this so it can take in array later on?

Link to comment
Share on other sites

So you'd do

$_SESSION['user_id'] = 9999; // Don't need to quote numbers.

$_SESSION['name'] = 'blah';

And then:

$_SESSION['something'] = array('mid' => 'value', 'mname' => 'value', 'mlastname' => 'value');

Link to comment
Share on other sites

  • 2 weeks later...

$_SESSION['something'] = array('mid' => 'value', 'mname' => 'value', 'mlastname' => 'value');

I got above code working. But then I realize, I need to store array of second array. Each user has multiple members. I tried to run following code, but it stores only the last "mid, mname" value from while loop? How to add array of array in already started session?

 

 

 

$smid = 0; // to give different index type value

//Fetch and print all records:

while($row = mysqli_fetch_row($r)) // from query run to fetch members under a user

{

 

// list out member names in a horizontal row:

echo '<tr class="rowA">';

echo '<td><a href="addbook9.php?id='. $row[1] .'&nm='. $row[2] .'"> '. ucfirst($row[2]) .'</a></td>'; //don't want to send id and name in url

//$mname = $row[1];

$mid = 'mid' . $smid; // this will give value like mid0, mid1....

$mname = 'mname' . $smid;

$_SESSION['members'] = array($mid => $row[1], $mname=>$row[2]); //want to store id, name in session to use later

$smid++;

echo '<td><a href="view_books.php?id='. $row[1] .'&nm='. $row[2] .'">' . $row[3] . '</td>';

echo '</tr>';

} //End of while loop.

 

echo '</table>';

Link to comment
Share on other sites

<p>I changed above code as per following: </p>

<p> </p>

<div>$memids = array();</div>

<div>          $memnames = array();</div>

<div> </div>

<div> //Fetch and print all records:</div>

<div> <span class="Apple-tab-span" style="white-space:pre"> </span>  while($row = mysqli_fetch_row($r))</div>

<div>   {</div>

<div> </div>

<div>// list out member names in a horizontal row:</div>

<div>echo '<tr class="rowA">';                </div>

<div>                  echo '<td><a href="addbook9.php?id='. $row[1] .'&nm='. $row[2] .'"> '. ucfirst($row[2]) .'</a></td>';</div>

<div>                  array_push($memids, $row[1]);                          //add each id & name in another array.</div>

<div>                  array_push($memnames, $row[2]);</div>

<div>                 </div>

<div>                  $_SESSION['members'] = array('mid' => $memids, 'mname'=> $memnames);</div>

<div>                 </div>

<div> echo '<td><a href="view_books.php?id='. $row[1] .'&nm='. $row[2] .'">' . $row[3] . '</td>';</div>

<div> echo '</tr>';</div>

<div>   }    //End of while loop.</div>

<div> </div>

<div>echo '</table>';</div>

<div> </div>

<div>And it shows following result:</div>

<div> </div>

<div>Array ( [user_id] => 5 [fname] => SONAL [lname] => MACWAN [lid] => 1 [members] => Array ( [mid] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 8 [4] => 11 [5] => 12 ) [mname] => Array ( [0] => Stavan [1] => Ethan [2] => sonal [3] => daxesh [4] => Rupal [5] => Kavi ) ) )</div>

<div> </div>

<div>So, I am more confused now. How will I access each id-name pair? Is this the right way to add array of array into session?</div>

 

Link to comment
Share on other sites

 Share

×
×
  • Create New...