Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Php + Js = ???

  1. #1
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Php + Js = ???

    I'm wanting to add the ability to fputs($fp the visitors' browser information like available screen width, colours, resolution, etc.

    I've searched all over and can't figure this out. Somehow there's a way to send it from JS into PHP?

    Any ideas on this? Extreme Tracking does this quite well, I'm looking for something simple, or starter tips on where to look for this information.

    Thanks.
    <div> - putting your mind in a box since 1997

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Create cookies using javascript then use php to read the cookies. For example save this page as test.php
    PHP Code:
    <script type="text/javascript">
    function createCookie(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    }

    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }

    function eraseCookie(name) {
        createCookie(name,"",-1);
    }
    createCookie('screenWidth',screen.width,9999);
    createCookie('screenHeight',screen.height,9999);
    </script>
    <?php
    if(isset($_COOKIE['screenWidth']))
    {
    echo 
    "User screen width: ".$_COOKIE['screenWidth']."px<br>";
    echo 
    "User screen height: ".$_COOKIE['screenHeight']."px<br>";
    }
    else
    {
    echo 
    "There is no cookie set.";
    }
    ?>
    Navigate to the page at least once and then refresh the page. PHP will echo the cookie created by javascript. Hmm the problem I see with this is the cookies aren't read by php until the next page load.
    Last edited by _Aerospace_Eng_; 03-05-2007 at 01:30 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You could also use XMLHTTPRequest (aka AJAX), but as the previous poster stated, anything other than just recording stats is useless because it wouldn't apply until the next pageload.

  • #4
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Cookies would be cool - I have yet to play with those. But, yes the problem would be of those people who only look at my first page.

    Here's a sample of Extreme's code:

    <div id="eXTReMe"><a href="http://extremetracking.com/open?login=momav21" target="_blank">
    <img src="http://t1.extreme-dm.com/i.gif" style="border: 0pt none ;" id="EXim" alt="eXTReMe Tracker" height="38" width="41"></a>
    <script type="text/javascript"><!--
    var EXlogin='momav21' // Login
    var EXvsrv='s10' // VServer
    EXs=screen;EXw=EXs.width;navigator.appName!="Netscape"?
    EXb=EXs.colorDepth:EXb=EXs.pixelDepth=;
    navigator.javaEnabled()==1?EXjv="y":EXjv="n";
    EXd=document;EXw?"":EXw="na";EXb?"":EXb="na";
    EXd.write("<img src=http://e1.extreme-dm.com",
    "/"+EXvsrv+".g?login="+EXlogin+"&amp;",
    "jv="+EXjv+"&amp;j=y&amp;srw="+EXw+"&amp;srb="+EXb+"&amp;",
    "l="+escape(EXd.referrer)+" height=1 width=1>");//-->
    </script><img src="http://e1.extreme-dm.com/s10.g?login=momav21&amp;jv=y&amp;j=y&amp;srw=1280&amp;srb=32&amp;l="
    height="1" width="1"><noscript><div id="neXTReMe"><img height="1" width="1" alt=""
    src="http://e1.extreme-dm.com/s10.g?login=momav21&amp;j=n&amp;jv=n" />
    </div></noscript></div>
    From what I can tell, it captures the information on the first pageload. I see the parts I want, but don't know what's going on in that script.

    And it's not just resolution and colour I'd look for, it's also the other visitors' information I can display easily with JS.
    Last edited by ArcticFox; 03-05-2007 at 03:00 AM.
    <div> - putting your mind in a box since 1997

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Hmm it looks like the image is a dynamic image and sends stuff back to the server. Look into creating dynamic images with php.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •