How to recover session data beyond ID

Asked

Viewed 77 times

-1

I am creating a session in PHP and taking her ID with cookie via javascript, however I would like to know how I do to get other data besides the ID, as the name of the session for example.

index.html:

<!DOCTYPE html>
<html>
<head>
   <title>teste</title>
</head>
<body>
    <form action="login.php" method="POST">
        Nome: <input type="text" name="nome"><br><br>
        <input type="submit" value="Login">
    </form>
</body>
</html>

login.php:

<?php
    if(!isset($_SESSION))
    {
        session_start();
    }
    $_SESSION['usuario'] = $_POST['nome'];
    $_SESSION['name'] = "Minha Sessao";
?>
<!DOCTYPE html>
<html>
<body>
    Usuário Logado:<p id='teste'></p>
    <script type="text/javascript">
        var usuario = document.cookie;
        alert(usuario);
        document.getElementById('teste').innerHTML = usuario;
    </script>
    <a href="logout.php">Logout</a>
</body>
</html>

logout.php:

<?php

  session_start();
  if(isset($_SESSION['sessao']))
  {
    echo 'Não existe uma sessão para ser encerrada';
  }else{                                                            
    header("location:index.html"); 
  } 

?>
  • 1

    Session is something unique to the server. You cannot access the session via JS, you will need to do this by PHP, as you did using $_SESSION.

2 answers

-1

Print everything on Session:

var_dump($_SESSION);

Then just use what you need.

-1

never use without initializing the session.

if(!isset($_SESSION))

The correct thing is

session_start();

if(!isset($_SESSION['usuario'])){

To check the contents of the standard PHP variables just print using

var_dump($_SESSION)

//ou

print_r($_SESSION)

You can also use with $_GET, $_POST, etc..

Browser other questions tagged

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