How to close a Session in PHP

Asked

Viewed 73 times

-5

I’m having trouble trying to destroy the sessions, I tried the code below, there is some way beyond that?

<?php
    session_start();
    session_destroy();
    header('Location: index.php');
?>

What happens is that the session variables are not destroyed, thus being able to rescue the values contained in it.

  • What do you mean by "end sessions"?

  • I am trying to make the user perform the "logoff"

  • Unfortunately, I don’t know what went wrong. When I do Logoff it performs redirection to the page "Index.php" but when I force access to another page like "Home.php", It enters the page usually as if I were logged in. <? php session_start(); session_destroy(); header('Location: index.php'); ? > This is my code for "logoff"

  • The question became clearer?

  • No, what was the "problem" you’re having?

  • The question has already been answered, I’m trying to make it clearer with the standards of the site. I’ll edit it again.

Show 2 more comments

3 answers

4

To log out you should use this in your PHP code.

session_destroy();

If you want to create a session you can do so in your PHP code:

session_start();

$_SESSION['usuario'] = "User1";

If you want to get some session value in your PHP code:

echo $_SESSION['usuario'];
  • When I do Logoff it performs redirection to the page "Index.php" but when I force access to another page like "Home.php", It enters the page usually as if I were logged in. <? php session_start(); session_destroy(); header('Location: index.php'); ? > This is my code for "logoff"

  • Try to make some changes: I will paste as a response.

3

Make the following change at Home.php in the first line of code:

<?php
    session_start();
    /* Aqui você está verificando se a sessão está setada,
     * quando ele fizer logoff, seu session_destroy fará com que ele não entre na página
     */
    if(!isset($_SESSION['logged_in'])){
        header('Location: index.php');
    }
?>

Then modify the login as well. Add this code if the login was successfully done:

...
$_SESSION['logged_in'] = true;
...

2


Try to make some changes:

On your "App Help Desk" page remove the excerpt:

session_destroy();

header('Location: index.php');

Create a page with the name: logoff.php

and paste the code below in logoff.php

session_destroy();

header('Location: index.php');

Browser other questions tagged

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