简体   繁体   English

为什么PHP总是重定向到同一页面?

[英]Why is PHP always redirecting to the same page?

I have written this small php page which should redirect users by the value put in the url. 我已经编写了这个小的php页面,该页面应通过url中的值重定向用户。 But the script does not seem to work. 但是该脚本似乎不起作用。 Anything i have missed? 我错过了什么吗?

<?php
if ($choice=="stb")
  $redirect="http://www.webpage1.com/";

    elseif ($choice=="player")
        $redirect="http://www.alternativewebpage.com/"; 

    else
        $redirect="http://www.world-tvpro.com/";

header( 'Location: '.$redirect ) ;
?>

If the page is saved at my server as script.php I would execute it like this: http://mypage.com/script.php?choice=stb .. Expected then is to be redirected to www.webpage1.com, but i get redirected to http://www.world-tvpro.com/ no matter what i write. 如果该页面以script.php的形式保存在我的服务器上,我将按以下方式执行该页面: http ://mypage.com/script.php?choice=stb ..预计此后会将其重定向到www.webpage1.com,但是无论我写什么,我都会重定向到http://www.world-tvpro.com/

You are depending on a feature called register_globals , which is dangerous. 您依赖于一个称为register_globals的功能,这很危险。 Rather than letting query string parameters set globals in your script, use the $_GET superglobal: 与其让查询字符串参数在脚本中设置全局变量,不如使用$_GET超全局变量:

if ( isset($_GET['choice']) && $_GET['choice'] === 'stb' ) {

For example. 例如。

您需要使用$_GET['choice'] (或isset($_GET['choice']) ? $_GET['choice'] : null )。

This seems to work for me, sometimes not using { } around your statements can cause issues. 这似乎对我有用,有时在语句周围不使用{}可能会导致问题。

<?php
if ($_GET['choice'] == "stb")
{
    $redirect="http://www.webpage1.com/";
} elseif ($_GET['choice'] == "player") {
    $redirect="http://www.alternativewebpage.com/"; 
} else {
    $redirect="http://www.world-tvpro.com/";
}
header( 'Location: '.$redirect );
?>

Also, $_GET['choice'] is how you get variables from the url query string. 另外,$ _GET ['choice']是从url查询字符串中获取变量的方式。

PHP uses a series of super global arrays to pass information from the client to the server. PHP使用一系列超级全局数组将信息从客户端传递到服务器。 They are as follows: 它们如下:

 $_GET, $_POST, $_COOKIE and $_REQUEST

In your case, since your passing the value in the url as a query parameter you should retrieve choice using $_GET: 在您的情况下,由于您将网址中的值作为查询参数进行传递,因此应使用$ _GET检索选择:

$choice = $_GET['choice'];

Note: It can also be retrieved using $_REQUEST as request is a super global array containing all the values of $_GET, $_POST, and $_COOKIE. 注意:也可以使用$ _REQUEST来检索它,因为请求是包含$ _GET,$ _ POST和$ _COOKIE的所有值的超全局数组。

You would simply do the same as above just with request: 您只需按照请求执行上述操作即可:

$choice = $_REQUEST['choice'];

Hope this helps. 希望这可以帮助。

If you add: $choice = isset($_GET['choice']) ? $_GET['choice'] : false; 如果添加: $choice = isset($_GET['choice']) ? $_GET['choice'] : false; $choice = isset($_GET['choice']) ? $_GET['choice'] : false; above the entire IF statement, then the code should be fine. 在整个IF语句之上,则代码应该没问题。

Also, while the IF statement is fine, a case statement could work nicely as well: 另外,尽管IF语句很好,但case语句也可以很好地工作:

$choice = isset($_GET['choice']) ? $_GET['choice'] : false;
switch($choice){
    case "stb":
        $redirect = 'http://www.webpage1.com/'; break;
    case "player":
        $redirect = 'http://www.alternativewebpage.com/'; break;
    default:
        $redirect = 'http://www.world-tvpro.com/';
}
header("Location: ".$redirect);
exit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM