www.webdeveloper.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 32

Thread: PHP reloading scripts without reloading the page

  1. #16
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by Mindzai View Post
    Who said screen resolution is an issue? It's stats software. Analytics captures client screen resolution...
    So why does he want an iframe?

  2. #17
    Join Date
    Nov 2008
    Posts
    2,477
    Because he was given the (extremely bad) advice that he should not use AJAX to send the stats back to the server. If you read the very first line of first post:

    I'm writing Analytical software that uses javascript but mainly PHP to log things

  3. #18
    Join Date
    Jun 2009
    Location
    Cannock
    Posts
    30
    Quote Originally Posted by bokeh View Post
    If you wrote your html and css properly you wouldn't need to worry about the client's screen dimensions. What are you going to do for people with javascript disabled.
    If you'll excuse me saying, I'm a web designer and I think I know how to write CSS and (X)HTML. As Mindzai correctly pointed out, I am writing an analytics package for distribution (free) and, although this is not something I require, Screen Resolution is something logged by Google Analytics, the most well-known free package. Many people may take this into account when considering using my software.

    For people without javascript, the main script runs in php, so that will pick most things up. Please do not flame, it is considered offensive on forums. The iframe was a temporary method of implementing javascript.

    To connect javascript and PHP, it is necessary either to set cookies or to compose a url using javascript. Both require a page reload for PHP to interpret them. This messes up detection of the referrer in PHP, so I used an iframe.

    I repeat on sending a url of a PHP script for the image, how do I get it do display the image?

    Thanks,

    Ronnie268
    Failure isn't an option - It comes bundled with Windows

    π ~= 3.14
    3*14 = 42
    Coincidence? I think not.
    π = The Answer to Life, the Universe, and Everything.

  4. #19
    Join Date
    Nov 2008
    Posts
    2,477
    Quote Originally Posted by Ronnie268 View Post
    To connect javascript and PHP, it is necessary either to set cookies or to compose a url using javascript. Both require a page reload for PHP to interpret them. This messes up detection of the referrer in PHP, so I used an iframe.
    This is not correct. As Ive said a few times in this thread now, the right way to do this is to send a post request via AJAX. This doesn't require composing a url with javascript, setting cookies or reloading any pages.

    if you really want to use an image (which I would consider a "bodge job" as we say in the UK), you'd have to set the appropriate HTTP header withthe header() function, and then output the image file to the browser.

    PHP Code:
    <?php
    header
    ('Content-type: image/gif');
    readfile('file.gif');
    exit();
    ?>
    I really would not suggest doing this though, it's not a good approach.
    Last edited by Mindzai; 06-05-2009 at 12:17 PM.

  5. #20
    Join Date
    Jun 2009
    Location
    Cannock
    Posts
    30

    Question

    Quote Originally Posted by Mindzai View Post
    if you really want to use an image (which I would consider a "bodge job" as we say in the UK), you'd have to set the appropriate HTTP header withthe header() function, and then output the image file to the browser.
    Yes - I'm UK too. I am aware of this being a "bodge" job.

    Thanks for the Ajax solution. How exactly would I do this? I'm not an Ajax guy!

    Thanks,

    Ronnie268
    Failure isn't an option - It comes bundled with Windows

    π ~= 3.14
    3*14 = 42
    Coincidence? I think not.
    π = The Answer to Life, the Universe, and Everything.

  6. #21
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by Ronnie268 View Post
    How exactly would I do this? I'm not an Ajax guy!
    Here's a really simple example. The Javascript alerts are only there so you can see what's happening.

    http://bokehman.com/ajax.stats.capture/

    index.php
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    
    <html>
    <head>
    
    <script src="./stats_capture.js" type="text/javascript"></script>
    
    <title>Stats capture</title>
    </head>
    <body>
    
    <p>
    Stats capture test
    </p>
    
    </body>
    </html>
    stats_capture.js
    HTML Code:
    var ajax_processor = './ajax_processor.php'
    
    onload = function(){stats_capture('', null)}
    
    function loadXMLDoc(url) 
    {
     if (window.XMLHttpRequest) 
     {
      req = new XMLHttpRequest()
      req.onreadystatechange = processReqChange
      req.open("GET", url, true)
      req.send(null)
     }
     else if (window.ActiveXObject)
     {
      req = new ActiveXObject("Microsoft.XMLHTTP")
       if (req)
       {
        req.onreadystatechange = processReqChange
        req.open("GET", url, true)
        req.send()
      }
     }
    }
    
    function processReqChange() 
    {
     if (req.readyState == 4) 
     {
      if (req.status == 200) 
      {
       response  = req.responseXML.documentElement
       method = response.getElementsByTagName('method')[0].firstChild.data
       result = response.getElementsByTagName('result')[0].firstChild.data
       eval(method + '(\'\', result)')
      }
     }
    }
    
    function stats_capture(input, response)
    {
     // The input variable is not used here but is present 
     // for conformity so other ajax functions using the
     // same functions that may have input.
     if (response !== null)
     { 
      // Response mode
      alert(response)
      return false
     }
     else
     {
      // Input mode
      alert('Sending stats, the following values are about to be sent to the server: screen width ' + screen.width + ', screen height ' + screen.height)
      url  = ajax_processor + '?stats_capture=' + screen.width + ',' + screen.height
      loadXMLDoc(url)
     }
    }
    ajax_processor.php
    PHP Code:
    <?php

    if(!empty($_GET['stats_capture']))
    {
        
    # record stats to DB here
        
    list($width,$height) = explode(','$_GET['stats_capture']);
        
        
    # start: build and send the XML response
        
    header('Content-Type: text/xml');
        echo 
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\n".
             
    "<response>\n".
             
    " <method>stats_capture</method>\n".
             
    " <result>Stats confirmation, the server recorded the following values to the database: screen width {$width}, screen height {$height}</result>\n".
             
    "</response>\n";
        
    # end: build and send the XML response
    }

    ?>

  7. #22
    Join Date
    Jun 2009
    Location
    Cannock
    Posts
    30
    Thanks very much for that.

    As I'm a js newbie, could you please explain the stats_capture.js line by line, so I know where to add new logging stuff?

    Also, has anyone any ideas how to make a PHP fall-back for if JS is disabled? This would enable bot logging.

    Thanks,

    Ronnie268
    Failure isn't an option - It comes bundled with Windows

    π ~= 3.14
    3*14 = 42
    Coincidence? I think not.
    π = The Answer to Life, the Universe, and Everything.

  8. #23
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by Ronnie268 View Post
    could you please explain the stats_capture.js line by line, so I know where to add new logging stuff?
    Any extra items just add to the stats_capture function. Also don't forget half the stuff you may want to log is available through the http headers so your logging script could capture them from the ajax request.


    Quote Originally Posted by Ronnie268 View Post
    Also, has anyone any ideas how to make a PHP fall-back for if JS is disabled? This would enable bot logging.
    Depends what you want to log. For example bots don't have screen width and height as they don't have screens.
    Last edited by bokeh; 06-07-2009 at 03:45 PM.

  9. #24
    Join Date
    Jun 2009
    Location
    Cannock
    Posts
    30
    Right. Thanks for that, but in what form should I put the stats to log. Could you show me an example or two, please.

    Also, might someone tell me what each line of stats_capture.js does, because it helps to understand it for debugging/expansion.

    Thanks,

    Ronnie268
    Failure isn't an option - It comes bundled with Windows

    π ~= 3.14
    3*14 = 42
    Coincidence? I think not.
    π = The Answer to Life, the Universe, and Everything.

  10. #25
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    First two functions do the request and response.

    The third function (input phase) collects the data and sends it to the first function for transmission to the server. When there is a response from the server (response phase) that response is return to the third function (by the second function). In the PHP response script (line 12) php is telling the javascrit which function to return the response to as there may be more than one javascript function that possesses the responses.

    In this example I've just used one variable which contains both the screen width and height delimited by a comma. If you choose you could send a whole string of variables and process them as you wish in PHP.

  11. #26
    Join Date
    Jun 2009
    Location
    Cannock
    Posts
    30
    Thanks for that.

    What does processReqchange do? Is it needed if you don't want to send a response?

    I've thought of a PHP fallback. Use the base PHP logging script to record basic stats and to write the javascript. Then if the javascript fails, the basic logging is still done.

    Thanks,

    Ronnie268
    Failure isn't an option - It comes bundled with Windows

    π ~= 3.14
    3*14 = 42
    Coincidence? I think not.
    π = The Answer to Life, the Universe, and Everything.

  12. #27
    Join Date
    Nov 2008
    Posts
    2,477
    You could also parse Apache logs depending on what sort of stats you are after.

  13. #28
    Join Date
    Jun 2009
    Location
    Cannock
    Posts
    30
    Thanks for all your help in this. It works! I shall definitely be adding a link to your website in the credits when I release the program!

    All the best,

    Ronnie268
    Failure isn't an option - It comes bundled with Windows

    π ~= 3.14
    3*14 = 42
    Coincidence? I think not.
    π = The Answer to Life, the Universe, and Everything.

  14. #29
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by Ronnie268 View Post
    What does processReqchange do? Is it needed if you don't want to send a response?
    Ronnie, it's like this, you just want your code to do this one job, but when I wrote that code I tried to make it as reusable as possible so that when someone else comes along who wants to have background communication with the server for another reason they will already be able find 75% of the code they need.

  15. #30
    Join Date
    Jun 2009
    Location
    Cannock
    Posts
    30
    Would this work across websites, like say if I my domain were example.com and I had a user who wanted to use it on foo.com, could I tell him to put
    Code:
    <script type="text/javascript" src="http://example.com/analytics.js"></script>
    in his HTML document?
    Failure isn't an option - It comes bundled with Windows

    π ~= 3.14
    3*14 = 42
    Coincidence? I think not.
    π = The Answer to Life, the Universe, and Everything.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles