1

I am trying to call a php script which destroys session and reload a page after it.

$(document).bind("active.idleTimer", function(){
  $.post("data.php?data=active");
  location.reload();
});

data.php

if($_GET['data'] == 'active') {
  session_destroy();
}

It does not destroy the session, only if I manually open the url data.php?data=active it does, why is that? Thanks!

2
  • 1
    is there session_start() in your data.php? Commented Mar 12, 2013 at 14:27
  • I think you need to initialize the session with session_start() prior to destroying it Commented Mar 12, 2013 at 14:32

3 Answers 3

2

The problem is that the script you send the request to is using its own session. So, the user has a session, which is different from the session you send the "refresh idle state" request.

One solution would be to just start a timer using javascript and when that timer runs out, just refresh the page.

That's why it only works when you actually access the data.php page.

Sign up to request clarification or add additional context in comments.

5 Comments

To be honest i don't quite get this answer: Why should the script you send the request to use it's own session? Both share the same session cookies and thus use the same session. And even if: How would a timer help?
Because the browser (i.e. the user loading the page) is a different "client" to the server, than the script which sends a request.
Where did he mention, that he sends the request from another script? As i read it, the request is initiated from some javascript from the browser. So i'd assume it's the same session cookie.
He didn't have to mention, that's how a server works. Your browser, and the actual page (which resides on yet another server) are two different clients, each with its own session.
I meant "two different clients for the server"
0

If you want to use the data stored in the $_SESSION array, you need to start or resume the existing session using session_start(). In your data.php, try this:

if($_GET['data'] == 'active') {
  session_start();
  session_destroy();
}

Please, take in consideration that session_start() should be used before you send any output to the client.

Hope it helps!

Comments

-1

Modified:

$.post("data.php", {data: active}, function(whatever){} 

and in your php file use:

$_POST['data'];

5 Comments

That's not true. Even for POST requests, URL parameters can be read from $_GET.
in the php file, you need to grab the $_POST once you send it via ajax / jquery
he is using a querystring ?data=active as the data so $_GET works just fine
i suppose so, except i like to pair my ajax request in both the send (via post) and the grab (via post) just so there is no confusion
Good sir, you do realize your answer is wrong, right? Why do you persist with useless edits and additional details, that are also wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.