A simple free PHP counter _php tutorial

Source: Internet
Author: User
This is managed with a database.
Recommended database
CREATE TABLE Count (
UserID varchar () is not NULL,
Count varchar (not NULL),
IP varchar (NOT NULL)
);
Application Page


<title>Counter Request</title>


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


}
Else
{
$mysql _db=mysql_connect ("localhost", "root", "") or Die ("database connection failed");
mysql_select_db ("Footboy", $mysql _db) or Die ("Database Select Table 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 if the ID exists, and whether the input is correct
{
printf ("%s already exists or is entered as empty", $userid);
Print ("return");
Mysql_close ();
}
Else
{
$insert = "INSERT INTO Count VALUES ($userid, 1, $ip)";//meet conditions, record data
mysql_query ($insert);
Print ("$userid, congratulations on your application for a successful return
");
Print ("You can apply http://footboy.host.wayall.com/count/count.php?userid= $userid with the following code");
Mysql_close ();
}
}
?>


Application Page
$mysql _db=mysql_connect ("localhost", "root", "");//Connect to 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 if the user has applied
$count = $sql _row[count];//out counter data
Format the data as 5-bit
$count _len=strlen ($count);
for ($i =0; $i <5-$count _len; $i + +)
{$count = "0". $count;}
Get browse User IP to prevent duplicate refreshes
$ip =getenv ("remote_addr");
if ($num)
{
if ($ip! = $sql _row[ip])
{
$sql _row[count]++;//condition satisfies start count output
printf ("You are the first%s to browse this site
", $count);
mysql_query ("Update count set count= $sql _row[count],ip= $ip where userid= $userid");//Deposit data
Mysql_close ();
}
Else
{
printf ("You are the first%s to browse this site
", $count);
Print ("Welcome to this site again");
}
}
Else
printf ("%s user not present, please apply First", $userid);
Mysql_close ();
?>

http://www.bkjia.com/PHPjc/532167.html www.bkjia.com true http://www.bkjia.com/PHPjc/532167.html techarticle This is managed with a database. Recommended database CREATE TABLE count (userid varchar) NOT NULL, count varchar (+) NOT NULL, IP varchar (+) not NULL); Application Page Counter Application ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.