【问题标题】:PHP redirection page based on GET variables基于 GET 变量的 PHP 重定向页面
【发布时间】:2015-04-18 07:54:06
【问题描述】:

我是 PHP 新手,所以请耐心回答这个初级问题。

我想创建一个脚本,根据 GET 变量将用户重定向到各种地址。例如,redirection.php?id=youtube 应该将他们重定向到 www.youtube.com,redirection.php?id=twitter 应该将他们重定向到 www.twitter.com , 等等。

这是我的代码:

<!DOCTYPE html>
<html>
<head>
<title>Please Wait...</title>
</head>
<body>

<?php
// directs the user to various locations on the internet
print_r($_GET);

if($_GET['id'] === 'youtube') {
    header('Location: http://www.youtube.com/') ;
    die()
}
if($_GET['id'] === 'twitter') {
    header('Location: http://www.twitter.com/') ;
    die()
}
if($_GET['id'] === 'reddit') {
    header('Location: http://www.reddit.com/') ;
    die()
}
?>

</body>
</html>

到目前为止,PHP 文件根本没有响应,我应该更改什么来解决这个问题?

再次,对于初级级别的问题,我很抱歉,但这实际上是我的第一个 PHP 脚本,我对一些术语不是很熟悉,这使得 Google 难以搜索正确的代码。

【问题讨论】:

  • 死();你忘记了 die() 中的分号
  • 成功解决了,谢谢!
  • @Richie 接受 saty 的回答,如果这对你有用。

标签: php variables redirect get


【解决方案1】:

在 PHP 中比较值是否相等时,您可以使用 == 运算符或 === 运算符。 2有什么区别?好吧,这很简单。 == 运算符只是检查左右值是否相等。但是,=== 运算符(注意额外的“=”)实际上检查左右值是否相等,并检查它们是否属于相同的变量类型(比如它们是否都是布尔值、整数等)。

死();你忘记了 die() 中的分号

你的代码应该是

if($_GET['id'] == 'youtube') {
    header('Location: http://www.youtube.com/') ;
    die();
}
if($_GET['id'] == 'twitter') {
    header('Location: http://www.twitter.com/') ;
    die();
}
if($_GET['id'] == 'reddit') {
    header('Location: http://www.reddit.com/') ;
    die();
}

【讨论】:

    【解决方案2】:

    你可以试试下面的代码:

        <!DOCTYPE html>
        <html>
        <head>
        <title>Please Wait...</title>
        </head>
        <body>
    
        <?php
        // directs the user to various locations on the internet
        extract($_REQUEST);
    
        if(isset($id) && $id == 'youtube') {
            header('Location: http://www.youtube.com/') ;
            die();
        }
        if(isset($id) && $id === 'twitter') {
            header('Location: http://www.twitter.com/') ;
            die();
        }
        if(isset($id) && $id === 'reddit') {
            header('Location: http://www.reddit.com/') ;
            die();
        }
        ?>
    
        </body>
        </html>
    

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 2010-10-26
      • 2023-02-06
      • 1970-01-01
      • 2016-02-12
      • 2017-12-26
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      相关资源
      最近更新 更多