Counter The simplest PHP program--Counter
Principle:
1. The first user browses a certain page.
2. The server program reads the number of times the page has been viewed from the database or file.
3. Store the number plus one and send it back to the first user.
4. The second user browses a certain page.
5. The server program reads the number of times the page has been viewed from the database or file.
6. Save the number by one and send it back to the second user.
Functions you need to know:
fopen() opens a file
filesize() gets the file size
fseek() moves the file pointer
fgets() gets the content of the line where the file pointer is
fputs() writes the string as the position of the file pointer
fclose() closes the file
file_exists() determines whether the file exists
exec() executes an external program
The simplest counter:
/*
(c)1998 David W. Bettis
Here is the copyright information
*/
$counterFile = "counter.txt";
#here Define the counter file
function displayCounter($counterFile) {
$fp = fopen($counterFile,"rw");
#Open the file in read and write mode
$num = fgets($fp,5);
#Get the current number
$num += 1;
#Add 1
print "You are the "."$num"." boring person";
exec( "rm -rf $counterFile");
exec( " echo $num > $counterFile");
#Lazy way, write without using fputs
}
if (!file_exists($counterFile)) {
exec( "echo 0 > $counterFile"); #If the counter file does not exist, create it and set the content to 0
displayCounter($counterFile);
?>
PHP counter simpler version:
< ;?
#The copyright is gone, it’s that simple
$fp=fopen("counter.txt","r+");
flock($fp,3);
#Open the counter file and lock it
$fsize= filesize("count.txt");
$count=fgets($fp,$fsize+1);
$count++;
#Get the number and add one
fseek($fp,0);
fputs($fp, $count);
fclose($fp);
#Write new numbers to the file
echo "You are the $count visitor";
?>
PHP counter graphic version:
Create 10 pictures, I won’t go into details about how to combine digital strings with pictures. Assume that the pictures are 0.gif ~ 9.gif.
....$count is the obtained value
$strcount=strval($count);
$strcount=chop($strcount);
$countlen=$strlen($strcount);
$shtml="";
for ($i=0; $i<$countlen; $i++) {
$shtml. ="

}
echo $shtml;
?>
PHP count Server database version:
Use SQL counter, build the table first
CREATE TABLE counter
(
counter int not null,
id int not null
)
INSERT INTO counter(counter,id) VALUE( 0,1)
$c ..., ...);
#MySQL database connection
$sql="select * from counter";
$result=mysql_query($sql,$conn);
$objresult=mysql_fetch_object($result);
$count=$objresult->counter;
$count++;
$sql="update counter set counter=".$count."where id=1";
mysql_query($ sql,$conn);
mysql_close($conn);
echo "You are the $count visitor";
?>
The above has introduced the counter. The simplest PHP program - the counter, including the counter content, I hope it will be helpful to friends who are interested in PHP tutorials.

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
