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?

Community
  • 1
  • 1
LvS
  • 507
  • 1
  • 4
  • 19
  • Normally, PHP sessions require cookies, otherwise PHP can’t identify the session. Are you sure that `$.post()` is sending cookies? – Manngo May 07 '17 at 07:52
  • any errors in console..?? also wrap your call within $(document).ready(); – RohitS May 07 '17 at 07:52
  • Nothing wrong with your JS/jQuery code, you either are not giving the correct path of `refresh_session.php` or as @Manngo mentiond. – divy3993 May 07 '17 at 07: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. – LvS May 07 '17 at 08:02
  • @LvS that indicate you are trying to use jquey in your code..$ reffer to document object ...make sure you include jquery – RohitS May 07 '17 at 08:03
  • I have included the jquery after that, that was not enough. The answer @Sylogista gave, is the solution. Thanks all, for the fast responses! – LvS May 07 '17 at 08:07
  • cheers...just a suggestion in your next post consider adding the character code ref...coz just looking at snippet without copying nobody can rectify the problem..as just what answerer did...cheers – RohitS May 07 '17 at 08:13

1 Answers1

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.

Sylogista
  • 565
  • 3
  • 10
  • 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 – LvS May 07 '17 at 08:05
  • While writing question or answer (in comment not) you can press leftCtrl+M that shows Code Snipped editor. – Sylogista May 07 '17 at 08:07
  • Cool, this is really something I would never have found out otherwise. Thanks. – LvS May 07 '17 at 08:08
  • 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 May 07 '17 at 08:10
  • @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 ;) – Sylogista May 07 '17 at 08:20
  • In my text editor (netbeans), those weird characters where not even visible... If they were visible, I would have noticed. It was just really weird.... – LvS May 07 '17 at 08:21
  • That reminds me simple joke with replacing semicolon with... greek question mark :D That's lovely! – Sylogista May 07 '17 at 08:23