Home >Backend Development >PHP Tutorial >Example of learning PHP voting program_PHP tutorial

Example of learning PHP voting program_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 17:21:44929browse

If you want to learn PHP, of course you must install PHP, so if you are learning for the first time, please read the online academy’s article first:

Installation of PHP4.03 under Linux

PHP4.04 Installation under win98

Installation of PHP4.04 under English win2000

If you cannot find the installation program, please download it below:

PHP4.04Beta WIN32 installation program


PHP4.03 source program


PHP3.0.16 WIN32 installation program


PHP3.0.16 source program

This This article is the continuation of "Learning Form Processing in PHP with Examples". Friends who are not familiar with form processing in PHP, please read the previous article - Learning Form Processing in PHP with Examples first.

After learning about form processing in the previous article, are you confident enough to do some small programs? OK, let’s start making something really useful! A PHP voting program that can be applied. In this article, you will learn the use of cookie technology, PHP array operations and file processing. how? Are you ready? Let's go!

Before starting the specific programming, let's first learn a few important concepts and functions that need to be used below:

The first is cookie, we need to use it to prevent the same machines for repeated voting.

So what are cookies? If your English is good enough and you want to eat a piece of original cookie, then come here to try it first; otherwise, you will have to eat the hot one provided by Jizo... (Don’t blame me if it’s not delicious, :- )) )

The original meaning of cookie in American English is cookie. Of course, we are not going to eat cookies now. Cookie here refers to an ASCII file with many restrictions. It is sent to the user by the server to record some information during the user's browsing process. The file size of cookies is limited to 4K. There are many uses for cookies. For example, some websites you have visited record the number of times you have visited them, and most of them use cookies. Here we use it to record whether the visitor has voted.

In PHP, we can use the setcookie function to use cookies very conveniently. Cookies are actually part of the header in the HTTP protocol. Therefore the setcookie function must be called before any other information is output to the browser. To put it simply, you need to use this function before the < html> mark... The following is an example of how to use setcookie, from the PHP4 Chinese manual of Xingkong Langzi. You can refer to our specific usage in the program later.

setcookie

Sends cookie information to the browser.

Syntax: int setcookie(string name, string value, int expire, string path, string domain, int secure);

Return value: integer

Function type : Network System

Content Description

This function will follow the header and send a small information string to the browser. Use this function before sending HTML data. In fact, cookies are also part of the header. The parameters of this function can be omitted except for the first name. The parameter name represents the name of the cookie; value represents the value of the cookie. If this parameter is an empty string, it means canceling the cookie data in the browser; expire represents the validity time of the cookie; path represents the relevant path of the cookie; domain represents the cookie. website; secure is only valid when the secure transmission of https is used.

The format of expire time is as follows:

Wdy, DD-Mon-YYYY HH:MM:SS GMT

GMT represents Greenwich Mean Time

Usage example

Setcookie() and header() examples provided by dante@mpath.com (27-May-1999).

  < ?php
$status = 0;
if (isset($myTstCky) && ($myTstCky == "ChocChip")) $status = 1;
if (! isset($CCHK)) {
setcookie("myTstCky", "ChocChip");
header("Location: $PHP_SELF?CCHK=1");
exit;
}
?>
  < html>
  < head>< title>Cookie Check< /title>< /head>
>
Cookie Check Status:
< ?php
printf ('< font color="#%s">%s< /font>< br>;',
$status ? "00FF00" : "FF0000",
$status ? "PASSED!" : "FAILED!");
?>
< /body>




How about it? Do you know how to use cookies? Jizo is here to tell you a little trick about the expire date. If you want the cookie's expire date to be the third day from the current day. Then you can use the time() function, which will return the current time in seconds (note! This time includes the year, month and day, isn’t it strange? :)), then if you want to expire The date is set as the third day, then it is time()+60*60*24*3.

The use of PHP arrays is very simple. You only need to note that its default starting subscript starts from zero like the C language. Of course, you can also set its own subscript, as follows:

$descArray=array(
1=>"English: source code, program download",
2=>"English: php dynamic",
3=>"English : News group, bulletin board",
4=>"English: Teaching",
5=>"Chinese: source code, program download",
6=>"Chinese: News Group, bulletin board",
7=>"Chinese: Teaching" );



When used $descArray[1]= "English: source code, program download ". What's even more amazing is that you can...  

$MyArray2 = array( "
Earthly Branches" => array("子", "Chou", "寅", "卯"),
"Zodiac" => array("Rat", "Ox", "Tiger", "Rabbit"),
"Number" => array(1, 2, 3, 4) );

How about using $MyArray2["Earthly Branches"][0]="子";? Isn’t it very humane? :)

Finally, let’s take a look at PHP’s file processing. There are about dozens of functions for file processing in PHP. In this section, we use one of them. Five functions: fopen(); fclose(); flock(); fexists(); fwrite(); Among them, I want to focus on flock(); for the others, you can check the manual yourself.

Why should we focus on flock()? Because this is a very important function for network programming, let me give you an example. Two people vote at the same time, and both choose option A. Assume that they open the data file at the same time. At this time, A’s vote is 2, and then both processes Add 1 to the original one, then one writes the data, and the other one also completes the writing. What do you think will happen at this time? What are A's votes? The correct result should be 4, but it is actually 3. Why is this happening? This is because of the characteristics of the multi-player environment on the Internet, so we must use the flock() function to lock the file before voting, and then open the file after voting to allow other processes to operate, so as to prevent the above situation from occurring. class error. The following is the usage instructions of flock function.

flock Lock the file.

Syntax: boolean flock(int fp, int operation);

Return value: Boolean value

Function type: File access

Content Description This function is used to lock the file so that other processes cannot access it. The parameter fp passed in is the index of the file. The value of the parameter operation is one of the following numbers:

1. Indicates that setting the locked file can allow other processes to read;

2. Indicates that only this process can write files;

3. Indicates that both reading and writing are locked;

4. The block is not locked.

The locking effect of this function is similar in both UNIX and Windows series. If the execution is successful, a true value is returned, otherwise a false value is returned.


Okay, we’ve learned the basics, let’s get started! Let’s download this sample program first. Then you can try it on your own platform first. I believe this will give you some perceptual understanding.

A total of three files vote.php, config.php, 1.gif and a file to save data are used in this application (the name of the file can be freely set, here we set it to sum. txt), vote.php is the main program file, and config.php is used to set some information that often needs to be modified.

//config.php file

Title
$title=Reader Type Questionnaire;

http://www.bkjia.com/PHPjc/532428.html

truehttp: //www.bkjia.com/PHPjc/532428.htmlTechArticleIf you want to learn PHP, of course you must install PHP, so if you are learning for the first time, please read the Online Academy first Articles: Installation of PHP4.03 under Linux PHP4.04 Installation of PHP4.0 under win98...
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