0

UPDATE2:

i tried this piece of code, but still no echo, i valued my cookie to 3 for just test

    <?php ?>
<script type="text/javascript">
    date = new Date();

    ms = (date.getHours() * 24 * 60 * 1000) + (date.getMinutes() * 60 * 1000) +      (date.getSeconds()     * 1000) + date.getMilliseconds();
    alert(ms);
    document.cookie += 'test=3';

</script>

    <?php 
    echo $_COOKIE['test'];

?>

UPDATE1:

i tried this: did i do it right? it did not echo anything.

<?php ?>
<script type="text/javascript">
    date = new Date();

    ms = (date.getHours() * 24 * 60 * 1000) + (date.getMinutes() * 60 * 1000) +      (date.getSeconds()     * 1000) + date.getMilliseconds();
    $.get("test.php", { test: ms } );
</script>

    <?php 
    echo $test;

?>

ORIGINAL POST:

well basically i'm on a site with cURL, but everytime i want to change the location where i am, i need to post an extra variable ms to the site..so for example

POST /chat.php?cg=0&ms=23585666

the ms value comes from javascript, but how i get the value to my PHP code to send it out with cURL post?

<script type="text/javascript">
date = new Date();

ms = (date.getHours() * 24 * 60 * 1000) + (date.getMinutes() * 60 * 1000) +      (date.getSeconds()     * 1000) + date.getMilliseconds();
</script>

so eventually it goes to my cURL postfields

$data = array('ms' => $VARIABLE_MS_FROM_JAVASCRIPT, 'cg' => 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
3
  • Why don't you just calculate it in PHP? Commented Feb 22, 2011 at 14:26
  • Seems like you need to explain what you're doing with that date in more detail as there is some confusion as to whether it is significant to use the value from the client (browser or JavaScript) or if you can just re-calculate the current date in PHP upon execution of your PHP script. Commented Feb 22, 2011 at 14:47
  • simpiest way i can put it, i need to post the value the right ms value to the site, or it wont allow me to curl through the site Commented Feb 22, 2011 at 15:02

4 Answers 4

2

javascript is always executed after server-side php code. you can make an ajax request in Javascript for example with jQuery:

<script type="text/javascript">
date = new Date();

ms = (date.getHours() * 24 * 60 * 1000) + (date.getMinutes() * 60 * 1000) +      (date.getSeconds()     * 1000) + date.getMilliseconds();


$.get("doCURL.php", { VARIABLE_MS_FROM_JAVASCRIPT: ms } );
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

i tried it , im not sure if i did it right, but did not work, i added it to main post
you will not see any echo, except if you use the firefox extension firebug. There you can see all XHR Requests in the Network Tab.
firebug says $ is not defined on the line $.get("test.php", { VARIABLE_MS_FROM_JAVASCRIPT: x } );
you must include jquery to use $
ok it worked now, but it seems to post the ms value to the doCURL.php right? so how i get the value from doCURL.php..i tried something like this ` $ms = $_GET("test"); echo $ms;` .. but it said ` Function name must be a string `
0

That is some stupid javascript. It could just be

ms=new Date().getTime();

But no matter, all you have to do is use an equivalent time function in PHP such as

$ms=microtime(true)*1000;

2 Comments

that $ms does not equal the ms from javascript.
facepalm. I see my mistake. The original javascript is just counting milliseconds from the day, not from jan 1st 1970, sorry for my ignorance.
0

the fastest and easiest way to do it is to save the value in a cookie:

document.cookie += 'aCookieName=your_javascript_number'

now, in PHP, to get it, just do

echo $_COOKIE['aCookieName'];

now, every time the user unloads(refreshes the page, run the above javascript code to update the cookie)

document.body.onUnload = function(){ 
   document.cookie += 'aCookieName=your_javascript_number'
}

but... as far as i see... you only need the current time from the user... With javascript you only get the MACHINE TIME - which is the time of the clock on his operating system that changes once he changes the time. It is a lot better (if u can) to just get it from php via the php date() function

2 Comments

i tried this, but it did not echo anything. look main post , under UPDATE2
i ran it with firebug and it says document.body is null on the line document.body.onUnload = function(){
0

Your Update2 isn't going to work at all.

Even though the javascript comes before the php, it's not executed before. It's not even executed on the same machine!

PHP treats that javascript block just like plain text; it just sends it to the browser as-is.

So PHP sees your code as:

<?php ?>
A bunch of stuff I don't care about

<?php 
echo $_COOKIE['something which doesnt exist'];
?>

It seems that you are using cURL to scrape a chat site, right? You're not trying to code the site?

If that's the case, then this is all the php code you need:

$ms=time();

And then just use that in your curl setopts.

Except that won't work, because the javascript is doing some wacky stuff with the time. For example, it's multiplying the number of hours by 24 * 60 *1000 to get milliseconds, I assume, but that's wrong.

As I'm typing this, it's 9:42am local time. 9 hours is 9 * 60 * 60 * 1000 = 32,400,000ms.

(9 hours * 60 minutes per hour * 60 seconds per minute * 1000 ms per second)

If you're in charge of this javascript, you should probably fix it to create a proper timestamp to avoid these headaches :)

But in the spirit of answering the question as presented, this php code will duplicate the (broken) time math in the javascript:

$ms = round(date('h',$sec)*24*60*1000 + date('i',$sec)*60*1000 + date('s',$sec)*1000+$usec*1000);

4 Comments

no, i am not doing my own website, im using curl to move around other persons website...well im trying to do something like that: check out my other question. maybe you get the idea ... stackoverflow.com/questions/5080212/advanced-curl-and-php ... if you have any more questions, just ask, i really need to get it working...btw ur code gave me this...on the alert box there is the javascript number, and behind it, is the php number you gave me, they should be same, but they arent PICTURE OF WHAT HAPPEN
They're never going to be the same. The server's got a different clock than the browser, and even if you were browsing from the server, the instructions to get today's date in milliseconds would take place at different times.
well im pretty sure it is not getting the value from server..its getting it from my computer with javascript
The PHP is executed on the server. The javascript is executed on the browser. This is why the times never match.

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.