Encodes all the session data as a string.
This functions offers a convenient way of converting all of the data in a session to a string. This is useful for saving it in database, for example.
<?php
// start a session, make and encode some data
session_start();
$_SESSION["myvar"] = "Hello world!";
$session_data = session_encode();
// connect to a database
mysql_connect("localhost", "www", "www") or die("Could not connect");
mysql_select_db("phpsample") or die ("Could not select db");
$query_update = "UPDATE sessions SET data = '" . $session_data . "' WHERE sid ='" . session_id() ."'";
$query_insert = "INSERT INTO sessions VALUES('" . session_id() . "', '" . $session_data . "')";
// execute query
if (mysql_query($query_insert)) {
print "Session data inserted into db!<br>";
} else {
mysql_query($query_update) or die ("db query failed");
print "Session data updated in db!<br>";
}
// remove the session variable
print "Session with id " . session_id() . " saved!";
unset($_SESSION["myvar"]);
session_destroy();
?>Session data inserted into db!
Session with id 1979d74b7c83d8ba0b1cc5f84b938f6f saved!This code encodes a session and saves it in a MySQL database. Use it together with the code sample for session_decode().