I have a php script that gets image data and then echoes it. That part works fine. But, i want to be able to track how many times the script is accessed, so everytime the users accesses the script, I insert another hit in the hits table. On the main homepages, I use a php function(count_num_rows) to count the number of rows in the 'hits' table. That parts looks like it works fine. Here is my entire code for the image script (with some info changed for security) :
Code: Select all
<?php
$con = mysql_connect("localhost","db","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
function genRandomString() {
$length = 10;
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
$string = "";
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}
$string = genRandomString();
mysql_select_db("db", $con);
$sql = "INSERT INTO `hits` VALUES ('3', '{$string}')";
mysql_query($sql,$con);
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
// get bytearray
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
// add headers for download dialog-box:
header('Content-Type: image/jpeg');
header("Content-Disposition: attachment; filename=".$_GET['name']);
echo $jpg;
}
?>
Code: Select all
require("db_connect.php");
mysql_select_db("db", $con);
$result = mysql_query("SELECT * FROM hits", $con);
$num_rows = mysql_num_rows($result);
echo "$num_rows";