一个用php3编写的简单计数器_PHP
计数器
php具有极其强大的图像处理能力,用它可以很轻易的动态生成web图像。一下是一个使用php做成的一个简单计数器。
1. 总体思路:
把以往的访问人数记录在一个文本文件中,当网页被访问的时候,从打开该文件
并从中读出以往的访问人数,加 1,得到最新的访问人数,并把该数目格式化成
标准的格式,再调用图像处理函数,把该数字输出成图片,再把新的访问数字回
写到纪录访问人数的文件中。
2. 程序所用到的函数说明:
A. 相关的文件操作:
a. 打开文件:
函数的原型:int fopen(string filename, string mode);
返回结果:如果打开文件成功,函数返回文件流指针,否则返回 FALSE(0)。
参数说明:
string filename -- 要打开的文件名,必须为字符串形式。
例如, "zzm.txt"、"..\zzm.txt"等。
string mode -- 打开文件的方式,必须为字符形式。
'r',只读形式,文件指针指向文件的开头
'r ',可读可写,文件指针指向文件的开头
'w',只写形式,文件指针指向文件的开头,把文件长度截成0,
如果文件不存在,将尝试建立文件。
'w ',可读可写,文件指针指向文件的开头,把文件长度截成0,
如果文件不存在,将尝试建立文件。
'a',追加形式(只可写入),文件指针指向文件的最后,如果文
件不存在,将尝试建立文件。
'a ',可读可写,文件指针指向文件的最后,如果文件不存在,
将尝试建立文件。
例子:用只读形式打开当前目录下面的"zzm.txt"
$fp = fopen("zzm.txt", "r");
b. 关闭文件:
函数原型:int fclose(int fp);
返回结果:成功返回1,失败返回0
参数说明:int fp是 fopen函数返回的文件流指针。
例子:关闭干刚才用fopen打开的zzm.txt文件
fclose($fp);
c. 读文件:
函数原型:string fgets(int fp, int length);
返回结果:返回 length -1 长度的字符串,如果到文件结尾,返回 EOF(End Of File)
参数说明:
int fp -- 要读入数据的文件流指针,由fopen函数返回的数值
int length -- 读入的字符个数,实际读入字符个数为 length -1 个
例子:从 $fp 中读取9个字符
$str1 = fgets($fp,10);
d. 写文件:
函数原型:int fputs(int fp, string str, int [length]);
返回结果:和fclose同
参数说明:
int fp -- 要写入信息的文件流指针,由fopen函数返回的数值
string str -- 要写入文件的字符串。
int length -- 写入的长度,可选的,如果不提供length,则整个串将被写入,
否则,写入length长度个字符。
例子:向 $fp 写入 "0000000001"
fput($fp, "0000000001");
B. 相关的字符串函数:
a. 计算字符串长度:
函数原型:int strlen(string str);
返回结果:返回字符串的长度
参数说明:
string str -- 要计算长度的字符串
例子:计算 "000000000" 的字符串长度
$str2 = "000000000";
$len2 = strlen($str);
b. 字符串相加:最简单不过了,用一个 . 把两个字符串连接起来。
例子:把 $str1和$str2相加
$str = $str1.$str2
C. 相关的图形函数:
a. 新建图像:
函数原型:int imagecreate(int x_size, int y_size);
返回结果:返回一个 X*Y 像素大小的空图像识别号(ImageID)
参数说明:x_size,y_size分别是新建图像的宽度和高度(以像素为单位)
例子:新建一个 88*31 像素大小的空图片
$ImageID = imagecreate(88, 31);
b. 给图像分配一种颜色:
函数原型:int imagecolorallocate(int im, int red, int green, int blue);
返回结果:给图像($im)返回一个RGB颜色识别号
参数说明:int im 图像识别号
int red、green、blue分别是红绿蓝三种颜色的分量,取值范围 0 - 255
例子:给图像$im 分配一个识别号为$white白色颜色,白色的RGB为(255,255,255)
$white = imagecolorallocate($im, 255, 255, 255);
c. 给图像填充颜色:
函数原型:int imagefill(int im, int x, int y, int col);
返回结果:成功返回1,否则返回0
参数说明:int im,图像的识别号
int x, int y,从图像的(x,y)坐标开始填充颜色
(0,0)表示图像的左上角
int col,颜色的识别号
例子:从图像的左上角开始(即整个图片)填入黑色(已经用imagecolorallocate函数
定义了黑色的颜色识别号为$black了)。
imagefill($im, 0, 0, $black);
d. 计算图像的宽度:
函数原型:int imagesx(int im);
返回结果:返回图像的宽度(单位为像素)
参数说明:int im,图像的识别号。
例子:计算图像$im的宽度
$px = imagesx($im);
e. 在图像中写入水平文字:
函数原型:int imagestring(int im, int font, int x, int y, string s, int col)
;
返回结果:成功返回1,否则返回0
参数说明:int im,图像的识别号
int font,字体识别号,内建字体1到5,用户可以用imageloadfont()自己?
?
载字体.
int x,int y,开始写入字体的坐标,(0,0)为图片的左上角。
string s,要写入的字串
int col,字体的颜色识别号
例子:在图像(3,3)位置写入字号为3,颜色为白(已经用imagecolorallocate()函数
定义了黑色的颜色识别号为$white)的字串"E&J Counter"
ImageString($im, 3, 3, 3, "E&J Counter", $white);
f. 在图像中划直线:
函数原型:int imageline(int im, int x1, int y1, int x2, int y2, int col);
返回结果:成功返回1,否则返回0
参数说明:int im,图像的识别号
int x1,int y1,划线的起坐标
int x2,int y2,划线的止坐标
int col,线的颜色识别号
例子:在图像$im中划一条从(1,14)到(85,14)颜色为$white的直线
imageline($im, 1, 14, 85, 14, $white);
g. 把图像输出成GIF格式:
函数原型:int imagegif(int im, string filename);
返回结果:成功返回1,否则返回0
参数说明:int im,图像的识别号
string filename,生成图片的名字,可选的,如果filename为空,则直接?
氖涑?
图片,你需要用Header("Content-type: image/gif")预先定义php输出的内
容为图片
例子:把图像$im输出成文件名为"image1.gif"的图片
imagegif($im, "image1.gif");
h. 释放图像:
函数原型:int imagedestroy(int im);
返回结果:成功返回1,否则返回0
参数说明:int im,要释放的图像识别号。该函数会释放识别号im的图像及图像所占
用的系统资源。
例子:释放图像$im
imagedestroy($im);
3. 如何安装这个计数器:
A. 系统必须安装了PHP解释器。PHP可以在http://www.php.net/上下载,在该站点上还有很
详细
的技术资料可以浏览或者下载阅读。如何安装PHP请参照它自己的说明。
B. 把下面的程序清单拷贝到一个文件中,并取扩展名为php,放入能够运行php脚本的目录?
旅妫?
并在目录下面建立一个纯文本文件,名字为zzm.txt。这个文件的作用是用来记录以往的
访问人
数用的。你可以预先设置计数器的初始值,例如5000。
C. 在网页如何调用这个计数器?你可以通过以下方式来调用:

附:完整的程序清单
Header("Content-type: image/gif");
//定义输出为图像类型
$fp = fopen("zzm.txt", "r");
//以读形式打开记录以往访问人数的文件zzm.txt
$str1 = fgets($fp,10);
//从文件中读入9个字符,本计数器最大能记录的访问人数为999999999
$str1 ;
//计数器加入
fclose($fp);
//关闭文件
$fp = fopen("zzm.txt", "w");
//一写的方式打开记录访问人数的文件zzm.txt
fputs($fp, $str1);
//把最新的访问人数写入文件
fclose($fp);
//关闭文件
/*
以下是把访问人数格式化输出,如果访问人数位数不够9位,例如时5000(4位),
则把访问人数变换成000005000的形式输出。方法是计算访问人数的位数,并且
把它和000000000的位数(9位)比较,得到相差的位数,然后在数字前面不上相
应个0。例如5000,和000000000两者的长度相差5,因此要在5000前面补5个0。
*/
$len1 = strlen($str1);
//计算访问人数的位数
$str2 = "000000000";
$len2 = strlen($str);
//定义计数器最大的计数位数
$dif = $len2 - $len1;
//计算两者的位数之差,即前面要补的0的个数
$rest = substr($str2, 0, $dif);
//截取要补的0
$string = $rest.$str1;
//前面补0
$font = 4;
//定义字号
$im = imagecreate(88,31);
//新建图象
$black = ImageColorAllocate($im, 0,0,0);
//定义黑色
$white = ImageColorAllocate($im, 255,255,255);
//定义白色
imagefill($im, 0,0,$black);
//把计数器的底色设置成黑色
$px = (imagesx($im)-8.3*strlen($string))/2;
//根据字串的长度,计算字串开始写入的水平坐标,目的是尽量让字串水平对中
ImageString($im,3,$px,2,"E&J Counter",$white);
//向图象写入"E&J Counter"
imageline($im, 1, 14, 85, 14, $white);
//划一根水平线
ImageString($im,$font,$px,15.5,$string,$white);
//写访问人数
ImageGif($im);
//把图象输出成GIF格式
ImageDestroy($im);
//释放图象
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Open WeChat, select Settings in Me, select General and then select Storage Space, select Management in Storage Space, select the conversation in which you want to restore files and select the exclamation mark icon. Tutorial Applicable Model: iPhone13 System: iOS15.3 Version: WeChat 8.0.24 Analysis 1 First open WeChat and click the Settings option on the My page. 2 Then find and click General Options on the settings page. 3Then click Storage Space on the general page. 4 Next, click Manage on the storage space page. 5Finally, select the conversation in which you want to recover files and click the exclamation mark icon on the right. Supplement: WeChat files generally expire in a few days. If the file received by WeChat has not been clicked, the WeChat system will clear it after 72 hours. If the WeChat file has been viewed,

In Windows, the Photos app is a convenient way to view and manage photos and videos. Through this application, users can easily access their multimedia files without installing additional software. However, sometimes users may encounter some problems, such as encountering a "This file cannot be opened because the format is not supported" error message when using the Photos app, or file corruption when trying to open photos or videos. This situation can be confusing and inconvenient for users, requiring some investigation and fixes to resolve the issues. Users see the following error when they try to open photos or videos on the Photos app. Sorry, Photos cannot open this file because the format is not currently supported, or the file

The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

Tmp format files are a temporary file format usually generated by a computer system or program during execution. The purpose of these files is to store temporary data to help the program run properly or improve performance. Once the program execution is completed or the computer is restarted, these tmp files are often no longer necessary. Therefore, for Tmp format files, they are essentially deletable. Moreover, deleting these tmp files can free up hard disk space and ensure the normal operation of the computer. However, before deleting Tmp format files, we need to

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

Quark Netdisk and Baidu Netdisk are currently the most commonly used Netdisk software for storing files. If you want to save the files in Quark Netdisk to Baidu Netdisk, how do you do it? In this issue, the editor has compiled the tutorial steps for transferring files from Quark Network Disk computer to Baidu Network Disk. Let’s take a look at how to operate it. How to save Quark network disk files to Baidu network disk? To transfer files from Quark Network Disk to Baidu Network Disk, you first need to download the required files from Quark Network Disk, then select the target folder in the Baidu Network Disk client and open it. Then, drag and drop the files downloaded from Quark Cloud Disk into the folder opened by the Baidu Cloud Disk client, or use the upload function to add the files to Baidu Cloud Disk. Make sure to check whether the file was successfully transferred in Baidu Cloud Disk after the upload is completed. That's it

The gho file is a GhostImage image file, which is usually used to back up the entire hard disk or partition data into a file. In some specific cases, we need to reinstall this gho file back to the hard drive to restore the hard drive or partition to its previous state. The following will introduce how to install the gho file. First, before installation, we need to prepare the following tools and materials: Entity gho file: Make sure you have a complete gho file, which usually has a .gho suffix and contains a backup

Recently, many netizens have asked the editor, what is the file hiberfil.sys? Can hiberfil.sys take up a lot of C drive space and be deleted? The editor can tell you that the hiberfil.sys file can be deleted. Let’s take a look at the details below. hiberfil.sys is a hidden file in the Windows system and also a system hibernation file. It is usually stored in the root directory of the C drive, and its size is equivalent to the size of the system's installed memory. This file is used when the computer is hibernated and contains the memory data of the current system so that it can be quickly restored to the previous state during recovery. Since its size is equal to the memory capacity, it may take up a larger amount of hard drive space. hiber
