Cool Poll Script for PHP

Created: October 29, 2014
Last Modified: March 8, 2021
Subscribe to Internet Tips and Tools Feed

This script adds a custom poll to your website using PHP and MySQL:


  • Uses advanced techniques to avoid votebots
  • Choose to allow users to vote 'Other' and type in answer
  • Activate or Archive 'Other' votes
  • Close poll but still display votes
  • Works with a login script or tracks users votes by cookies and/or ip address
  • Hosted on your own server for better security
  • May use one MySQL database for all your polls
  • Fully customize look and color of polls using CSS
  • New! Automatically uses MySQLi or MySQL

Example Poll: Which Superhero is your Favorite?

Your Vote:
1. Batman
(112 votes) 26%
2. Superman
(65 votes) 15%
3. Spider-Man
(44 votes) 10%
4. Flash
(31 votes) 7%
5. Deadpool
(26 votes) 6%
6. Captain America
(20 votes) 5%
7. Hulk
(20 votes) 5%
8. Iron Man
(18 votes) 4%
9. Wolverine
(15 votes) 3%
10. Green Lantern
(11 votes) 3%
11. Other
(24 votes) 5%

Total Votes: 437



Downloaded 0 times.
Please make a donation to reveal the download link.


There are two files in called coolpoll.php and db.php. Unzip both files and after editing coolpoll.php upload them to your server.

Paste the following code at the very top of your web page:

<?PHP session_start(); ?>

Paste the following where you want the poll to display on your page:

	$poll_secret = "secret123"; // this should match the variable $poll_password in coolpoll.php

Edit the following variables in coolpoll.php:

/* Edit the variables below */

$poll_name = "Superhero"; // Every poll should have a unique poll name (50 characters max)

$poll_answers = array ("Batman", "Captain America", "Catwoman", "Daredevil", "Flash", 
"Green Lantern", "Hulk", "Iron Man", "Robin", "Rogue", "Shadowcat", "Spider-Man", 
"Superman", "The Tick", "Thor", "Wolverine", "Wonder Woman");

/* 	$_SESSION['user_id'] is how this script allows users to submit votes if $require_login = 1;
They must be logged
in and a $_SESSION['user_id'] must be set.  Instead, you may be using $_SESSION['username'] 
on your server.  In that case you will need to change the poll_users table
to have something like "user_id VARCHAR(50)," in place of "user_id INT unsigned," and you
will need to replace all references of $_SESSION['user_id'] in this script to $_SESSION['username']
$poll_password = "secret123";
$require_login = 0; // 1 = yes; 0 = no 	
$use_cookies = 1; // 1 = Use cookies to keep track of votes. Only applies if $require_login = 0
$use_ip = 1; // Use IP Addresses to keep track of votes. Only applies if $require_login = 0
$your_login_page = "login.php"; /* Put the link to your login page here */					
$results_limit = null; // If you want to limit the results to display only the top results enter the limit here
$allow_voting = 1; // 0 = Close poll voting; 1 = Allow voting
$allow_other = 1; // 0 = Don't have "Other" field; 1 = "Other" field for user to type in answer
$admin_email = "Your name <>"; // You will receive email if a user selects "Other" if $allow_other 
$admin_username = "Admin";
$admin_password = "random123";
/* $bar_style is the style of the percentage bar for each poll result. You can also
keep this variable blank and style the bar in a CSS style sheet because each bar
has a class='bar_style'. Also you can style each bar differently because each bar 
will have an id of bar1, bar2 etc... Example:
.bar_style { height: 1em; background-color: #bbbbbb; }
#bar1 { background-color: red; }
#bar2 { background-color: blue; }
$bar_style = "background-color:#bbbbbb; height: 0.5em;";
/* It is not recommended but $allow_other = 2 will automatically show what users type into
the "Other" field as part of the poll, but they could enter off the topic answers or bad words.
$allow_other = 1 will let users type into "Other" field, but the admin will have to manually
activate the field.  An email will be sent to $admin_email when a user selects "Other". You can
email more than one address with a , separating the email addresses.

To display the "login as admin" prompt put this on the url:


March 8, 2021 - Version 1.2d - Bug Fix - Put inet_pton() inside of escape_string because on the outside mysql sometimes has an error because inet_pton() sometimes puts an unescaped single quote in the string.

March 26, 2019 - Version 1.2c - Fixed "warning: undefined variable" if PHP is set to display warnings.

August 9, 2017 - Version 1.2b - Bug fix in db.php - Changed if ($server_has_mysqli) to if ($GLOBALS["server_has_mysqli"]) throughout. Also changed $db_link->affected_rows() to $db_link->affected_rows.

5/4/2017 - Version 1.2a - 'Other' votes were not being calculated correctly on line 759. Fixed by adding && $row['votes'] >= 1

5/2/2017 - Version 1.2 - Added db.php which is required by coolpoll.php to support MySQLi and MySQL. The script automatically uses MySQLi if your server supports it.

4/26/2016 - Version 1.1d - If $results_limit is set to just display the top results now 'Other' is displayed at the bottom with all the other results tallied together.

4/26/2016 - Version 1.1c - Added option on line 49:
$hide_results = 1; // 0 = Don't hide results; 1 = Hide results until user votes

5/5/2015 - Version 1.1b - Bug Fix: If $allow_other was set to 0 then when a person voted it was not displaying the results because of two bugs: 1. Around line 770 forgot to close html select if not allowing other vote. 2. Around line 887 needed to check to make sure 'other_div' existed before trying to not display it.

1/8/2014 - Version 1.1 - Bug Fix: Even when logged in as admin was still only showing $results_limit of answers. Added || isset($_SESSION['admin_username']) to line 689 to fix and show all answers for admin.

1/8/2014 - Version 1.1 - Bug Fix: The row for each answer was being increased even if a poll answer was not being shown because it was archived. Fixed by removing answer_num++ from line 745 and adding it to lines 717 and 724.

10/29/2014 - Version 1.0 - Cool Poll Script Created

Back to
Subscribe to Internet Tips and Tools Feed        

User Comments

There are 0 comments.

Displaying first 50 comments.