0

I want to destroy all the session values.I have logout button in all pages.I want to call this logout.php in two ways. 1.while clicked logout button in page. 2. set time for refresh a page after 10 minutes.

In header of my html I have the meta tag to refresh and call logout.php page.

<META HTTP-EQUIV="refresh" CONTENT="600;URL=logout.php?timeout">

logout.php

<?php
 session_start();
 // remove all session variables
 session_unset(); 
 $_SESSION = array();
// destroy the session 
 session_destroy();
 echo ("<SCRIPT LANGUAGE='JavaScript'>
 window.location.href='login.php';
  </SCRIPT>");  
 ?>

My problem is session is not destroyed but redirect to login.php.I have a code to redirect to home page if no session value in caught in the page.If I login it redirect to dashboard page.
For testing I copy dashboard link and paste it.Instead redirect to login page It shows dashboard page.

user3386779
  • 6,883
  • 20
  • 66
  • 134
  • 3
    For redirecting, you could use the php function "header" instead of injecting a script. See http://php.net/manual/en/function.header.php – Shishdem Mar 02 '15 at 11:19
  • 1
    How can you say that session is not destroyed and redirected to login page, when you refresh login page is it redirecting you to dashboard or else.. – Narendrasingh Sisodia Mar 02 '15 at 11:19
  • header is not supported so I cant use it – user3386779 Mar 02 '15 at 11:26
  • what do you mean with not supported? – Shishdem Mar 02 '15 at 11:29
  • header("Location: login.php"); is not working because more than one header to be sent.I have problem with this always – user3386779 Mar 02 '15 at 11:31
  • you can only use header if you have not sent anything back to the browser at all. check all your code for spaces in between php tags etc as this can often trip people up. – DevDonkey Mar 02 '15 at 11:58
  • To fix the headers already sent error, go to [this question](http://stackoverflow.com/a/8028987/3999944). It can be caused by a single whitespace or BOM. – numsu Mar 02 '15 at 11:58

2 Answers2

1

Try to add exit; to the end, this stops the page rendering which is no longer needed. And use the header('Location :'); function, find a way to fix the error you're getting here. This little snippet has always worked for me. Do a require('logout.php') to every page calling this code. The session_start(); should already be stated in the main file, so it doesn't need to be stated here.

if(isset($_GET["logout"])){
    $_SESSION = array();
    session_destroy();
    header("Location: /");
    exit;
}
Community
  • 1
  • 1
numsu
  • 472
  • 3
  • 10
-1

@numsu after using your code, Is not working Sir. Warning: Array to string conversion in C:\xampp\htdocs\Mayowa_Timetable\admin\logout.php on line 3 Array

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33023326) – Nadun Kulatunge Oct 31 '22 at 09:24