Current Location: Home> Latest Articles> How to use the serialize function to store and pass data in a PHP session (Session)?

How to use the serialize function to store and pass data in a PHP session (Session)?

gitbox 2025-05-27

In PHP, Session is a common way to store and pass user data between different pages. By using Session , we are able to maintain state between multiple requests from the user and the server, avoiding the need to recalculate or get the same information for each request.

Sometimes, we need to store complex data structures, such as arrays or objects, into a session. In PHP, the serialize function is very useful, which converts complex data types into strings, allowing this data to be stored in a session and restores the original data structure in subsequent requests.

Use serialize function to store data in PHP sessions

The serialize function can convert an array or object into a string, allowing it to be stored in a session (Session). Conversely, the unserialize function restores the string to the original array or object.

Here is an example of how to use the serialize function for data storage and passing in a PHP session.

 <?php
// Start a conversation
session_start();

// Create a complex array
$data = [
    'username' => 'john_doe',
    'email' => 'john.doe@example.com',
    'preferences' => ['theme' => 'dark', 'notifications' => 'enabled']
];

// use serialize Function converts array to string
$serialized_data = serialize($data);

// Store serialized data into a session
$_SESSION['user_data'] = $serialized_data;

// Print stored data
echo 'Data has been serialized and stored in session.<br>';

// The delivered content can be accessed through links
echo '<a href="https://gitbox.net/user_page.php">Go to user page</a>';
?>

In the above code, we first create an array containing user information, then use the serialize function to convert it into a string and store the string in the $_SESSION hyperglobal array. This way, when the user visits other pages, the data will be able to be persisted.

How to recover data

When the page is loading, we can use the unserialize function to restore the stored serialized data. Here is an example of how to do this.

 <?php
// Start a conversation
session_start();

// Check whether user data is stored in the session
if (isset($_SESSION['user_data'])) {
    // Get serialized string data from a session
    $serialized_data = $_SESSION['user_data'];

    // use unserialize Function restores it to the original array
    $data = unserialize($serialized_data);

    // Print recovered data
    echo 'Username: ' . $data['username'] . '<br>';
    echo 'Email: ' . $data['email'] . '<br>';
    echo 'Theme: ' . $data['preferences']['theme'] . '<br>';
    echo 'Notifications: ' . $data['preferences']['notifications'] . '<br>';
} else {
    echo 'No user data found in session.';
}

?>

In this example, we first check whether user data is stored in $_SESSION . If it exists, we use the unserialize function to restore the serialized string to an array and output the relevant user information.

summary

By using serialize and unserialize functions, PHP developers can easily store complex data structures into sessions and recover this data when needed. This approach is very useful for applications that need to pass data across multiple pages, especially if users need to keep their status or preferences after logging in.

When using Session to store data, pay attention to protecting the security of the session and avoiding session hijacking and data breaches.