开发者

Javascript value to PHP, cURL

开发者 https://www.devze.com 2023-02-12 08:54 出处:网络
UPDATE2: i tried this piece of code, but still no echo, i valued my cookie to 3 for just test <?php ?>

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 c开发者_如何转开发hange 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);


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>


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;


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


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消