I have the $user array that contains data with special characters. It seems like each element of $user that contains special characters can’t render properly after they are stored in a session.
Here is my code:
<?php
session_start();
include_once('../application/classes/DataLayer.class.php');
$dl = new DataLayer();
$user = $dl->doLogin($_POST['email_sub'], $_POST['password_sub']);
if(isset($user)) {
foreach($user as $detail_array => $detail){
$fn = html_entity_decode($user['fn']);
$ln = html_entity_decode($user['ln']);
}
var_dump($fn, $ln); // $fn and $ln display well here
$_SESSION['user'] = $user;
$_SESSION['fn'] = $fn;
$_SESSION['ln'] = $ln;
var_dump($_SESSION['fn'], $_SESSION['ln']); // $_SESSION['fn'], $_SESSION['ln'] display well here too
}
else {
//do something here
}
?>
Any help would be appreciated. Sorry for my bad english.
Use the function from this link https://stackoverflow.com/a/8454838/997178 to encode the data for output
Use encode_output_vars function , param $vars is your session data … return value will be a single element encoded for output or an array with all elements encoded , depending what your parameter is .
Or just use php function htmlentities for your session data before you output it . Here is link http://php.net/manual/en/function.htmlentities.php