我需要根据输入的代码将用户重定向到另一个页面

我需要根据输入的代码将用户重定向到另一个页面

问题描述:

I need to create a form on my site where users enter a code and based on that code they're redirected to another page. Doing it through a database would be more secure but I wouldn't mind if the logic was handled on the form itself.

So, here's an example of what I mean:

Enter Code: (Box here)

Form searches through list of potential answers( s5d11, wy4sd, lk123, etc.), if it finds a match it redirects to a specific page for each code, if not it gives an error message that the code entered was wrong.

我需要在我的网站上创建一个用户输入代码的表单,并根据该代码重定向到 另一页。 通过数据库完成它会更安全,但我不介意逻辑是否在表单本身处理。 p>

所以,这是我的意思的一个例子: p>

输入代码:(此处为方框) p>

表单搜索潜在答案列表(s5d11,wy4sd,lk123等),如果找到匹配项,则会重定向到每个代码的特定页面,如果没有,则会显示输入的代码错误的错误消息。 p > div>

This is an example with two files, one html and one php file. The html-page submits the contents of your input to a php-page, which redirects the browser to an URL based on the input.

HTML, index.html:

<form action="redirect.php" method="get">
  <input name="mytext" />
  <input type="submit" />
</form>

PHP, redirect.php:

<?
switch($_GET['mytext'])
{
    case 's5d11':
        header('Location: http://somepage.com/');
        break;
    case 'qy4sd':
        header('Location: http://someotherpage.com/');
        break;
    default:
        print "<p>Wrong code. Try again</p>";
        include('index.html');
        break;
}
?>

Try this. I just whipped this up. You have a code and a path to redirect to for each code. You'll need to apply your own sanitation of course and any other standards. I put them all uppercase...

<?php

$_POST['CODE'] = 'CODE'; // Simulate post data...
$array_of_codes = array('CODE' => 'pathtoredirect');
$get_code = addslashes(strtoupper($_POST['CODE']));

if(array_key_exists($get_code, $array_of_codes))
{
    header("Location:".$array_of_codes[$get_code]."");
}else
{
    echo "Error: Code not in array.";
}
?>