So why does he want an iframe?
Printable View
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:
Quote:
I'm writing Analytical software that uses javascript but mainly PHP to log things
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
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.
I really would not suggest doing this though, it's not a good approach.PHP Code:<?php
header('Content-type: image/gif');
readfile('file.gif');
exit();
?>
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.phpstats_capture.jsHTML 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>
ajax_processor.phpHTML 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)
}
}
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
}
?>
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
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.
Depends what you want to log. For example bots don't have screen width and height as they don't have screens.
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
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.
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
You could also parse Apache logs depending on what sort of stats you are after.
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
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.
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
in his HTML document?Code:<script type="text/javascript" src="http://example.com/analytics.js"></script>