Rewrite the title as: Translate "Array as session variable" into Chinese as "Use array as session variable"
P粉833546953
P粉833546953 2023-08-21 18:07:56
0
2
489

In PHP, is it possible to set an array as a session variable?

The situation is that I have a table (first page) in which some cells have links to specific pages. The next page will have a list of names (the second page, which I want to save in a session array) with corresponding checkboxes. After submitting this form, it will lead to a transaction page (third page) where the value of the published checkbox will be saved in the database under the corresponding name. Now, if I go back to the first page and click on another cell, will the sessions array contain the new list of names or the old list of names?

P粉833546953
P粉833546953

reply all (2)
P粉318928159

Yes, you can put an array into the session, for example:

$_SESSION['name_here'] = $your_array;

Now you can use$_SESSION['name_here']on any page, but before using any session function, make sure to add thesession_start()line to your code, So your code should look like:

session_start(); $_SESSION['name_here'] = $your_array;

Possible examples:

session_start(); $_SESSION['name_here'] = $_POST;

Now you can get the field value on any page like this:

echo $_SESSION['name_here']['field_name'];

As for the second part of your question, unless you allocate different array data, the session variables will remain there:

$_SESSION['name_here'] = $your_array;

The session lifetime is set in thephp.inifile.

For more information please click here

    P粉166779363

    Yes, PHP supports arrays as session variables. Please refer tothis pagefor examples.

    As for your second question: Once a session variable is set, it will remain the same unless you change it orunsetit. So if the third page doesn't change the session variable, it will remain the same as it was before the second page changed it.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!