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
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.
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.
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.
<!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
}
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.
Originally Posted by Ronnie268
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.
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.
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.
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.
Bookmarks