0

I am trying to use the solution called in here: How to keep session alive without reloading page?

Unfortunately I can't get it to work, I have very limited experience with Javascript and jQuery.

This is my index.php

<?php
session_start();   
echo session_id();
$_SESSION['id'] = session_id(); //just so there is a variable within the session
?>

EDIT: added jquery library after comment/answer

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

<script>
setInterval(function(){
    $.post('refresh_session.php')‌​;
}, 60000);
</script>

And this is the refresh_session.php where I write to a file, so I can test if the file is actually being called.

<?php
session_start();
if (isset($_SESSION['id'])){
$_SESSION['id'] = $_SESSION['id']; // or if you have any algo.
}
$date = new DateTime();
$fp = fopen('data.txt', 'a');
fwrite($fp, $date->format('Y-m-d H:i:s') . " " . session_id() ."\n");
fclose($fp); 
?>

If I call refresh_session.php manually, I see the date showing up in data.txt. If I open up index.php and wait for the data.txt file to change, nothing happens.

What am I missing here?

7
  • Normally, PHP sessions require cookies, otherwise PHP can’t identify the session. Are you sure that $.post() is sending cookies? Commented May 7, 2017 at 7:52
  • any errors in console..?? also wrap your call within $(document).ready(); Commented May 7, 2017 at 7:52
  • Nothing wrong with your JS/jQuery code, you either are not giving the correct path of refresh_session.php or as @Manngo mentiond. Commented May 7, 2017 at 7:55
  • @RohitS I am getting an en error in the console "Uncaught SyntaxError: Invalid or unexpected token" wich refers to the $. according to the online Javascript tester. Commented May 7, 2017 at 8:02
  • @LvS that indicate you are trying to use jquey in your code..$ reffer to document object ...make sure you include jquery Commented May 7, 2017 at 8:03

1 Answer 1

2

I don't know why, but after copy-paste your javascript code – I've got strange characters in Code Snipped. It can be charset problem. Code looks good, but bracket only looks like bracket. It's not bracket. What it is? I don't know, but look at that what I've got in Code Snipped after pasting your code:

Strange characters that shouldn't be here

Code will execute if you write it using good charset. Take that working code:

setInterval(function(){
    $.post('refresh_session.php');
    alert("dsa");
}, 5000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

By the way – alert is of course only test, you can delete this.

So, the answer is – check your charset.

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

7 Comments

Yes! That is it. How did you check this with what tool? I had copied the code from a comment on the thread I had reffered to. Probably that gave me the illegal characters
While writing question or answer (in comment not) you can press leftCtrl+M that shows Code Snipped editor.
Cool, this is really something I would never have found out otherwise. Thanks.
this is really strange question and answer i have came across..how would somebody answer if he\she wont copy paste the exact code OP has posted...lol
@RohitS In "basic" codeblock (8 spaces) charset problem will be still invisible. Check it – that's true. So, at last, it's possible to copy paste code and think that bracket is... bracket ;)
|

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.