PHP script is not redirecting to different website

I'm trying to use header() to redirect to a site chosen from a drop-down list. But when I choose any option in the list, I get the "This page cannot be displayed" error in IE. Other browsers don't work either. Please advise.

<?php
  if(isset($_POST['site']))
  {
    header("Location:" . $_POST['site']);
    exit;
  }
?>
<!DOCTYPE html>
 <html>
<head>
    <title>PHP</title>
 
</head>
<body>
 <form action="index.php" method="post">
   <select name="site" id="site">
     <option value="htttp://www.google.com">Google</option>
     <option value="htttp://www.yahoo.com">Yahoo</option>
     <option value="htttp://www.microsoft.com">Microsoft</option>
   </select>
   <input type="submit" value="Submit" />
 </form>
 
</body>
</html>

Open in new window

ArchimelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mukesh YadavFull Stack DeveloperCommented:
Replace htttp to http in the following html.

<option value="htttp://www.google.com">Google</option>
<option value="htttp://www.yahoo.com">Yahoo</option>
<option value="htttp://www.microsoft.com">Microsoft</option>

Open in new window


:)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
Mukesh is correct.  I also replaced 'index.php' with '#' which make the page post to itself.
<?php
  if(isset($_POST['site']))
  {
    header("Location:" . $_POST['site']);
    exit;
  }
?>
<!DOCTYPE html>
 <html>
<head>
    <title>PHP</title>
 
</head>
<body>
 <form action="#" method="post">
   <select name="site" id="site">
     <option value="http://www.google.com">Google</option>
     <option value="http://www.yahoo.com">Yahoo</option>
     <option value="http://www.microsoft.com">Microsoft</option>
   </select>
   <input type="submit" value="Submit" />
 </form>
 
</body>
</html>

Open in new window

0
Ray PaseurCommented:
You might want to take a few moments to study PHP Security before you install a script like this.  It would be very easy for a malicious hacker to make you the number one referrer to pornography or terrorist web sites, and I'm pretty sure you do not want the government attention that will inevitably follow.
http://php.net/manual/en/security.php

This shows a safer way to do it.  In this example your script controls the data.  In the original example, your script used external data without filtering or sanitizing it, so your script can become an open relay for hacks.  By relying on your internal PHP data instead of external ($_POST) data, you're safer.
http://iconoun.com/demo/temp_archimel.php

<?php // demo/temp_archimel.php

/**
 * http://www.experts-exchange.com/questions/28709886/PHP-script-is-not-redirecting-to-different-website.html
 *
 * Demonstration of doing it the safer way
 * http://php.net/manual/en/security.php
 *
 * New to PHP:
 * http://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
 */
error_reporting(E_ALL);
ob_start();


// ACTIVATE THIS TO SEE WHAT THE REQUEST VARIABLE CONTAINS
// var_dump($_POST);


// THE SITES WE CAN USE
$sites =
[ 'Choose Site' => NULL
, 'Google'      => 'http://www.google.com'
, 'Yahoo!'      => 'http://www.yahoo.com'
, 'Microsoft'   => 'http://www.microsoft.com'
]
;
// GET THE SIGNAL AND USE IT TO LOOK UP THE SITE LOCATION
$signal = !empty($_POST['signal']) ? (string)$_POST['signal'] : NULL;
$locate = !empty($sites[$signal])  ? $sites[$signal]          : NULL;

// ONLY REDIRECT IT THERE IS A VALID SIGNAL
if ($locate)
{
    header("Location: $locate");
    exit;
}


// PREPARE THE OPTION TAGS
$options = NULL;
foreach ($sites as $signal => $url)
{
    $options .= '<option value="' . $signal . '">' . $signal . '</option>' . PHP_EOL;
}


// PREPARE THE HTML DOCUMENT USING HEREDOC NOTATION
$html = <<<ENDHTML
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<title>PHP With Better Security</title>
</head>

<body>
 <form method="post">
   <select name="signal">
$options
   </select>
   <input type="submit" value="Submit" />
 </form>

</body>
</html>
ENDHTML;

echo $html;

Open in new window

If you're new to PHP and want some good learning resources, this article can help guide you.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
ArchimelAuthor Commented:
Thank you. I must have been getting dyslexic to overlook that. It happens when I get tired.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.