A php session is created at the page load on the server.
so there is no way to create the session after the page has loaded.
so you have only one/2 solution if you don't want to use ajax.
solution 1
sess.php
<?php
session_start();
$_SESSION['time']=$_GET['time'];
?>
js
head.appendChild(document.createElement('script')).src='sess.php?time='+offset;
note:head is only a reference in this case.
BUT
this is also async.
so again. there is no way to set the session after the page has loaded.
solution 2
preload a image outputted by php with gd that also set's your session. ;)
if you want to know how i can look into it.(this would cause a natural page load & would prolly work also on ie6 )i just don't remember exactly how to preload images before the body does.
I had the same problem some time ago ... i use ajax.
function ajax(a,b,c){ // Url, Callback, just a placeholder
c=new XMLHttpRequest;
c.open('GET',a);
c.onload=b;
c.send()
}
function LoadThePage(){
//just some milliseconds passed.
//session is set
//load the rest of your page.
//MOST OF THE TIME this loads faster than the body does.(window.onload)
}
ajax('sess.php?time='+offset,LoadThePage);
at the other side ... now most modern browsers support localstorage.
all phones & the modern browsers.
if i store things like that. why use a php session?
localstorage is made to replace that.
window.localStorage['offset']=((new Date().getTimezoneOffset()) * -1) * 60;
var time = '<?php echo $_SESSION['time']; ?>';
works for me but I guess it's a bad practice? – I Can Has Kittenz Jan 6 at 10:06ajax
for assigning javascript value into PHP variable – Krish R Jan 6 at 10:09