session_encode php – PHP Session Encode Decode

session_encode php functions offers a convenient way of converting all of the data in a session to a string. The “session_encode()” function encodes the current session data as a string.

PHP session_encode() Function – Syntax

session_encode(void)

session_encode php – Session Encode Decode functions in PHP

  • session_encode()
  • session_decode()

What is session_encode Function?

session_encode php – session handling is a way to make the data available across various pages of a web application.

Free Live Chat for Any Issue

PHP Session Encode Decode Example

<?php
session_start();
$_SESSION["computer_code"] = "8888";
$_SESSION["logged_in"] = "yes";
$dt_encoded_sss = session_encode();
print "<b>Encoded Session Data:<br/></b>";
print $dt_encoded_sss . "<br/><br/>";
// Changing session values
$_SESSION['computer_code'] = "2000";
$_SESSION["logged_in"] = "no";
// printing $_SESSION
print "<b>SESSION Array:<br/></b>";
print "<pre>";
print_r($_SESSION);
print "</pre>";
session_decode($dt_encoded_sss);
// printing Reloaded $_SESSION
print "<b>Reloaded SESSION Array:<br/></b>";
print "<pre>";
print_r($_SESSION);
print "</pre>";
?>

Results:

Encoded Session Data:
computer_code|s:4:"8888";logged_in|s:3:"yes";
Changed SESSION values:
Array (
    [computer_code] => 2000
    [logged_in] => no
)Reloaded SESSION Array:
Array(
    [computer_code] => 8888
    [logged_in] => yes
)

Example :

<?php
session_start();
if (!$counter) {
$counter=10;
session_register("counter");
}
echo $counter;
echo"<br>";
echo session_encode();
?>

Top 50 PHP Function Example

Also Read This ๐Ÿ‘‰   install PHP Accelerator in Xampp - prestashop

PHP – Cookies and Sessions – session_encode()

<?php

// start a session, make and encode some data
session_start();
$_SESSION["product"] = "Welcome To Pakainfo!";
$session_data = session_encode();

// connect to a database
mysql_connect("localhost", "www", "www") or die("Could not connect");
mysql_select_db("pakainfi_v1") 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["product"]);
session_destroy();

?>

Results

Session data inserted into db!
Session with id 2459d55b7c83d8ba0b1bb5f84b895f6f saved!

I hope you get an idea about session_encode php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.