1. Computing

Discuss in my forum

Angela Bradley

Drop Down Menu Redirection

By , About.com GuideApril 20, 2006

Follow me on:

If you go to PBS.org's home page, you will notice a drop down menu called "Explore" that lets you navigate to different areas of the web site.   Drop down menu navigation like this is popular because it lets you add a lot of navigation options, without taking up much space.  Most sites wouldn't use PHP to do this, but one way to replicate this with PHP is to setup an HTML form somewhere on your page and have it pass the site's URL to a PHP page when you submit it.  For example:

<form action="jump.php" method="post">
<select name=url>
<option value="http://php.about.com">About PHP</option>
<option value="http://www.identity.st">Identity</option>
</select>
<input type="submit" value="Go">
</form>


Then have the PHP file jump.php use basic redirection to send them to the right page.  Something like this:

<?php
$url = $_POST["url"];
header("Location: $url");
?>
Comments
April 21, 2006 at 8:39 am
(1) G says:

Sweet stuff, thanks ! i tried to do this myself but i’m not very skilled at php so i got stuck. This works perfectly :)

May 12, 2006 at 1:03 pm
(2) Dave says:

This is a bad idea. This script is wide open for abuse, letting 3rd parties use your site to redirect users to anywhere on the internet.

At minimum, you should check the $_SERVER['HTTP_REFERER'] to make sure the POST is coming from your site, but even that can be spoofed since it is information supplied by the client.

A better way would be to hard-code the redirect URLs in the PHP script itself, and have the form POST tokens to the script. The script would know what the tokens meant, and which redirect URL to use.

May 12, 2006 at 2:40 pm
(3) php says:

I’m sorry you don’t like the idea Dave. This was meant to be a simple solution for people learning PHP. While more security could be built into it if the programmer felt the need, I think this conveys the basic idea of how to do the menu. Like all of our learning scripts, we encourage people to build upon this base to fit their needs and to improve them as they learn more PHP.

May 31, 2006 at 4:59 pm
(4) Brenty says:

This allows the link to open up in the same window. What would I need to do to allow it to work in a new window?

May 18, 2007 at 5:23 pm
(5) Mike says:

How would i do it without a drop down menu but rather an input type text field where the user inputs a certain value and it redirects to a certain page on the site. And if they type something different it goes to a different part on the site?

June 15, 2007 at 9:54 pm
(6) Regina says:

Thanks for this information. I searched for this code all over the net. It was too complicated for me to understand. Then I found you! THANKS!!

October 15, 2007 at 3:33 pm
(7) michael says:

you can send a variable back to the same page and write an if statement that included the file or redirect based on the variable. Is this possible?

November 20, 2007 at 10:04 am
(8) Rosário says:

Hello, your tutorial is too simple and very interesting. I found it usefull and easiest way on how to use a DropDown list. Thanks you, and I am sorry for my poor english.

December 10, 2008 at 5:54 am
(9) damanpreet says:

sir/madam who is suppose to answer php problem plz give me u id i need help for php as my skills are very week but i am working on project plz do help me out

December 17, 2008 at 8:55 am
(10) fraze says:

thanks

January 8, 2009 at 11:47 pm
(11) rrr says:

To open it in a new window, just add the attribute for the form element, target=”_blank”

it looks like this

php site
site

January 8, 2009 at 11:49 pm
(12) rrr says:

sorry this webpage is not allowing me to enter the complete html code for the form.
so
the code after adding it to form is like this

January 28, 2009 at 12:29 pm
(13) Harpsih says:

Very simple idea, will try it out and report back on what happens.

February 18, 2009 at 3:29 am
(14) Becky says:

This was such a great help! I was trying to do something else (make a contact form that takes you to a thank you page at the end) but because you boiled it down so simply I was able to figure it out. Thank you SO MUCH! :)

April 4, 2009 at 8:05 pm
(15) Rob Grattan says:

Sorry – the stupid flashing ad telling me I’d won something just put me off.

Didn’t read any further…

May 6, 2009 at 10:17 am
(16) Parham Doustdar says:

For the page itself redirecting you to another page, you can put something like this. I’m writing the algorithm, and not the code itself since I’m guessing the web application wouldn’t allow me to. So here it is:
Before the HTML tag, put this:
if $_post['url'] exists //can do this with the isset() function.
header(…);

Then open the HTML tag and so on.

Good luck!
P.S. I have just started learning PHP for two days, so correct me if I’m wrong.

May 22, 2009 at 11:51 am
(17) FeralReason says:

This is a great approach. I have a pulldown list where I want to select and pass a record ID to a specific URL, which uses that ID to SELECT data for its own form. Since this (target) file’s form’s action is $_SERVER["PHP_SELF"], I couldn’t use the (target) filename as the ACTION in my pulldown file (wrong $_POST variables). So I’m taking your generalized jump file and concatenating the target file’s URL with an integer value from the pull down list e.g.: “myfile.php?var=N” and passing my record ID for the SELECT in that way. I am still interested in some more elaboration on security implications and alternative ways to go from pulldown lists to other files ! THanx !

November 23, 2009 at 10:38 am
(18) FIlipe says:

Reply @ (2) comment:
This is not unsecure. The “abuse” you refer too is just people using the redirect for their own websites, which poses no threat on website security, just possible abuse of bandwith, nothing else. To prevent it you can just include a check for “http://” on $_POST['URL'], avoiding redirects to external websites, which would null the point of using it in 3rd party sites.
Once again, it still poses no security threat as most of the simpler solutions never do.
The simpler the code, the safer it is.

January 19, 2010 at 4:07 pm
(19) Setarkos says:

I wanted to have that without javascritpt – now I have it.
Thx a lot!

Regards, setarkos

April 17, 2010 at 1:20 am
(20) prakash says:

how to learn php if easy way to learn any idiya for this plz inform me very effectiveness

September 22, 2010 at 12:33 am
(21) Rakesh Kumar Mishra says:

This is very good script for PHP beginners.

December 29, 2010 at 10:16 am
(22) Rob says:

Referring to Comments 2 & 18…

FIlipe is correct; the only user a potential hacker would be able to redirect to a page other than the ones specified, would be him or herself! Now if the user were generating the form dropdown values for OTHER users to click on, that could pose a problem. Otherwise, Dave might be confusing header redirects (where a user can be directed only to a web-accessible directory or site) with file includes (where a user can include and therefore view any file on the server). You would not want to use the above technique for file includes without checking the user-submitted value thoroughly. Ideally you would map such values to a desired directory, and strip out all characters which could be used to access directories outside that directory. I use the following technique:

<?php

// Strip offending characters from string

$clean_path = str_replace(‘../’,”,trim(trim($user_submitted_path,’/')));

// Map string to given directory

$file = ‘./path/to/directory/’.$clean_path;

if (is_file($file)) {
// Do something if it’s a valid file…
} else {
// Do something if it’s NOT a valid file…
}

?>

January 1, 2011 at 12:27 pm
(23) uut3 says:

thanks for the tutorial…
it give me some ideas…
:D

April 13, 2011 at 4:30 am
(24) canndy girl says:

Great post. You seem to have a good understanding that how to create a professional drop down menu. When I entering your blog, I felt this. Come on and keep writing your blog will be more attractive. To Your Success!

April 26, 2011 at 9:58 pm
(25) teetee says:

With reference to security, test for possible sites and if no matching site, goto to default site.

July 2, 2011 at 7:14 pm
(26) Casey says:

Finally, I found someone’s page who had the code I needed!

Thanks Angela!

November 13, 2011 at 4:56 am
(27) dropbox portable says:

Thanks, was most helpful

November 16, 2011 at 11:12 pm
(28) Imran says:

Simple Fabolous !

November 28, 2011 at 2:22 pm
(29) Unagantesia says:

Thanks for your personal amazing posting :)

December 20, 2011 at 2:03 am
(30) Karthick says:

Thanks Angel. This is very nice stuff.

January 1, 2012 at 10:36 am
(31) Anil says:

nice stuff…..this is really good. i just read this and got lot’s more info regarding php. thanks dude..

February 11, 2012 at 9:49 am
(32) Håvard F says:

I see that this in a way could be abused by other people. A simple way to improve the security on it is very simply to give each option tag a number or an ID instead of an URL, then make PHP in the other file decide by that ID what URL to use. I understand that beginners care little about security, but it’s a good habit to not let the user throw any string into the PHP-code.

inside first file: (note that the urls are exchanged with IDs)

About PHP
Identity
The Guardian
YouTube

inside jump.php:
“http://php.about.com”,
2 => “http://www.identity.st”,
3 => “http://www.guardian.co.uk”,
4 => “http://www.youtube.com”
);

//Redirects to the url in the list according to the id given from $_POST
header(“Location: $list[$pid]“);

?>

February 11, 2012 at 9:57 am
(33) Håvard F says:

Hehe, I see that parts of the last comment disappeared (because of the html code), but I’ll try again.

The code in the first file is almost the same as the one used in the article, but the URLs are exchanged with numbers (IDs)
In the second file:

//assign requested page ID
$pid = $_POST["url"];

//the numbers must be the same as the paired option ID in the first file
$list = Array(
1 => “http://php.about.com”,
2 => “http://www.identity.st”,
3 => “http://www.guardian.co.uk”,
4 => “http://www.youtube.com”
);

//Redirects to the url in the list according to the id given from $_POST
header(“Location: $list[$pid]“);

March 28, 2012 at 5:10 pm
(34) Robert R says:

http://yourlinkhere.com',
2 => ‘http://yourlinkhere2.com‘ //Note that the last array item doesn’t have a comma at the end
);

//Now we check to make sure that that marker is in the array and exists:
if( isset($list[$url_id]) )
{
$redirect = $list[$url_id];
}
else
{ // Insert default URL you want them to fall back to:
$redirect = $list[0]; //This defaults to the first item in the above array
}
header(“Location: ” . $redirect ); // Separate the variable from the string (text)
}
else
{
header(“Location: http://yourredirect.com“); //Just something to serve in case POST isn’t set.
}

// Code can always be improved, please share.
?>

March 28, 2012 at 5:14 pm
(35) Robert R says:

Trying again, treated part of the tags as HTML, hopefully this one works.

// To build on what others posted for basic validation (checking)
// Some server builds require a check that the POST is present otherwise throw an error
// We will also do a check for integer (number)
if( isset($_POST['url']) && is_int($_POST['url']) )
{
$url_id = $_POST['url'];
$list = array( // following what Harvard built upon above…
1 => ‘http://yourlinkhere.com',
2 => ‘http://yourlinkhere2.com‘ //Note that the last array item doesn’t have a comma at the end
);

//Now we check to make sure that that marker is in the array and exists:
if( isset($list[$url_id]) )
{
$redirect = $list[$url_id];
}
else
{ // Insert default URL you want them to fall back to:
$redirect = $list[0]; //This defaults to the first item in the above array
}
header(“Location: ” . $redirect ); // Separate the variable from the string (text)
}
else
{
header(“Location: http://yourredirect.com“);
}

April 19, 2012 at 11:33 am
(36) chanel bag on sale says:

good very,good very,

April 25, 2012 at 5:22 pm
(37) sanjay says:

really,its simple but proved very helpful,thanks

May 11, 2012 at 2:43 am
(38) SingaporeCondo says:

Thanks a lot!
Great help for me.

June 18, 2012 at 9:26 am
(39) Malini says:

Thanks.
It worked!! :)

September 4, 2012 at 6:27 am
(40) mahesh says:

I wanted to have that without javascritpt – now I have it.
Thx a lot!

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>
Top Related Searches drop down menu

©2013 About.com. All rights reserved.