Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Storing arrays into Session Variables

    I know this is possible by using Serialize and unserialize functions. But I just can't understand its logic. I mean.. I saw an example of a session variable having a couple of values, that is through a select multiple form..

    <select name = products[] multiple>
    etc..

    then all values that the user selected in the combo box will apear in the session variable after he has transferred the data from the products[] to the serialize($_SESSION[products])..

    now what i would like to happen is storing an array into a session variable. The data will not be from a combo box.. but rather from different $_POST variables..

    can someone give me a sample code?

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You don't really need to serialize the data at all unless you have a specific reason to do so. If not, sessions can be viewed almost as arrays themselves and as such you can turn them into multi-dimensional arrays and treat them pretty much as you would any other array.

    For instance:
    PHP Code:
    session_start();

    $_POST['name'] = 'Bob';
    $_POST['email'] = 'bob@domain.com';
    $_POST['address'] = 'my house';

    $_SESSION['form_results'] = array('name' => $_POST['name'], 'email' => $_POST['email'], 'address' => $_POST['address']);

    echo 
    '<pre>';
    print_r($_SESSION);
    echo 
    '</pre>'
    This results in:
    Code:
    Array
    (
        [form_results] => Array
            (
                [name] => Bob
                [email] => bob@domain.com
                [address] => my house
            )
    
    )
    Last edited by delinear; 07-01-2005 at 12:44 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •