Recover session by sessionStorage

Asked

Viewed 237 times

0

Good evening, my question is very simple I would like to know how to display the username of the session on the console. I tried this way but only returns null.

<?php
    session_start();
    echo $_SESSION['usuario'] = 'Pedro';
?>

<!DOCTYPE html>
<html>
<head>
   <title>teste</title>
</head>
<body>
    <script type="text/javascript">
       var teste = sessionStorage.getItem('usuario');
    console.log(teste);
</script>
</body>
</html>
  • 2

    PHP session is one thing, JS sessionStorage is another completely different. One is on the server, one is on the client. In which of the two you stored the user name?

  • i stored it on the server, but I wanted to take the session data by sessionStorage

  • 1

    Impossible. Research the concepts of client-side and server-side.

  • So how would I recover the user name? Or there is no way ?

  • 1

    Through $_SESSION['usuario'], as you did in the beginning.

1 answer

0


You need to set the data that is currently in the php in the sessionstorage

sessionStorage.setItem('usuario', '<?php echo $_SESSION['usuario']?>');

After that you can take user data with

var teste = sessionStorage.getItem('usuario');
console.log(teste);
  • gave right, vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.