weixin_33747129 2014-07-01 20:16 采纳率: 0%
浏览 20

用JavaScript触发PHP

I have a PHP process that get's a user's IP address, the page they are on, screen resolution and Date/Time on my website.

I want to put a line of code on my other website such as:

<script type="text/javascript" src="http://www.mywebsite.com/tracker.js"></script>

In this JavaScript file, it would post the information to a file called tracker.php which then processes the information to put in the database.

  • 写回答

1条回答 默认 最新

  • weixin_33674976 2014-07-01 21:03
    关注

    I assume you're asking how to send an asynchronous request for tracker.php, AKA AJAX. W3 schools has a guide on that here: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

    The IP address and Date/Time can be easily gotten without passing any information down from the website. If you want to pass information such as the page they are on and the resolution of their viewing area, then one very easy way to do it is to pass URL variables.

    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","tracker.php?WindowWidth=" + window.innerWidth +"&WindowHeight=" + window.innerHeight + "&Page=" + encodeURIComponent(document.URL) ,true);
    xmlhttp.send();
    

    These variables will be loaded into the tracker.php as the array $_GET

    $Width = $_GET["WindowWidth"];
    $Height = $_GET["WindowHeight"];
    $Page = $_GET["WindowPage"];
    

    There are more elegant solutions, such as $_POST which allows far more information to be passed down in a single request, or 1x1 tracking images. The Page they are on can also be acquired by using $_SERVER["HTTP_REFERER"] on tracker.php.

    评论

报告相同问题?