2012-07-03 52 views
0

我一直在處理某種統計PHP腳本的計數器,我遇到了以下錯誤:圖像http://localhost/site/counter.php無法顯示,因爲它包含錯誤。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); 

我知道,整個報頭();應該總是排在第一位,但我不知道應該向哪裏移動。

將帖子 全碼:

  // 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個錯誤,現在有以下幾種:

  • 注意:未定義指數:HTTP_REFERER在C:\ WAMP \ WWW \網站\上線counter.php 27

  • 說明:未定義變量:單元在C:\瓦帕\ WWW \站點\上線counter.php 245

,這是代碼我用我的網頁上做計數:

<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

標題不必首先出現在_any_輸出之前。 – Bojangles

+2

當您刪除Content-Type標題時是否收到任何錯誤消息?如果你刪除這個,那麼常規的PHP警告,通知和錯誤將會出現。如果沒有出現,請嘗試將error_reporting(E_ALL)放在頂部以顯示這些錯誤。讓我們知道你看到了什麼。 –

+0

當我運行在http://william.isted.me/lab/ting.php減去字體(我沒有他們)它似乎輸出圖像 –

回答

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  
      } 

,並作爲其他錯誤我不確定,因爲該行代碼似乎從您的帖子中丟失。

+0

對不起,第二個錯誤是在生成圖像部分。//短值 – Khaled

+0

任何想法,我該如何解決第二個錯誤? – Khaled

相關問題