Php voting class-PHP source code

Source: Internet
Author: User
Voting type
 Errmsg) {case 1: return "successful first vote"; break; case 2: return "successful again"; break; case-1: return "incorrect vote"; break; case-2: return "please try again tomorrow"; break; default:; break ;}} private function conn () {// create a database connection $ this-> link = mysql_connect ($ this-> host, $ this-> name, $ this-> password) or die ("host connection failed! "); // TODO Connect Database $ charset = 'utf8'; mysql_query (" set names ". $ charset); // set the character set $ this-> db = mysql_select_db ($ this-> dbname, $ this-> link ); // TODO Open Database} public function _ construct ($ item) {$ this-> time = time (); $ this-> ip = $ this-> getIP (); // Current IP $ this-> conn (); if ($ item> $ this-> MAXvotes | $ item <0 |! Is_int ($ item) {// echo 'vote item error'; $ this-> errmsg =-1; // incorrect voting item} if ($ this-> chkVoteAble ($ this-> getIP () {// echo 'voting allowed '; $ this-> vot ($ item);} else {// echo 'current IP cannot vote '; $ this-> errmsg =-2; // The current IP cannot be voted, please vote again tomorrow} private function chkVoteAble ($ ip) {// TODO check whether the vote can be performed $ SQL = "select ip, date from user where ip = '$ IP '"; $ result = mysql_query ($ SQL); if (! $ Result) {return true; // no records in the database, you can vote} else {$ row = mysql_fetch_row ($ result); if (time ()> $ row [1]/* date */+ $ this-> t) {// return true if the limit period is exceeded;} else {return false ;};}} private function vot ($ item) {// TODO vote if (is_int ($ item )) {// write user database $ SQL _chkuser = "select ip from user where ip = '{$ this-> ip}'"; if (mysql_fetch_array (mysql_query ($ SQL _chkuser ))) {$ this-> errmsg = 2; // The vote is successful again; mysql_query ("UPDATE user set da Te = {$ this-> time} where ip = '{$ this-> ip }'"); // update current time} else {// record for no current IP address in the database mysql_query ("insert user VALUES ('', '{$ this-> ip }', {$ this-> time}) "); $ this-> errmsg = 1; // The first successful vote }; // write the voting database $ SQL = "select id from item where id = {$ item}"; $ result = mysql_query ($ SQL); if (! Mysql_fetch_row ($ result) {$ this-> errmsg =-1; // echo "no such item, error"; return false ;} else {$ SQL _getvotes = "select votes from item where id = {$ item}"; $ votes = mysql_fetch_row (mysql_query ($ SQL _getvotes); $ votes = $ votes [0]; $ votes ++; $ SQL _setvotes = "UPDATE item set votes = {$ votes} where id = {$ item}"; // add one if (mysql_query ($ SQL _setvotes )) {return true;} else {return false; // SQL syntax error, failed, impossible} else {return False; // Parameter error};} private function getIP () {// obtain the user IP if (getenv ('http _ CLIENT_IP ')) {$ onlineip = getenv ('http _ CLIENT_IP ');} elseif (getenv ('http _ X_FORWARDED_FOR') {$ onlineip = getenv ('http _ X_FORWARDED_FOR ');} elseif (getenv ('remote _ ADDR ') {$ onlineip = getenv ('remote _ ADDR ');} else {$ onlineip = $ HTTP_SERVER_VARS ['remote _ ADDR '];} return $ onlineip ;}}?>

2. vo. php

 

3. toupiao. SQL

-- PhpMyAdmin SQL Dump -- version 3.5.2.2 -- http://www.phpmyadmin.net---- Host: localhost -- generation date: September 12, 2012 -- Server version: 5.5.24-log -- PHP version: 5.4.3SET SQL _MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+ ";/*! 40101 SET @ OLD_CHARACTER_SET_CLIENT = @ CHARACTER_SET_CLIENT */;/*! 40101 SET @ OLD_CHARACTER_SET_RESULTS = @ CHARACTER_SET_RESULTS */;/*! 40101 SET @ OLD_COLLATION_CONNECTION = @ COLLATION_CONNECTION */;/*! 40101 set names utf8 */; ---- database: 'toupiao' ---- ------------------------------------------------------------ TABLE structure 'item' -- create table if not exists 'item' ('id' int (11) not null AUTO_INCREMENT COMMENT 'item', 'Votes 'Int (11) not null default '0' comment' votes ', primary key ('id ')) ENGINE = InnoDB default charset = utf8 COMMENT = 'ITEM' AUTO_INCREMENT = 36; ---- data 'item' in the stored table -- insert into 'item' ('id', 'Votes') VALUES (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0 ), (7, 0), (8, 0), (9, 0), (10, 0), (11, 0), (12, 0), (13, 0), (14, 0), (15, 0), (16, 0), (17, 0), (18, 0), (19, 0 ), (20, 0), (21, 0), (22, 0), (23, 0), (24, 0), (25, 0), (26, 0), (27, 0), (28, 0), (29, 0), (30, 0), (31, 0), (32, 0 ), (33, 0), (34, 0), (35, 0); -- ------------------------------------------------------------ TABLE structure 'user' -- create table if not exists 'user' ('id' int (11) not null AUTO_INCREMENT, 'IP' varchar (15) not null default ''comment 'user IP', 'date' int (11) not null default '0' comment' determines whether voting can be performed on an int date. ', primary key ('id') ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 6; ---- 'user' -- insert into 'user' ('id', 'IP', 'date') VALUES (1, '123. 0.0.11 ', 0), (2, '123. 168.1.100', 0), (3, '100. 168.1.101 ', 0), (4, '123. 0.0.12 ', 1347469612), (5, '123. 0.0.1', 1347471829 );/*! 40101 SET CHARACTER_SET_CLIENT = @ OLD_CHARACTER_SET_CLIENT */;/*! 40101 SET CHARACTER_SET_RESULTS = @ OLD_CHARACTER_SET_RESULTS */;/*! 40101 SET COLLATION_CONNECTION = @ OLD_COLLATION_CONNECTION */;

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.