带有Switch语句的$ _GET

时间:2012-04-19 23:18:41

标签: php

我有很多获取定义用户将看到的页面的值,例如“profile”我将显示个人资料页面等等。

找出要显示的页面我尝试做类似的事情:

switch ($_GET) {
    case 'profile':
        require_once('function/profile.php');
        break;
    case 'team':
        require_once('function/team.php');
        break;

但它没有显示结果..

我发送GET请求:index.php?profile,例如..

这里有什么问题,我怎样才能设法做类似的工作呢?提前谢谢你!

11 个答案:

答案 0 :(得分:9)

要使您的示例有效,您可以将$_GET替换为key($_GET)

请注意key()会返回数组的第一个键,因此如果您更改了网址的变量顺序,此行将停止运行。

答案 1 :(得分:8)

$ _ GET是查询字符串中找到的键值对中的数组(脚本名称和问号后面的URL的一部分)。

例如,查询字符串test=1&foo=bar将转换为:

Array(
    test => 1
    foo => 'bar'
)

在OP示例index.php?profile中,您将得到一个$ _GET数组,如:

Array(
    profile => null
)

做这样的网址的问题在于它是非标准的。当您以非标准方式执行操作时,您必须提出非标准解决方案来解决问题。

以下是一些选项以及每个选项的问题:

  1. 您可以使用$_SERVER['QUERY_STRING']来获取网址中?之后的所有内容。如果在url中传递的唯一内容只是profile(或其他一些单值),这很好。在这种情况下,$_SERVER['QUERY_STRING']将只包含profile。但是你也失去了在get字符串中传递其他参数的能力。

  2. 您可以使用@stewe描述的方法。 php key函数将从传入的数组中的当前位置返回键。如果没有进行任何循环,则当前位置是第一个元素。这也适用于多个get参数。您的查询字符串看起来就像index.php?profile&test=1&foo=bar。问题是profile(或任何页面)必须是第一个,否则键将返回传递的第一个参数的密钥。

  3. 另一种选择是使用标准方法来使用键和值。无论页面如何,您都使用相同的密钥,只更改值。然后,您的网址看起来像index.php?page=profile,并且您始终可以使用$_GET['page']访问该网页。

  4. 您可以使用mod_rewrite。设置简单,大多数主机支持它(或其他类似的东西),并且有数百万个关于如何使其工作的教程和示例。您最终得到最干净的URL,它适用于查询字符串参数。例如,可以重写/profile/以指向/index.php?page=profile。用户看到/profile/并且php看到标准。这允许您使用$_GET['page']来获取所请求的页面,而不必进行额外的解析以获取php中的其他值。

答案 2 :(得分:5)

$_GET是基于URL的查询字符串填充的一个或多个变量。您需要执行以下操作:

switch ($_GET['myVar']) { ... }

您的网址如下:

http://www.domain.com/index.php?myVar=value

有关详细信息,请参阅PHP Manual for $_GET

答案 3 :(得分:2)

$ _ GET本身对你没什么用。我想你正在寻找钥匙,比如'page',对吗?请记住声明默认值。

所以..

$page = $_GET['page'];

switch ($page) {
  case 'profile':
    require_once('function/profile.php');
    break;
  case 'team':
    require_once('function/team.php');
    break;
  default:
    require_once('function/page-not-found.php');
}

答案 4 :(得分:1)

这是一个数组,因此您需要使用循环来迭代值:

foreach($_GET as $key => $val){
    switch ($key) {
        case 'profile':
            require_once('function/profile.php');
            break;
        case 'team':
            require_once('function/team.php');
            break;
    }
}

答案 5 :(得分:1)

$ _ GET是一个超级全局变量,数据以存储为数组的形式发送。所以你必须这样做 使用索引

访问它

假设您在页面上尝试在数据发送时包含页面:

  

domain.com?page=product

然后你必须使用这样的开关

switch($_GET['page']) {
   ....
}

注意:可能我不必提醒您此代码对注入有多么容易受到攻击。

答案 6 :(得分:0)

使用foreach,您可以获得密钥和值对

foreach ($_GET as $switchkey => $switchval) {
  switch ($switchkey) {
    case 'profile':
        require_once('function/profile.php');
        break;
    case 'team':
        require_once('function/team.php');
        break;
  }
}

答案 7 :(得分:0)

我在正常获取后实际使用以下内容导致lfi / rfi漏洞。以下解决方案是通过bug赏金计划提交给我的,效果很好。注意包含default属性。很重要。以下应该是唯一可接受的答案。归功于飞行的意大利面怪物(他的Noodly Appendage)

                    switch($_GET['page']) {

                        case 'foo':

                            include('pages/foo.php');

                            break;

                        case 'bar':

                            include('pages/bar.php');

                            break;
                        default:

                            include('pages/home.php');

                    }

答案 8 :(得分:0)

如前所述,似乎首先想到的是传递信息的非标准方式。这会在解析值时产生一些困难。虽然,对我来说,主要问题不是检查/清理/清理$ _GET上的数据。可能是太明显了,因为几乎所有答案都是由那些似乎知道他们在做什么的人给出的,我会假设他们只是因为那个而没有提到它

但请记住,如果您不检查它,您很容易受到攻击和脚本故障的影响。损坏的程度取决于您自己的应用程序,因此预测并不容易。

无论如何,这就是我要做的,包括html

<?php
    // initialize variables
    $variable_1 = false; // assume this is the page you want to load
    $variable_2 = false;
    $default = 'index.php'; // the idea is to load something controlled by you. index, error, 404, etc.

    // process $_GET, check, clean and assign values
    if ( isset( $_GET ) !== false ) {
        foreach ( $_GET as $keys => $values ) {
            // check both, $keys and $values for; character set, length, validity against a white list, content
            // using an if to match the $keys garantees that regardless of the order, you will get what you want
            if ( $keys === 'field_1' ) {
                // do what you have to do with this, for instance ...
                $variable_1 = $values;
            }
            if ( $keys === 'field_2' ) {
                // do what you have to do with this, for instance ...
                $variable_2 = $values;
            }
            unset( $_GET[$keys] );
        }
        unset ( $keys, $values );
    }

    // check there are no surprises on $_GET. Load and study anything here
    if ( empty( $_GET ) === false ) {
        // it should be empty, so log what is in here and prepare your code for that
        unset( $_GET );
    } else {
        unset( $_GET );
    }

    // process the variables according to what you want to do
    // if there are just a few options, and they are not going to change often
    // use a switch, otherwise, use a method to check if a file/content exists
    // for the request and load it. If it doesn't exist, inform the user
    // with out giving away internals and suggest a new destination

    // process other variables, here or before this part, wherever makes sense


?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>testing get</title>
    </head>
    <body>
        <form method="get" action="test_get_00.php" accept-charset="utf-8">
            <p><label for="field_1">write something<input type="text" id="field_1" name="field_1" /></label></p>
            <p><label for="field_2">write something<input type="text" id="field_2" name="field_2" /></label></p>
            <p><button type="submit">send</button></p>
        </form>
    </body>
</html>

当然你可以做更多的事情,但是如果你正确地准备你的表格,包括字符集,你就会减少担忧,或者至少还有一些已知的元素。它不是防故障的,但它有帮助。

另外,我上面提到的机制是按照白名单的思维方式工作,这是foreach的想法,在记录之后检查你是否得到了你所期望的并丢弃了其余部分。

答案 9 :(得分:0)

您正试图以错误的方式存档SEO网址。

我发现index.php?profileindex.php?page=profile更好,但在这种情况下这是错误的行为方式。

您应该使用index.php?page=profile,然后应用重写规则来创建SEO网址,如下所示:

RewriteEngine On

RewriteRule ^(.*)$ index.php?page=$1

通过这种方式,您的用户将使用:

http://example.com/profile

,显示的页面为:

http://example.com/index.php?page=profile

答案 10 :(得分:0)

而不是打开键(由key($ _ GET)提出)你可以在$ _GET(url)中定义一个变量,例如'action',它将包含'profile'或'team'或者其他你希望将来。那么你的开关就是:

switch ($_GET['action'])

因此,无论您为此键指定了什么操作,都可以在开关中用作案例