Cool Poll Script for PHP

Created: October 29, 2014
Last Modified: January 2, 2017
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

Example Poll: Which Superhero is your Favorite?

Your Vote:

Vote to see poll results
Total Votes: 273


Downloaded 0 times.
This script is provided for free but please consider making a donation to help with server costs and other expenses.


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:


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.