Asked  9 Months ago    Answers:  5   Viewed   51 times

here is the code I am trying to execute, it was working fine on my localhost and another server PHP Version 5.3.2-1ubuntu4.11 but this creates problem in PHP Version 5.2.17 on a shared hosting

<?php
/* PHP version PHP Version 5.2.17   
 * Server API   CGI/FastCGI
 */
session_start();
$products = array();
if (!isset($_SESSION['products'])) {
    $products = array("somedata1", "somedata2", "somedata3");
    $_SESSION['products'] = serialize($products);
    $_SESSION['test_products'] = serialize($products);
    echo "<br/>session products<br/>";
    print_r(unserialize($_SESSION['products']));
    echo "<br/>session test_products<br/>";
    print_r(unserialize($_SESSION['test_products']));
} else {
    echo "<br/>session products<br/>";
    print_r(unserialize($_SESSION['products']));
    echo "<br/>session test_products<br/>";
    print_r(unserialize($_SESSION['test_products']));
}
?>

On first run outputs

session products
Array ( [0] => somedata1 [1] => somedata2 [2] => somedata3 ) 
session test_products
Array ( [0] => somedata1 [1] => somedata2 [2] => somedata3 )

but on reloading

session products

Warning: unserialize() expects parameter 1 to be string, array given in /home/uaustral/public_html/itoi/test.php on line 17

session test_products
Array ( [0] => somedata1 [1] => somedata2 [2] => somedata3 )

Same code works fine on my PHP version PHP Version 5.3.2-1ubuntu4.11

Am I missing something (that is automatically corrected in new PHP version) or is it a PHP bug for the old version ?

 Answers

82

If you work with register_globals enabled, any array-item in $_SESSION is also known as a variable by that key:

With register_globals on:

<?php
session_start();
var_dump($products);

Should show you the unserialized string. Because you later say $products = array(); you are implicitly altering $_SESSION['products']. Solution: disable register_globals, and on a side note: you don't need to serialize that data, a session can hold multi-dimensional arrays just fine. Just make sure to have any needed class-definitions loaded before calling session_start, or have an autoload function.

Wednesday, March 31, 2021
 
Tapha
answered 9 Months ago
16
foreach ($_SESSION["cart_array"] as $item_key => $each_item) {
    if ($item_to_adjust == $each_item["item_id"]) {
        $_SESSION["cart_array"][$item_key]["quantity"] = $quantity;
    }
}

This is still modifying the array inside the loop (not cool), but it does not mess with indexes.

Wednesday, March 31, 2021
 
Andres
answered 9 Months ago
52

Use array_merge() to achieve this:

$newArray = array();

foreach ($myArray['target2'] as $key => $innerArr1) {
    $newArray['target'][$key] = array_merge(
        $myArray['target1'][$key],  /* 0th and 1st index */
        array($innerArr1[1])        /* 2nd index         */
    );
}

print_r($newArray);

Output:

Array
(
    [target] => Array
        (
            [0] => Array
                (
                    [0] => 333333
                    [1] => 13
                    [2] => 99
                )

            [1] => Array
                (
                    [0] => 444444
                    [1] => 15
                    [2] => 98
                )

            [2] => Array
                (
                    [0] => 555555
                    [1] => 17
                    [2] => 97
                )

        )

)

Demo

Friday, May 28, 2021
 
Sidarta
answered 7 Months ago
49

The screenshot clarified it.

You should be able to use

$session['user']['firstName']

As noted in the comments above though, you will want to migrate away from using eval, which has security implications.

Saturday, May 29, 2021
 
o_flyer
answered 7 Months ago
51

You have the classic problem with filesystem-backed sessions. Your upload script locks the session backing file for its duration, so it's impossible to get access to the session information until it releases the lock.

There are several possible solutions here, but the easiest one would be for your upload script to periodically release the session and lock it again; this would leave opportunity for your progress script to read the session in the meantime.

To release the session lock, call session_write_close at any point from your upload script. This leaves you without access to session variables until you call session_start again later on. You can repeat this cycle as much as you like.

There are also other, more capable solutions. For example you could move the progress information to some storage mechanism that does not stay locked for the duration of a script; you can identify each user's information based on their session id (you don't need to start the session to get its ID, if one exists).

Sunday, August 15, 2021
 
edsk
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share