我一直在使用统计 PHP 脚本处理某种命中计数器,但遇到以下错误:图像http://localhost/site/counter.php
无法显示,因为它包含错误。
这是执行图像生成的代码:
// Generate Image
// Get Value from DB
if ($show == "last24h")
{
// Last24h
$islast = $time-24*60*60;
$totalips = mysql_query("SELECT count(id) FROM counter_ips WHERE time>='$islast'");
$value = mysql_result($totalips,0,0);
$title = "Last 24 hours";
mysql_free_result($totalips);
}
else
{
// Total Visitors
$totalvisitors = mysql_query("SELECT sum(user) FROM counter_day");
$value = mysql_result($totalvisitors,0,0);
$title = "Total Visitors";
mysql_free_result($totalvisitors);
}
// Short Value
if ( $value > 999 ) { $value = $value / 1000; $unit = "k"; }
if ( $value > 999 ) { $value = $value / 1000; $unit = "m"; }
if ( $value > 999 ) { $value = ">999"; $unit = "m"; }
else {
if ( $value >=10 ) $value = round($value,0);
else $value = round($value,1);}
$value .= $unit;
// Variables
$title_font="OpenSans-Regular.ttf";
$value_font="OpenSans-Bold.ttf";
if ($size == "small")
{
$width = 90;
$height = 20;
$title_font_size = 8;
$value_font_size = 9;
$title_pos_y = 15;
$value_pos_y = 16;
// short title
if ($show == "last24h") {$title="Last24h";}
else {$title="Visitors";}
// left title
$size = imagettfbbox($title_font_size, 0, $title_font, $title);
$titleWidth = $size[2] - $size[0];
$title_pos_x = 8;
// right center value
$size = imagettfbbox($value_font_size, 0, $value_font, $value);
$valueWidth = $size[2] - $size[0];
$space_left = $title_pos_x + $titleWidth;
$value_pos_x = $space_left + ((($width - $space_left) / 2) - ($valueWidth / 2));
}
else
{
$width = 90;
$height = 55;
$title_font_size = 8;
$value_font_size = 24;
$title_pos_y = 15;
$value_pos_y = 48;
// center title
$size = imagettfbbox($title_font_size, 0, $title_font, $title);
$textWidth = $size[2] - $size[0];
$title_pos_x = ($width / 2) - ($textWidth / 2);
// center value
$size = imagettfbbox($value_font_size, 0, $value_font, $value);
$textWidth = $size[2] - $size[0];
$value_pos_x = ($width / 2) - ($textWidth / 2);
}
// Create a blank image
$im = imagecreatetruecolor($width,$height);
// Colors
if ($style == "light")
{
$bg_color = imagecolorallocatealpha($im, 235,235,235,0);
$title_color = imagecolorallocate($im, 50,50,50);
$value_color = imagecolorallocate($im, 25,25,25);
}
else
{
$bg_color = imagecolorallocatealpha($im, 50,50,50,0);
$title_color = imagecolorallocate($im, 255,255,255);
$value_color = imagecolorallocate($im, 255,255,255);
}
$shadow_color = imagecolorallocatealpha($im, 0,0,0,115);
$red = imagecolorallocate($im, 223,1,1);
// Fill BG color
imagefill($im, 0, 0, $bg_color);
// Red line
imageline($im,0,0,$width,0,$red);
imageline($im,0,1,$width,1,$red);
// title
imagettftext ($im, $title_font_size, 0, $title_pos_x+2, $title_pos_y+2, $shadow_color, $title_font, $title);
imagettftext ($im, $title_font_size, 0, $title_pos_x, $title_pos_y, $title_color, $title_font, $title);
// value
imagettftext ($im, $value_font_size, 0, $value_pos_x+2, $value_pos_y+2, $shadow_color, $value_font, $value);
imagettftext ($im, $value_font_size, 0, $value_pos_x, $value_pos_y, $value_color, $value_font, $value);
// image output
header ("Content-type: image/png");
// create PNG
imagepng($im);
// destroy temp image
imagedestroy($im);
我知道整个 header(); 应该总是排在第一位,但我不知道该移到哪里。
[编辑] 完整代码:
// Date and Time
$time = time();
$day = date("Y.m.d",$time); // YYYY.MM.DD
$month = date("Y.m",$time); // YYYY.MM
// Language
$language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);
// IP address
$ip = $_SERVER['REMOTE_ADDR'];
// Get Referrer and Page
if (isset($_GET["ref"]))
{
// from javascript
$referer = $_GET["ref"];
$page = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_PATH);
}
else
{
// from php
$referer = $_SERVER['HTTP_REFERER'];
$page = $_SERVER['PHP_SELF']; // with include via php
}
// Cleanup
if (basename($page) == basename(__FILE__)) $page="" ; // count not counter.php
$server_host = $_SERVER["HTTP_HOST"]; // Server Host
if (substr($server_host,0,4) == "www.") $server_host = substr($server_host,4); // Server Host without www.
$referer_host = parse_url($referer, PHP_URL_HOST); // Referrer Host
if (substr($referer_host,0,4) == "www.") $referer_host = substr($referer_host,4); // Referer Host without www.
// Adjust Search Engines
// Google
if (strstr($referer_host, "google."))
{
$referer_query = parse_url($referer, PHP_URL_QUERY);
$referer_query .= "&";
preg_match('/q=(.*)&/UiS', $referer_query, $keys);
$keyword = urldecode($keys[1]); // These are the search terms
$referer_host = "Google"; // adjust host
}
// Yahoo
if (strstr($referer_host, "yahoo."))
{
$referer_query = parse_url($referer, PHP_URL_QUERY);
$referer_query .= "&";
preg_match('/p=(.*)&/UiS', $referer_query, $keys);
$keyword = urldecode($keys[1]); // These are the search terms
$referer_host = "Yahoo"; // adjust host
}
// Bing
if (strstr($referer_host, "bing."))
{
$referer_query = parse_url($referer, PHP_URL_QUERY);
$referer_query .= "&";
preg_match('/q=(.*)&/UiS', $referer_query, $keys);
$keyword = urldecode($keys[1]); // These are the search terms
$referer_host = "Bing"; // adjust host
}
// Delete Old IPs
$timecheck = mktime(0, 0, 0, date("n"), date("j"), date("Y")) - 48*60*60 ; // 48*60*60 => after 48 hours
$delete = mysql_query("DELETE FROM counter_ips WHERE time < '$timecheck'");
if (!$delete)
{
die("Database Selection Failed: ".mysql_error());
}
// Delete Old Page, Eeferrer, Language and Keywords
$old_day = date("Y.m.d", mktime(0, 0, 0, date("n"), date("j")-$oldentries, date("Y")) );
$delete = mysql_query("DELETE FROM counter_page WHERE day<='$old_day'");
$delete = mysql_query("DELETE FROM counter_referer WHERE day<='$old_day'");
$delete = mysql_query("DELETE FROM counter_keyword WHERE day<='$old_day'");
$delete = mysql_query("DELETE FROM counter_language WHERE day<='$old_day'");
if (!$delete)
{
die("Database Selection Failed: ".mysql_error());
}
// Insert a New Day
$newday = mysql_query("SELECT id FROM counter_day WHERE day='$day'");
if (!$newday)
{
die("Database Selection Failed: ".mysql_error());
}
if (mysql_num_rows($newday)==0)
{
mysql_query("INSERT INTO counter_day (day, user, view) VALUES ('$day', '0', '0')");
}
// Check Reload and Set Online Time
$newuser = 0;
$oldreload = $time-$reload;
$userid = mysql_query("SELECT id FROM counter_ips WHERE ip='$ip' AND time>'$oldreload' ORDER BY id DESC LIMIT 1");
if (!$userid)
{
die("Database Selection Failed: ".mysql_error());
}
if (mysql_num_rows($userid)==0)
{
// New Visitor
$newuser = 1;
mysql_query("INSERT INTO counter_ips (ip, time, online) VALUES ('$ip', '$time', '$time')");
mysql_query("UPDATE counter_day SET user=user+1, view=view+1 WHERE day='$day'");
}
else
{
// Reload Visitor
$olduser = mysql_result($userid,0,0);
mysql_query("UPDATE counter_ips SET online='$time' WHERE id='$olduser'");
mysql_query("UPDATE counter_day SET view=view+1 WHERE day='$day'");
}
// Page
if($page <> "") {
$result = mysql_query("SELECT id from counter_page WHERE page='$page' AND day='$day'");
if (!$result)
{
die("Database Selection Failed: ".mysql_error());
}
if (mysql_num_rows($result)==0)
{
mysql_query("INSERT INTO counter_page (day, page, view) VALUES ('$day', '$page', '1')");
}
else
{
$pageid = mysql_result($result,0,0);
mysql_query("UPDATE counter_page SET view=view+1 WHERE id='$pageid'");
}
}
// Referer
if(stristr($server_host, $referer_host) === FALSE AND $referer_host<>"" AND $newuser == 1) {
$result = mysql_query("SELECT id FROM counter_referer WHERE referer='$referer_host' AND day='$day'");
if (!$result)
{
die("Database Selection Failed: ".mysql_error());
}
if (mysql_num_rows($result)==0)
{
mysql_query("INSER INTO counter_referer (day, referer, view) VALUES ('$day', '$referer_host', '1')");
}
else
{
$refererid = mysql_result($result,0,0);
mysql_query("UPDATE counter_referer SET view=view+1 WHERE id='$refererid'");
}
}
// Keywords
if(isset($keyword) AND $newuser == 1) {
$result = mysql_query("SELECT id FROM counter_keyword WHERE keyword='$keyword' AND day='$day'");
if (!$result)
{
die("Database Selection Failed: ".mysql_error());
}
if (mysql_num_rows($result)==0)
{
mysql_query("INSERT INTO counter_keyword (day, keyword, view) VALUES ('$day', '$keyword', '1')");
}
else
{
$keywordid = mysql_result($result,0,0);
mysql_query("UPDATE counter_keyword SET view=view+1 WHERE id='$keywordid'");
}
}
// Language
if($language<>"" AND $newuser == 1) {
$result = mysql_query("SELECT id FROM counter_language WHERE language='$language'");
if (!$result)
{
die("Database Selection Failed: ".mysql_error());
}
if (mysql_num_rows($result)==0)
{
mysql_query("INSERT INTO counter_language (day, language, view) VALUS ('$day', '$language', '1')");
}
else
{
$languageid = mysql_result($result,0,0);
mysql_query("UPDATE counter_language SET view=view+1 WHERE id='$languageid'");
}
}
现在的2个错误如下:
注意:未定义索引:第 27 行 C:\wamp\www\site\counter.php 中的 HTTP_REFERER
注意:未定义变量:第 245 行 C:\wamp\www\site\counter.php 中的单位
这是我在我的页面上使用的代码来进行计数:
<a href="<?php echo $siteurl; ?>admin/login.php">Admin Login</a>
<script type="text/javascript">
document.write('<a href="stats.php"><img src="counter.php?ref=' + escape(document.referrer) + '" border="0"></a>')
</script>
<noscript><a href="stats.php"><img src="counter.php" border="0"></a></noscript>