Niks
Niks

Reputation: 41

How to destroy session and log out

I have a login page and a main page. On the login page I am asking for Username and Password and on the main page I m displaying some values from the database. Problem is with the logout function. I am doing it like this:

login.php Page

<?php
session_start();

if (isset($_POST['submit'])) {
    //log in code and to go to main.php
}

if (isset($_REQUEST['logstate'])) {
    session_destroy();
}

?>

main.php Page

<a href="login.php?logstate=logout"><img src="logout.png" id="Logout"></a>

Now when I click on the log out image, it ends the session, but returns me with the URL: xxxxx.com/login.php?logstate=logout

I want it to return me the url: xxxxx.com/login.php

Upvotes: 0

Views: 1252

Answers (6)

Christian
Christian

Reputation: 504

You can try to redirect

<?php
session_start();

if (isset($_POST['submit'])) {
    //log in code and to go to main.php
}

if (isset($_REQUEST['logstate'])) {
    session_destroy();
    header("Location: xxxxx.com/login.php");
}

?>

Upvotes: 1

make this your logout.php file and use it as a link to logout button

    <?php 
    session_start();
    session_destroy();
    header("Location:yourlocation.php");
    exit;
    ?>

Upvotes: 0

Harshal
Harshal

Reputation: 3622

you can try this on logout

you can call the file logout.php on logout image and code is :

<?php 
session_start();
session_destroy();
header("Location:home.php");//change the filename where you want to redirect it after logout
?>

OR in your way :

if(isset($_REQUEST['logstate']))
{
  session_destroy();
  header("Location:home.php");//change the filename where you want to redirect it after logout
}

See the Session_destroy() Doc, Initialize the session is necessary while destroying the session.

Upvotes: 0

Awlad Liton
Awlad Liton

Reputation: 9351

Try it It will solve your problem:

 <?php
    session_start();

    if(isset($_POST['submit']))
    {
      //log in code and to go to main.php
    }

    if(isset($_REQUEST['logstate']))
    {
      session_destroy();
    header("location: xxxxx.com/login.php"); 
    exit();
    }

?>

Upvotes: 0

Fawzan
Fawzan

Reputation: 4849

Just redirect the user after destroying the session.

<?php
header('Location: http://www.example.com/login.php');
exit;
?>

Upvotes: 0

Alex Siri
Alex Siri

Reputation: 2864

You need to do a redirect

if(isset($_REQUEST['logstate']))
{
  session_destroy();
  header( 'Location: /login.php' ) ;
}

Upvotes: 0

Related Questions