#1
  1. funky munky
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2001
    Location
    UK
    Posts
    1,446
    Rep Power
    16

    Resolution Sniffing with PHP and Javascript


    hey folks,

    I've been trying to write a script to track hits of what screen resolutions ppl are using - after searching in this place I found a few similar threads but nothing that sorts me out...

    See I'm using the script below to grab the screen resolution into a variable $resolution and pass $resolution to the popup url within it's query string. From there it is added to the db - or would be if it worked!

    The problem is that the string passed to the popup url, $screenRes is actually "<SCRIPT language='javascript'> etc..." and not "1024","800" etc... Not the desired effect!

    How do I sniff the resolution and pass the resolution to a script using forms and hidden fields?

    ######
    <html>
    <head>
    <?
    $resolution = "<SCRIPT language='javascript'>
    <!--
    if (screen.width) {
    document.write(screen.width);
    } else {document.write('undefined')}
    //-->
    </SCRIPT>";
    ?>
    </head>
    <body onload="window.open('/games/falcon/index.php?screenRes='<?PHP print $resolution; ?>')" bgcolor="#ffffff">
    <!-- THIS BIT DOES PRINT OUT EITHER 1024,800 ETC -->
    <!-- WITHIN THE GENERATED PAGE -->
    Your resolution is <?=$resolution?>.
    <!-- BUT PASSING $RESOLUTION IN QUERY STRING DOESN'T!!! -->
    </body>
    </html>
    ######

    thanks in advance
    <:-)>
    Last edited by munkfish; July 2nd, 2001 at 08:40 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Sydney
    Posts
    41
    Rep Power
    14
    You don't seem to understand PHP and javascript.

    PHP only works on the web server.
    javascript only works on your browser.

    You are trying to run the javascript on the server. This will not work.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Rego Park, NY (Queens)
    Posts
    1
    Rep Power
    0
    sparki is correct. PHP is parsed on the server side before it is sent to the client. The client then parses whatever JavaScript it sees. Therefore, PHP and JavaScript cannot talk to each other directly.

    From what I can see munkfish, you just want to pass the resolution into some other page where something will be done to with the values. You can simply use JavaScript itself to generate the page. For example:

    ####
    <html>
    <head>
    <title>Short Screen Resolution Example</title>
    <script language="JavaScript" type="text/javascript">
    <!--
    var winURL = "window.php?screenwidth=" + screen.width + "&screenheight=" + screen.height;
    var bodyTag = "<body onload=\"window.open(winURL);\">";
    // -->
    </script>
    </head>
    <script language="JavaScript" type="text/javascript">
    <!--
    document.write(bodyTag);
    // -->
    </script>
    </body>
    </html>
    ####

    The JavaScript will do the work here, and then open a window up. I broke the variables down so you can view it a little bit more easily. Hope that helps!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    New Zealand
    Posts
    167
    Rep Power
    14

    this might help...


    It detects the screen resolution, passes it to the popup page - and from there you can add it to your database if you wish.

    -- main page --

    <html>
    <head>
    <title>Homepage</title>
    <SCRIPT LANGUAGE="Javascript">
    function widthFunction()
    {
    var screenw = screen.width;
    var url = "popup.php?&resolution=" + screenw;
    window.open(url);
    }
    </SCRIPT>
    </head>
    <body onLoad="widthFunction()">
    Blah blah
    </body>
    </html>

    -- popup page --

    <?php

    if ($resolution)
    {
    echo "your screen resolution is $resolution";
    }
    ?>

    -- end scripts --

    Hope that is of some help.

    Cheers,

    Jen
  8. #5
  9. funky munky
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2001
    Location
    UK
    Posts
    1,446
    Rep Power
    16
    Thanx phreq - that looks like it'll do the job fine. The problem I have is with javascript - not my favourite language, but am learning to like it!

    Chris thanx too, a variation on the theme...

    OK - job done!

    Heres how I did it (in case anyone else wants to know!!!):

    User loads the index page for the site:
    PHP Code:
    <!-- index.php -->
    <
    html>
    <
    head>
    <
    title>Homepage</title>
    <
    SCRIPT LANGUAGE="Javascript">
    <!--
    function 
    widthFunction()
    {
    var 
    screenw screen.width;
    var 
    url "/games/mario2/index.php?resolution=" screenw;
    window.open(url);
    }
    -->
    </SCRIPT>
    </head>
    <body onLoad="widthFunction()">
    Blah blah
    </body>
    </html> 
    (Many thanks phreq)
    So I have the regular stuff I want in the index page here, and the index.php script also opens "/games/mario2/index.php", including the resolution in the query string.

    For now I have the game file "/games/mario2/index.php" include the counter code (I'll make a random game popup script later!!!) as follows:

    PHP Code:
    <?php
    // THIS FILE IS "/games/mario2/index.php" !!!
    // This script logs the user's browser resolution into a db, then 
    // continues to load the game afterwards...

    // Include files:
    // ALL THE INC FILES IN HERE!
    define("INC_DIR""/home/sites/site117/web/include/");
    // DB CONNECTION DONE HERE:
    include(INC_DIR."dblib.inc");
    // COUNTER FUNCTIONS IN HERE (LISTING BELOW):
    include(INC_DIR."counter.inc");

    // Check the resolution passed:
    if($resolution!="1024" && $resolution!="800" && $resolution!="640"){
        
    $resolution="other";
    }

    // Update the db:
    resCounter($resolution);

    // END OF RES COUNTER - GAME FOLLOWS:
    ?>
    <HTML>
    <HEAD>
    <TITLE>Marionette 2</TITLE>
    </HEAD>
    <BODY>
    etc
    </BODY>
    </HTML>
    And for completeness(!), this is the
    PHP Code:
    rescounter($resolution
    function from the include file counter.inc':

    PHP Code:
    // This function takes the resolution passed to it and enters it into the db:
    function resCounter($resolution){
        global 
    $link;

        
    // Build a query for insertion in counter table:
        
    $query="UPDATE counter "
            
    ."SET count=count+1 "
            
    ."WHERE type='resolution' and var='$resolution'";

        
    // Run the query:
        
    if(!($result=mysql_query($query$link))){
            die(
    "Error occured returning result handle, error was: ".mysql_error());
        }

    If anyone wants the MYSQL for the table 'counter' above just mail me!!!

    Thanx again for your help chris and phreq.

    Hope all that pasting helps someone else!!!
    Last edited by munkfish; July 3rd, 2001 at 01:39 PM.

IMN logo majestic logo threadwatch logo seochat tools logo