0

In this i am posting a question in which i am using a java script and PHP code and sending back the timestamp using the time function of the PHP. let have the code,

<?php
session_start();
echo time();
?>

<html>
<head>
    <title>my app</title>
    <script type="text/javascript" src="jquery-2.0.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $(this).mousemove(function(){
                var time_=new Date();

                var time=<?php echo time();?>;
                alert(time);
                $.post('loggout.php',{input: time});
            });
        });

    </script>
</head>
<body>
    <h2>we are on the main_session</h2>
</body>
</html>

now the problem is that when i move the mouse than the mousemove event gets into action and displays the value of the var time. but every time it displays the same value. the value only changes when i reload the page. so please let me know the reason behind it and how to make this dynamic

Anurag Singh
  • 727
  • 6
  • 10
  • 27
  • 2
    Send the timestamp "live" in Javascript, using [How do you get a timestamp in JavaScript?](http://stackoverflow.com/q/221294) (`$.post('loggout.php',{input: new Date().getTime()});` – Pekka Aug 01 '13 at 10:33
  • Why pass the time? You can not calculate the time in the file logout.php? – JellyBelly Aug 01 '13 at 10:34
  • dude there is a need i am making automatic log out after some time – Anurag Singh Aug 01 '13 at 10:37
  • 2
    @user so why not find out the current time *on the server*? That would make much more sense. Otherwise, I could simply prolong my session by sending you a faulty time stamp, which would be a security hole – Pekka Aug 01 '13 at 10:39

3 Answers3

0

This is because the PHP is only run once - when the page loads. So the Javascript time variable gets filled with the time returned by PHP and then never changes.

If you're happy to get the client-side time, just use this:

 var time = time.getTime();

Instead of var time=<?php echo time();?>;

Otherwise, you can use AJAX to send a query that'll run some PHP, return the time, and put it into the variable.

For example, you could try something like this:

$.get('getTime.php', function(data) {
    time = data;
});

And in getTime.php, just echo the time.

SharkofMirkwood
  • 11,483
  • 2
  • 17
  • 25
0

This is because PHP is back-end programing language and once your page loaded timestamp written to code and can't be dynamic. But you can send JS timestamp:

var time = Math.round(+new Date() / 1000);

( + will convert date Object to integer )

or

var time = Math.round(new Date().getTime() / 1000);

division by 1000 needed because JS use milliseconds.

See Date reference at MDN.

antyrat
  • 27,479
  • 9
  • 75
  • 76
0

put this javascript code anywhere in your php file.

<script type="text/javascript">    
    var currenttime = '<?php echo date("F d, Y H:i:s"); ?>' //PHP method of getting server date    
    var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
    var serverdate=new Date(currenttime)

function padlength(what){
   var output=(what.toString().length==1)? "0"+what : what
   return output
}

function displaytime(){
   serverdate.setSeconds(serverdate.getSeconds()+1)
   var datestring=montharray[serverdate.getMonth()]+" "+padlength(serverdate.getDate())+", "+serverdate.getFullYear()
   var timestring=padlength(serverdate.getHours())+":"+padlength(serverdate.getMinutes())+":"+padlength(serverdate.getSeconds())
   document.getElementById("servertime").innerHTML=timestring
}
window.onload=function(){
setInterval("displaytime()", 1000);
}
</script>

add span or div where you want to show current time. no need to reload page.

 <span id="servertime"></span>