首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >点击没有显示计数器的计数器

点击没有显示计数器的计数器
EN

Stack Overflow用户
提问于 2015-02-27 11:00:46
回答 1查看 135关注 0票数 1

我学习了我在网上发现的这个例子,但是它不起作用。

http://hibbard.eu/how-to-make-a-simple-visitor-counter-using-php/ co计数器显示,它确实创建了我正在本地主机上测试的counter.txt文件

随函附上我的代码:

代码语言:javascript
复制
 <!doctype html>
 <html>
 <head>
 <meta charset="utf-8">
 <title>Login Form</title>
 </head>
 <body background="images/stockvault.jpg">      
    <b><ul>THIS IS A WORK IN PROGRESS</ul></b>
    <br />  
    <br />    
    <img alt="hit counter" src="php/counter.php" />
    <br />  
    <br />      
 </body>
 </html>

然后我的php文件:

代码语言:javascript
复制
  <?php
  session_start();
  $counter_name = "counter.txt";

 // Check if a text file exists. If not create one and initialize it to zero.
 if (!file_exists($counter_name)) {
   $f = fopen($counter_name, "w");
   fwrite($f,"0");
   fclose($f);
 }


 // Read the current value of our counter file
 $f = fopen($counter_name,"r");
 $counterVal = fread($f, filesize($counter_name));
 fclose($f);

// Has visitor been counted in this session?
// If not, increase counter value by one
 if(!isset($_SESSION['hasVisited'])){
    $_SESSION['hasVisited']="yes";
    $counterVal++;
    $f = fopen($counter_name, "w");
    fwrite($f, $counterVal);
    fclose($f); 
  }


  $counterVal = str_pad($counterVal, 5, "0", STR_PAD_LEFT);
  $chars = preg_split('//', $counterVal);

   // Change directory
   chdir("../");

  // Get current directory
  //echo getcwd();
  chdir("images");


  $im = imagecreatefrompng("images.png");
  $src1 = imagecreatefrompng("$chars[1].png");
  $src2 = imagecreatefrompng("$chars[2].png");
  $src3 = imagecreatefrompng("$chars[3].png");
  $src4 = imagecreatefrompng("$chars[4].png");
  $src5 = imagecreatefrompng("$chars[5].png");

  imagecopymerge($im, $src1, 0, 0, 0, 0, 56, 75, 100);
  imagecopymerge($im, $src2, 60, 0, 0, 0, 56, 75, 100);
  imagecopymerge($im, $src3, 120, 0, 0, 0, 56, 75, 100);
  imagecopymerge($im, $src4, 180, 0, 0, 0, 56, 75, 100);
  imagecopymerge($im, $src5, 240, 0, 0, 0, 56, 75, 100);

  header('Content-Type: image/png');
  echo (imagepng($im));
  imagedestroy($im);
 ?>

产出如下:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-27 11:15:58

在PHP代码的开头,至少在复制和粘贴中,您忘记了这部分代码。

代码语言:javascript
复制
session_start();
$counter_name = "counter.txt"; 

您需要session_start()来处理$_SESSION变量,并将生成的文件counter.txt指定为$counter_name

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28763491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档