A simple PHP voting program source code _ PHP Tutorial

Source: Internet
Author: User
Tags string back
A simple PHP voting program source code. Analysis: We use a file (data. dat) to store voting columns. each topic occupies one line. in this way, you can join and subtract the columns you want to vote. we use a file (analysis:
We use a file (data. dat) to store voting columns. each column occupies one row.
In this way, you can join and subtract the columns you want to vote.
We use a file (votes. dat) to store our voting results.
Record the IP address of the most recent voter to prevent multiple voting requests.
Therefore, you should create two files under the program directory: data. dat and votes. dat.
Program running process
When running the program, you should first have an HTML form for voting, waiting for voting.
When someone votes on an HTML form, determines whether its IP address is the IP address of the most recent voter,
If this person has just cast a vote, the two values should be equal and show "you have already cast a vote. thank you for your participation! "
If this person has not voted, the two values are not equal, and the vote is written to the file (votes. dat)
(The method for obtaining the voter's IP address here is: the Web client must provide a table recognition when sending requests to the server,
This table recognition is stored in the environment variable REMOTE_ADDR, and we can reference it .)
Finally, the voting result is displayed. Generally, the voting result is displayed in "bar chart + percentage.
Therefore, we need to select a rectangular image with a single color to meet the requirements of horizontal scaling.
--------------------------------------------------------------------------------
Program code part downloadable program code
// The file stored in the voting column. you can change the file name.
$ Data = "data. dat ";
// Stores the voting result file
$ Votes = "votes. dat ";
// Graphic file name used to display the percentage of voting results,
// Use a rectangle of a single color to facilitate horizontal scaling of voting results,
// Upload the file to the program directory
$ Path_img = "vote.gif ";
// Vote title
$ Poll_name = "which topic do you like me? ";
// The following parts do not need to be modified //
// Open the voting column file and store all the content in the form of an array in the variable $ dataf for backup.
$ Dataf = file ($ data );
If ($ go! = 1)
{
// Display the voting program form
Echo" $ Poll_name
N ";
Echo "";
Echo "view results ";
}
Else
{
// $ Go = 1 indicates that someone has voted. // You can read $ file_votes = fopen ($ votes, "r") from the file in another way ");
$ Line_votes = fgets ($ file_votes, 255 );
Fclose ($ file_votes );
// Cut the data according to the specified string, and then pass the string back to the array variable
$ Single_vote = explode ("|", $ line_votes );
// Prepare to write the voting result
If ($ result! = 1)
{
// Check whether the IP address is repeated
$ File_votes = file ($ votes, "r ");
If ($ REMOTE_ADDR = $ file_votes [1])
{
Echo" You have already voted. thank you for your participation! ";
Exit;
}
// If the IP address is not repeated, execute the following program
$ Ficdest = fopen ($ votes, "w ");
For ($ I = 0; $ I <= count ($ dataf)-1; $ I ++)
{
// Determine the topic to be cast
If ($ I = $ vote)
{
$ Single_vote [$ I] + = 1;
}
// Write data back to a file
Fputs ($ ficdest, "$ single_vote [$ I] | ");
}
// IP address of the writer
Fputs ($ ficdest, "n $ REMOTE_ADDR ");
Fclose ($ ficdest );
$ Result = 1; // vote successful
}
// Write the voting result and display the voting result
If ($ result = 1)
{Echo"











";For ($ I = 0; $ I <= count ($ dataf)-1; $ I ++){// Obtain the total number of votes$ Tot_votes + = $ single_vote [$ I];}For ($ I = 0; $ I <= count ($ dataf)-1; $ I ++){// Calculate the percentage$ Stat [$ I] = $ single_vote [$ I]/$ tot_votes * 100;Echo" ";}Echo"
  • ";
    Echo "$ dataf [$ I]
  • ";
    Echo "";
    // Output percentage printf ("%. 1f", "$ stat [$ I]");
    Echo "%
    "; // Number of votes in this topic
    Echo "$ single_vote [$ I]"; echo"

    ";
    Echo "Total votes: $ tot_votes ";
    }
    }
    ?>

    Using a file (data. dat) to store voting columns. each topic occupies one line. in this way, you can join and subtract the columns you want to vote. we use a file (...

    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.