March 10, 2013

Get only one array from the $_SESSION variable

Question by Nadia Shiwdin

I have an array that stores in a session variable. However I want to send only one part of the array in the SESSION variable to another page depending on which button is clicked.

The code for the button

foreach($name as  $bookname => $name){
        echo "<div class='control-group' align='center'><h4>$bookname</h4><a href='/handler' class='btn btn-large btn-block btn-primary' type='button'>follow $name</a></ul></div>";    
        $_SESSION['name'] = $name;
        $_SESSION['bookname'] = $bookname


The print_r gives me each in the array, what I want to do is when the button is clicked only the $name for that button is set as the $_SESSION. The way I have it now it sets the SESSION for only the last part of the array.

Answer by Starx

It works for only last part, because of the loop overrides every preceding value.

If you want to set the value in SESSION after it is clicked, you have to send the request to a different page (HTTP GET or POST request) or an AJAX request to set the values.

April 29, 2011

PHP Session ID the same but variables are lost

Question by Nick

I have a login page that sets session variables upon successful login. The login page then redirects to an admin page. The admin page is able to read session variables just fine. But when I do a jQuery load() function that loads viewUsers.php in the content div the session variables are gone. The weird part is the session id is the same.

I used var_dump() on both the admin page and the viewUsers page. The admin pages shows all the proper variables from the login page but the viewUsers page which is called with a jQuery load() function var_dump of $_SESSION is blank. var_dump of $_COOKIE[‘PHPSESSID’] has the proper ID though, it doesn’t make any sense to me.

This is how I set the session variables.

$_SESSION['userID'] = $userInfo['ID'];
$_SESSION['userType'] = $userInfo['userType'];
$_SESSION['programID'] = $userInfo['programID'];

This is the jQuery


All pages have session_start() at the very top. The session variables also didn’t work when I tried and window.location instead of a jQuery load() function.

Some how the session variables are getting lost even though I have the correct session id. If anyone could shed any light on this I would really appreciate it!

As of right now I’m populating hidden fields and using a post function instead of load to get around it. I understand this isn’t the best way, but it’s the only way I could figure out how to do it.

Here is the top of the index which read the session variables fine.

        header("location: ../login/index.php");

Here is the entire viewusers


//foreach($_POST as $name => $value){
    //$_SESSION[$name] = $value;

//echo " session id " . $_COOKIE['PHPSESSID'];

<?php require("adminFunctions.php"); ?>

<h2>View Current Users</h2>
<?php require("userlinks.php"); ?>
<table id="userTable" class="tablesorter">
    <?php getUsers($_SESSION['programID'], $_SESSION['userType']) ?>
<script type="text/javascript">


    //$("#userTable th").click(function(){

    function colorTable(){
        $("tr:odd").css("background-color", "#c0c0c0");
        $("tr:even").css("background-color", "#ffffff");

    function color(){
        $("tr:odd").css("background-color", "#ffffff");
        $("tr:even").css("background-color", "#ffffff");

        $("#userTable").tablesorter({widgets: ['zebra']});


Another Edit:
Here is the javascript code to load viewusers
The only reason I’m using post is because I set the session variables as hidden fields in order to pass session variables. On viewusers I use a foreach loop to set the session variables. I understand this isn’t secure.
function maintainUsers(){

$.post("viewUsers.php", $("#sessionform").serialize(),function(data){

Answer by Starx

Normally, this sort of error occurs when you dont use session_start() to read the session data.

Try placing (Although, you said you have, I would suggest rechecking)


At the beginning of your viewUsers.php

In case, the above is not the case, thenyour current page (the one from which you execute the .load() function) is resetting the session and tampering with the values. Unless you upload the code or find it out, there is no solution for this case.

November 18, 2010

Using form contents to add values to $_SESSION array

Question by BigRob

I’m trying to make a page that takes a form submission and adds the contents as a new value in the $_SESSION() array, what seems to be happening though is the value is being overridden.

The form has 3 text inputs named a, b and c and refreshes the page on submission. What tells me it’s being replaced is $_SESSION[0] will display 1 2 and 3 as defined below, then the next row defined by $_POST will be the same but with the array values replaced by the last submitted values rather than adding the last submitted as another row.

<form action="test2.php" method="post">
<input type="text" name="a">
<input type="text" name="b">
<input type="text" name="c">
<input type="submit" value="Submit">

    if (isset($_POST['a']))
     $a = $_POST['a'];
     $b = $_POST['b'];
     $c = $_POST['c'];
     $order = array('a' => $a, 'b' => $b, 'c' => $c);
     $_SESSION[0] = array('a' => 1, 'b' => 2, 'c' => 3);
     $_SESSION[] = $order;
     $count = count($_SESSION);
     for ($i = 0; $i < $count; $i++) {
      echo "w: " . $_SESSION[$i]['a'] . "n";
      echo "h: " . $_SESSION[$i]['b'] . "n";
      echo "p: " . $_SESSION[$i]['c'] . "n";
      echo "<br />";

Would be extremely grateful for any help,

Answer by Nick Pyett

It looks to me like you are trying to add a new array to a new $_SESSION var each time the form is submitted. The method you are using will only add the value to the $_SESSION array for that page load – it won’t actually be in the $_SESSION array! Confusing right? So either of these won’t work…

$_SESSION[] = 'value or array';
$_SESSION[1] = 'some other stuff';

But this will, due there being text in the $_SESSION key (and don’t forget to start the session).

$next = count($_SESSION) + 1;
$next = 'foo' . $next;
$_SESSION[$next] = 'bar' . $next;

This will generate the below for “print_r($_SESSION)”.

Array ( [foo1] => barfoo1 [foo2] => barfoo2 [foo3] => barfoo3 [foo4] => barfoo4...

Answer by Starx

Most simplest way of adding form values would be

$_SESSION['form'] = $_POST; //once the form is posted

Then access the values using


Please fill the form - I will response as fast as I can!