Jump to content

[SOLVED] Saving Session Info


TheJoey

Recommended Posts

Just wondering if there is a way to parse information stored in session and put it into a .txt file.

 

echo '<pre>';
print_r($_SESSION);
echo '</pre>';

 

i get this information about my session

Array
(
    [logins] => 1
    [item] => item1
    [price] => 70
    [itemname] => Array
        (
            [item1] => item1
        )

    [itemqty] => Array
        (
            [item2] => 3
        )

    [itemprice] => Array
        (
            [item1] => 70
        )

    [qty] => 3
)

 

just wondering if this can be done and how i would go about doing so.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/177211-solved-saving-session-info/
Share on other sites

you can get the data from a session by using the session name and adding it to a variable like this:

 

$data = $_SESSION['item'];

 

Then you can save that information into a text file

 

$file = "session.txt";//set a name for a text file will be created if it does not exist
$fo = fopen($file, 'w');//open the file
fwrite($fo, $data);//write data there 
fclose($fo);//close file

Just wondering if there is a way to parse information stored in session and put it into a .txt file.

 

Yes. How you would do it would be entirely up to you and what order/structure you wished the output to have.

Edit: nuttycoder just showed the technique for doing so.

 

One thing I can say looking at what you've got, that doesn't appear to me to be the most sensible way of keeping the information in your $_SESSION array. I'd have thought the items would make more sensed stored like so...

 

<?php
$items = array(
array('name'=>"Name of item 1", 'price'=>"Price of item 1", 'quantity'=>"Quantity of item 1"),
array('name'=>"Name of item 2", 'price'=>"Price of item 2", 'quantity'=>"Quantity of item 2"),
array('name'=>"Name of item 3", 'price'=>"Price of item 3", 'quantity'=>"Quantity of item 3"),
);
?>

 

I guess thats by-the-by though.

The saving of the session works fine but it doesnt seem to get the information i want

 

Array
(
    [login] => 1
        (
            [item] => item
            [item1] => item1
            [item2] => item2
        )

    [itemqty] => Array
        (
            [item] => 16
            [item1] => 22
            [item2] => 23
        )

    [itemprice] => Array
        (
            [item] => 150
            [item1] => 60
            [item2] => 40
        )
)

 

thats how the array looks.

 

how do i extract all of the itemprice array because when i do it just echos 'array'

foreach($_SESSION['itemprice'] as $item_price) {
   echo '$'.$item_price."\r\n";
}
echo "\r\n";
foreach($_SESSION['itemqty'] as $itemqty) {
   echo '#'.$itemqty."\r\n";
}
echo "\r\n";
foreach($_SESSION['itemname'] as $itemname) {
   echo 'Name:'.$itemname."\r\n";
}

 

how would i make it display

itemname : item_price : itemqty

 

when i tried i just got a bunch of errors, i even tried joining the foreach's

IF you'd have changed the array to the format I suggested earlier you could have done...

 

foreach($_SESSION['items'] as $item){
   echo $item['name'] . ', ' . $item['price'] . ', ' . $item['quantity'];
}

 

Using the more confusing pattern you have, you would have to do something like...

 

foreach($_SESSION['itemname'] as $k=>$v) {
   echo $v . ', ' . $_SESSION['itemprice'][$k] . ', ' . $_SESSION['itemqty'][$k];
}

Of course it does, it doesn't apply to your code.

 

IF you'd have changed the array to the format I suggested earlier you could have done...

 

foreach($_SESSION['items'] as $item){
   echo $item['name'] . ', ' . $item['price'] . ', ' . $item['quantity'];
}

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.