Home >Backend Development >PHP Tutorial >A simple free php counter_PHP tutorial

A simple free php counter_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:24:20790browse

This is managed using a database.
Recommended database
create table count(
userid varchar(20) not null,
count varchar(20) not null,
ip varchar(20) not null
);
Application page


Counter application



if(! isset($submit))
{
?>



}
else
{
$mysql_db=mysql_connect("localhost", "root","") or die("Database connection failed");
mysql_select_db("footboy",$mysql_db) or die("Database table selection failed");
$sql="select * from count where userid=$userid";
$result=mysql_query($sql);
$num=mysql_num_rows($result);
$ip=getenv("remote_addr");

if($num || $userid=="")//Determine whether the ID exists and whether the input is correct
{
printf("%s already exists or the input is empty",$userid);
print("return");
mysql_close();
}
else
{
$insert="INSERT INTO count VALUES($userid,1,$ip)" ;//Meet the conditions and record the data
mysql_query($insert);
print("$userid, congratulations on your successful application and return
");
print("You can use the following code to apply http ://footboy.host.wayall.com/count/count.php?userid=$userid");
mysql_close();
}
}
?>


Application page

$mysql_db=mysql_connect("localhost","root","");//Connect to the database
mysql_select_db("footboy",$ mysql_db);
$sql="select * from count where userid=$userid";//Database query
$result=mysql_query($sql);
$sql_row=mysql_fetch_array($result);
$num=mysql_num_rows($result);//Determine whether the user has applied
$count=$sql_row[count];//Get the counter data
//Format the data into 5 digits
$ count_len=strlen($count);
for($i=0;$i{$count="0".$count;}
//Get Browse user IP to prevent repeated refreshing
$ip=getenv("remote_addr");
if($num)
{
if($ip!=$sql_row[ip])
{
$sql_row[count]++;//When the conditions are met, start counting output
printf("You are the %sth person to browse this site
",$count);
mysql_query( "update count set count=$sql_row[count],ip=$ip where userid=$userid");//Save data
mysql_close();
}
else
{
printf("You are the %sth person to visit this site
",$count);
print("Welcome to visit our site again");
}
}
else
printf("%s user does not exist, please apply first", $userid);
mysql_close();
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532167.htmlTechArticleThis is managed using a database. It is recommended that the database create table count( userid varchar(20) not null, count varchar(20) not null, ip varchar(20) not null ); Application page Counter application...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn