Search code examples
phpsessionauthenticationsession-storage

Saving sessions on log out


I am a beginner in PHP,trying to create a simple ecommerce site. I have a login/registration form and user is able to add items or choose options that will only affect his/her account. On logging out,im using session_destroy() hence everything returns back to default. I also tried using arrays so that I can save and add items in the session array:

 $_SESSION['user_info'] = array();
 array_push($_SESSION['user_info'],        $item1);

I'm stuck on how to log out the user and retain their info,in the logout.php file I'm unsetting the session like this:

 session_start();
 unset($_SESSION['user_info']);
 session_destroy();

But on logging back again,nothing is saved.I would gladly appreciate any help.


Solution

  • Nothing is saved because session_destroy() deletes the session data. Sessions are only intended to store data while the customer is active or logged in. Either use file_put_contents to save data with file_get_contents to retrieve the data, or create a database, such as MySQL.

    file_get_contents

    MySQL

    Save Session To Database