Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to set the php session $_SESSION['time'] based on user browser time

 // gives time difference between utc time and current user local time
var offset = ((new Date().getTimezoneOffset()) * -1) * 60;

I want this offset value to be stored in a session variable like

$_SESSION['time'] = offset ; // something like this

Example

 <script type="text/javascript">
   I have to set the $SESSION['time'] variable at the beginning of page
   so that i can use it down the dom
 </script>
 <span class="time_stamp"><?php echo $_SESSION['time'];?></span> 
share|improve this question
1  
Why in the world do you have to set the variable via Javascript when you want to output it using PHP afterwards? This fundamentally does not work. –  deceze Jan 6 at 10:06
    
Actually, 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:06
    
Sorry I have to set the server side session value from javascript –  niko Jan 6 at 10:08
    
You need to use ajax for assigning javascript value into PHP variable –  Krish R Jan 6 at 10:09
1  
@user2513523 Sure you can do that. Remember that - depending on purpose - this is a security issue if this timestamp is required to validate any specific information. Only use it if its only for local output and not if want this timestamp to be posted back in an Ajax request or form or similar. –  Steini Jan 6 at 10:10

1 Answer 1

up vote 0 down vote accepted

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;
share|improve this answer
    
This is what i was looking for @cocco thanks indeed –  niko Jan 6 at 10:50
    
i know it's a mess dealing with session & js... session is only good for '..secure' login data. –  cocco Jan 6 at 10:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.