• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

[PHP/javascript] Loading a php scipt instead of picture for webstats

FromHollandWithLove

Junior Member
Hi. 🙂

I have recently set up a Mysql database to store user information from people visiting my site. I use PHP to gather the information and write it to the database ($var_ip = $REMOTE_ADDR; $var_agent = $HTTP_USER_AGENT; etc).

Someone told me there is a better method, namely by calling an image from javascript which is actualy a PHP script. Nedstat uses this method too. Can someone explain the basics of this method, perhaps with some code?

I understand that when calling the picture a lot of information is sent along with the request (in the headers right?). Because i call a PHP script instead of a real picture, the information can be captured by PHP and written to the database.

How do yuu capture that information and assign it to PHP variables?
 
You lost me somewhere inbetween the "Hi" and the ending period.

Do you want the PHP script to display an image? Or what are you trying to accomplish by calling an image from Javascript?
 
There is no advantage to using am image to log user information UNLESS you want to set it up so that someone WITHOUT access to PHP can get website stats.

For instance, someone who can just use strait HTML, they could put a tag like this:

--- http://www.theirserver.com/theirpage.html ----

<img src="http://www.yourserver.com/yourphpstatpage.php">

---------------------------------------------------------------


Then you could log statistics for them. All the PHP script needs to do it set the appropriate HTTP headers, then do something like fpassthru() to read in the JPG you want to display ( coudl be a 1 pixel transparent gif or something ) and display it. Then the rest o fhte code could be to log user data.
 
Here's how to do it:

<script language="php">
Header("Cache-Control: no-cache");
Header("Pragma: no-cache");
header( "Content-type: pjpeg");
$file=fopen("yourimage.jpg","r");
$size=filesize("yourimage.jpg");
$data=fread($file,$size);
echo $data;
fclose($file);
</script>

if it's a gif, alter the script as necessary.

Edit: to capture the header info from the browser, use the $_SERVER[] array, like:
$host = $_SERVER["HTTP_HOST"]
$referrer = $_SERVER["HTTP_REFERRER"]

For more info, look here:
http://php.planetmirror.com/manual/en/reserved.variables.php#reserved.variables.server
 
I'll try to explain again, because i don't think this is what i mean.

Someone gave me this example:
<script>
myvar = navigator.userAgent;
document.write('<img src="yourphpstatfile.php?something='+myvar+'" width="1" height="1"/>');
</script>

and then, in the yourphpstatsfile, you can just extract accordingly.

As you can see, 'myvar' is sent on the query string to the php file. PHP should be able to extract the variables from the query string, but how? I want to put multiple variables on the query string.
 
You are right, you explained it badly in the beginning.

Getting the query string is easy, you basically do:

$myvariable = $_SERVER["QUERY_STRING"];

if you set register globals, the variable defined in the query string is also set, for instance:
index.php?myvar=1

leads to

$myvar=1 being set in index.php.
Registered globals are not a good behaviour, though.
Just evaluate the query string above.
 
Back
Top