0

I am looking to do something along the lines of what is shown here: http://strawpoll.me

When you create a poll on there it gives you a unique url to which you and anyone else can access it by visiting that url.

Now obviously they can't be cluttering up their server with thousands of individual files containing the polls and I'm assuming they have one basic template to which they load the poll information from a database.

I want to make a form and when the user submits it they get their own unique link like how it is on the strawpoll website.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mr.Smithyyy
  • 2,157
  • 12
  • 49
  • 95
  • 1
    possible duplicate of [How to code a URL shortener?](http://stackoverflow.com/questions/742013/how-to-code-a-url-shortener) – Panama Jack Jun 29 '15 at 14:24

2 Answers2

1

As is it is a simple dynamic page creation requiring only few files...

  1. The poll creation page which is static
  2. The processing.php file which collects the data and inserts into the db
  3. The dynamic poll rendering page polls.php which renders the poll based on an auto-incremented id assigned to the poll.
  4. The polls-results.php which returns the results.

http://thepollsite.com/12345

.htaccess is used to identify which poll to render as well as which results to render.

# Turns on Mod-Rewrite Engine
# ----------------------------------------------------------------
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

# Poll Pages 
# ----------------------------------------------------------------
RewriteRule ^([^/]+)/?$ polls.php?id=$1 [QSA,L]
RewriteRule ^([^/]+)/?$ polls-results.php?id=$1 [QSA,L]

On both polls.php and polls-results.php you get the id with: $poll_id = $_REQUEST['id']; and process from there.

The embed code is rendered in a simple dialog pop which provides the iframe code as shown.

Though there are frills you can add... its is really a simple process.

petebolduc
  • 1,233
  • 1
  • 13
  • 20
1

On the .htaccess side, you'll want something like:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

With that in place, all requests for files/directories that don't already exist on the server will run index.php instead of showing a 404 page. Then, to get the requested path, use $_SERVER['PATH_INFO'] in your index.php.

This is the general method of using "clean" URLs with PHP. It's probably already been said in another SO answer, but it's hard to find so I wrote it again.

Related: Why use a single index.php page for entire site? and How to do URL re-writing in PHP?

Community
  • 1
  • 1
nkorth
  • 1,684
  • 1
  • 12
  • 28