ravikanth
ravikanth

Reputation: 61

session and cookie variables are stored without special characters

I am storing my variables which contain special characters like @#$(to describe a few)into session and cookies using PHP,but when I retreive them i am unable to see the special characters in the stored session and cookie variables.Thanks in advance.

                        //storing
                        $_SESSION['userid'] = $db_id;
            $_SESSION['email'] = $db_email;
            $_SESSION['password'] = $db_pass_str;
            setcookie("id", $db_id, strtotime( '+30 days' ), "/", "", "", TRUE);
            setcookie("email", $db_email, strtotime( '+30 days' ), "/", "", "", TRUE);
            setcookie("pass", $db_pass_str, strtotime( '+30 days' ), "/", "", "", TRUE); 

    //Retreivel
    if(isset($_SESSION["userid"]) && isset($_SESSION["email"]) && isset($_SESSION["password"]))
 {
    $log_id = preg_replace('#[^0-9]#', '', $_SESSION['userid']);
    $log_email = preg_replace('#[^a-z0-9]#i', '', $_SESSION['email']);
    $log_password = preg_replace('#[^a-z0-9]#i', '', $_SESSION['password']);
    // Verify the user`enter code here`

} 
else if(isset($_COOKIE["id"]) && isset($_COOKIE["email"]) && isset($_COOKIE["pass"]))
{
    $_SESSION['userid'] = $_COOKIE['id'];
    $_SESSION['email'] = $_COOKIE['email'];
    $_SESSION['password'] = $_COOKIE['pass'];
}

When I echo the session variables,I do not see the special characters which I stored. For eg: I stored [email protected] but when I view,I can see only testexample.com.

Upvotes: 0

Views: 2059

Answers (1)

Ronak Patel
Ronak Patel

Reputation: 390

Use htmlspecialchars to encode your original string. For example,

<?php
$session_variable = "hello@World#$%@";
$new = htmlspecialchars("<?php echo $session_variable; ?>", ENT_QUOTES);
echo $new; // output: hello@World#$%@
?>

Upvotes: 2

Related Questions