Making Usage Statistics in PHP - Before Saying Goodbye
(Page 7 of 7 )
Put the picture making part of your program to the beginning of your main PHP that produces HTML output! Use the isset() function that makes you able to check whether a variable is set or not.
An example: you have a huge code in an index.php file with an if (isset($gd)) condition at the beginning. The output of the index.php is HTML, if there's no $gd set and if there is, then the output is image. Imagine that you can include pictures with echoing a simple <img src=?gd=> tag. My favourite usage of this is the code below (thanks to Jack Shieh). It shows a thumbnail of the images stored in a MySQL table.
if
(isset($pic_id)) {
$q='SELECT Picture FROM images WHERE ID='.$pic_id;
$pic_r=mysql_query($q) or print(mysql_error());
$pic=mysql_fetch_array($pic_r);
header("Content-type: image/jpeg");
$size=80; // new image width
$src=imagecreatefromstring($pic['Picture']);
$width=imagesx($src); $height=imagesy($src);
$aspect_ratio = $height/$width;
if ($width <= $size) {
$new_w = $width;
$new_h = $height;
} else {
$new_w = $size;
$new_h = abs($new_w * $aspect_ratio);
}
$img = imagecreatetruecolor($new_w,$new_h);
imagecopyresized (img,$src,0,0,0,0,$new_w,$new_h,$width,$height);
imagejpeg($img);
}
$q='SELECT ID FROM images';
$ids_r=mysql_query($q) or print(mysql_error());
echo "<table width=100%><tr>";
while ($ids=mysql_fetch_array($ids_r))
echo "<td><img src=?pic_id=".$ids['ID']."></td>n";
echo "</tr></table>";
I hope that now you can track your visitors and make graphical statistics easily as well. I wish you the experience of being visited - and I wish you saw your success also in the statistics!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |