【问题标题】:PHP Redirect using custom variables from HTML form使用 HTML 表单中的自定义变量进行 PHP 重定向
【发布时间】:2015-07-06 16:38:59
【问题描述】:

不是 100% 确定是否已经有答案,但我正在努力寻找任何东西。我想从 html 中获取一个字符串并使用它将用户重定向到子域。例如,如果用户在表单中输入“abc123”,他们将被重定向到 abc123.awebsite.com。看起来它应该很简单,但我无法完全解决。到目前为止我的代码如下:

HTML:

<form action="redirect.php" method="get">
<span>
Reference:
<input type="text" name="reference">
</span>
<input type="submit" value="Submit">
<form>

PHP:

$redirectlocation = ["reference"] + ".awebsite.com"

function redirect($url, $statusCode = 303)
{
   header('Location: redirectlocation ' . $url, true, $statusCode);
   die();
}

为糟糕的解释道歉

谢谢

【问题讨论】:

    标签: php html forms redirect get


    【解决方案1】:

    试试这个,看here阅读更多。

    正确的303 redirect 发送一个HTTP/1.1 303 See Other 标头,然后是url Location。

    function redirect($url, $statusCode = 303)
    {
       header("HTTP/1.1 " . $statusCode . " See Other");
       header("Location: " . $url);
       die();
    }
    

    要从表单中获取值,您在表单中使用了GET 方法,因此您可以使用从表单传递给脚本的$_GET[key] 数组,该数组的键是name="" 属性您的 html 表单输入。

     $redirectlocation = "http://" . $_GET["reference"] . ".awebsite.com";
    redirect($redirectlocation);
    

    【讨论】:

    • 出于某种原因,$redirectlocation = $_GET["reference"] + ".awebsite.com"; Redirect($redirectlocation); 无论输入什么都返回 0。然而,这确实有效,但不允许通过表单输入更改重定向:Redirect('http://example.com/');
    • 是否需要将http://添加到$redirectlocation = $_GET["reference"] + ".awebsite.com"; like $redirectlocation = "http://" + $_GET["reference"] + ".awebsite.com";
    • 感谢您的快速回复,我刚刚尝试过,但似乎仍然有问题。我试过使用&lt;?php echo $redirectlocation; ?&gt; 来检查它不是其他东西弄乱了它,但它也返回了一个 0。
    • $redirectlocation = $_GET["reference"]; 确实重定向,所以创建字符串似乎有问题?
    • 这行得通,虽然有些是多余的:$reference = $_GET["reference"]; $http = "http://"; $domain = ".awebsite.com"; $redirectlocation = $http . $reference . $domain; Redirect($redirectlocation);
    【解决方案2】:

    这个解决方案奏效了:

    HTML

    <form action="redirect2.php" method="get">
    <span>
    Reference:
    <input type="text" name="reference">
    </span>
    <input type="submit" value="Submit">
    </form>
    

    PHP

    function redirect($url, $statusCode = 303)
    {
       header("HTTP/1.1 " . $statusCode . " See Other");
       header('Location: ' . $url, true, $statusCode);
       die();
    }
    $reference = $_GET["reference"];
    $http = "http://";
    $domain = ".awebsite.com";
    
    $redirectlocation = $http . $reference . $domain;
    Redirect($redirectlocation);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 2020-02-14
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多