Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 点击计数器不显示计数器_Php - Fatal编程技术网

Php 点击计数器不显示计数器

Php 点击计数器不显示计数器,php,Php,我在网上找到了这个例子,但它不起作用 co计数器显示,它确实创建了counter.txt文件 我正在chrome的localhost上测试t 随函附上我的密码: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Login Form</title> </head> <body background="images/stoc

我在网上找到了这个例子,但它不起作用

co计数器显示,它确实创建了counter.txt文件 我正在chrome的localhost上测试t

随函附上我的密码:

 <!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文件:

  <?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);
 ?>

输出为:


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

session_start();
$counter_name = "counter.txt"; 

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

到图像的路径正确吗?是的,我用echo getcwd()检查了它;您当前得到的输出是什么?html代码是my page2.html,其中包含对counter.php的“调用”。请参阅下面的OPLook。它的回报是什么?布尔值。您正在输出图像内容,后面跟着一个
1
true
,将
imagepng
的响应转换为字符串)。将echo(imagepng($im))更改为
imagepng($im)
。当有人询问输出时,请在浏览器中打开您的PHP文件,查看是否有任何错误。哦,是的,我的代码中有,我也会将其添加到这里,对不起,您的图像是否在.png中?您确定图像的名称正确吗?您是否使用firebug检查了输出?是的,它是一个png,我使用paint创建了.png,并且打开了chome的开发工具,与firebug类似是的,名称正确,检查过:)