我一直在研究某种带有统计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);
我知道整个标题();应该永远是第一,但我不知道在哪里移动它。
[EDITED] 完整代码:
// 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>
答案 0 :(得分:0)
尝试更改:
// 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
}
为:
// Get Referrer and Page
if (isset($_GET["ref"]))
{
// from javascript
$referer = $_GET["ref"];
$page = ((isset($_SERVER['HTTP_REFERER'])) ? (parse_url($_SERVER['HTTP_REFERER'], PHP_URL_PATH)) : (''));
}
else
{
// from php
$referer = ((isset($_SERVER['HTTP_REFERER'])) ? ($_SERVER['HTTP_REFERER']) : (''));
$page = $_SERVER['PHP_SELF']; // with include via php
}
至于其他错误,我不确定,因为您的帖子中似乎缺少这行代码。