如何在提交特定访问代码时将用户发送到某个网站

时间:2017-09-16 00:54:11

标签: javascript php html forms redirect

如果在表单中提交某个访问代码,我一直在寻找多种方法将用户重定向到html页面。我有表格并且工作正常,但是我无法将其重定向到正确的页面,我已经看到了另外一个这样的帖子,但具有特定的用户登录。

<?php
$_GET["code"] = $accessCode;

if ($accessCode === "code123") {
header("Location: http://www.example.com/code_123.html");
} else if ($_GET["code"] === "test") {
header("Location: http://www.example.com/code_test.html");
} else {
header("Location: http://www.example.com/unknown_code.html");
}
?>

我甚至尝试使用重定向选项(cPanel) 每当我使用代码123或测试时,他们会将我重定向到unknown_code.html 我尝试使用if ($accessCode === "code123")($_GET["code"] === "test")

2 个答案:

答案 0 :(得分:0)

您需要将$ _GET ['code']的值分配给$ accessCode。

<?php
$accessCode = $_GET['code'];

switch ($accessCode) {
     case 'code123':
         header("Location: http://www.example.com/code_123.html");
         exit;
     case 'test':
         header("Location: http://www.example.com/code_test.html");
         exit;
     default:
         header("Location: http://www.example.com/unknown_code.html");
         exit;
}
?>

此外,当您将单个变量与多个值进行比较时,请使用开关语句。

最好使用退出关注重定向标题 - 您已完成此操作。

答案 1 :(得分:0)

你倒退了。

当您制作HTTP&#34; GET&#34;请求,PHP将存储&#34;查询字符串参数&#34;在全局$ _GET数组中。

例如,http://www.yoururl.com?code=code123&mobile=1

在此上下文中,查询字符串以&#34;?&#34;开头。并由&#34;&amp;&#34;分隔,为您留下键值对:

  • code = code123
  • mobile = 1

PHP将其转换并将其存储在全局$ _GET数组中:

$_GET['code'] = "code123"
$_GET['mobile'] = 1

如果您要执行HTTP POST请求,而不是查询字符串,HTTP POST请求&#34; Body&#34;会发生同样的事情。包含查询字符串,或在某些情况下,包含JSON或XML等。

PHP会解析它,你会这样:

$_POST['code'] = "code123"
$_POST['mobile'] = 1

因此,对于您的背景,您实际上是向后倾斜。

你想&#34;分配&#34; $ accessCode,其值存储在$ _GET数组的索引&#34;代码&#34;

<?php

$accessCode = $_GET['code'];

if ($accessCode == 'code123') {
   header('Location: http://yaddayadda', 301); /* you can change 301 to whatever redirect code that is most appropriate. this is good for information search engines on the appropriate behavior. do a google search for http redirect codes to understand. 301 is permanent redirect. 302 is temporary. etc.*/
    exit;

} elseif ($accessCode == 'code234') {
    header('Location: http://');
    exit;
}
else {
    echo 'No valid access code.';
    exit;
}
相关问题