Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php $\u GET with Switch语句_Php - Fatal编程技术网

Php $\u GET with Switch语句

Php $\u GET with Switch语句,php,Php,我有很多get值来定义用户将要看到的页面,例如“profile”我将显示profile页面等等 为了找出要显示的页面,我尝试这样做: switch ($_GET) { case 'profile': require_once('function/profile.php'); break; case 'team': require_once('function/team.php'); break; 但是没有结果 我发

我有很多get值来定义用户将要看到的页面,例如“profile”我将显示profile页面等等

为了找出要显示的页面,我尝试这样做:

switch ($_GET) {
    case 'profile':
        require_once('function/profile.php');
        break;
    case 'team':
        require_once('function/team.php');
        break;
但是没有结果

我发送GET请求的方式如下:例如,index.php?profile


这里的问题是什么?我怎样才能做类似的工作呢。提前谢谢你

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

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

$\u GET是一个超级全局变量,其中数据以数组形式存储发送。所以你必须 使用索引访问它

假设您发送了一个页面,则在发送数据时,您尝试包括一个页面,如下所示:

domain.com?页面=产品

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

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

注意:也许我不必提醒您该代码在注入时有多脆弱

$\u GET
是基于URL的查询字符串填充的数组或变量。您需要执行以下操作:

switch ($_GET['myVar']) { ... }
您的URL看起来像:

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

有关更多信息,请参阅。

使用foreach可以获取密钥和值对

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

要使示例正常工作,可以将
$\u GET
替换为
键($\u GET)


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

$\u GET是查询字符串中找到的键值对的数组(URL的一部分,位于脚本名称和问号之后)

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

Array(
    test => 1
    foo => 'bar'
)
在OP示例中,
index.php?profile
,您将得到一个$\u GET数组,如下所示:

Array(
    profile => null
)
像这样做URL的问题在于它是非标准的。当你以非标准的方式做事时,你必须拿出非标准的解决方案来解决问题

以下是几个选项以及每个选项都有的问题:

  • 您可以使用
    $\u服务器['QUERY\u STRING']
    ,它将在url中的
    之后获取所有内容。如果url中只传递了
    profile
    (或其他单个值),则这是很好的。在这种情况下,
    $\u服务器['QUERY\u STRING']
    将只包含
    概要文件。但是,您也将失去在get字符串中传递其他参数的能力

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

  • 另一种选择是使用键和值的标准方法。无论页面如何,都使用相同的键,只是值发生了变化。然后,您就有了类似于
    index.php?page=profile
    的URL,您可以始终使用
    $\u GET['page']
    访问该页面

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


  • $\u单独获取对您没有多大用处。我想你在找一把钥匙,比如“page”,对吧?记住也要声明一个默认值

    所以


    我实际上在正常的get之后使用了以下命令,导致lfi/rfi漏洞。下面的解决方案是通过bug赏金计划提交给我的,效果非常好。请注意包含了
    default
    属性。非常重要。以下是唯一可以接受的答案。归功于会飞的意大利面怪兽(他那弯曲的附属物)


    如前所述,首先想到的似乎是传递信息的非标准方式。这将在解析值时产生一些困难。不过,对我来说,主要问题不是检查/清理/清理$\u 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>
    

    您试图以错误的方式归档SEO URL

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

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

    RewriteEngine On
    
    RewriteRule ^(.*)$ index.php?page=$1
    
    这样,您的用户将使用:

    http://example.com/profile
    
    显示的页面将是:

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

    您可以在$\u-GET(url)中定义一个变量,例如“action”,它将包含“profile”或“team”或您将来希望的任何内容,而不是打开键(由key($\u-GET)建议)。那么,您的切换将是:

    switch ($_GET['action'])
    

    因此,无论您分配给此键的操作是什么,您都可以在您的开关中使用作为一个案例

    这应该是foreach($\u GET as$key=>$val),然后是开关($key),谢谢,帮了我很多忙..该死的,我太慢了30秒。
    http://example.com/index.php?page=profile
    
    switch ($_GET['action'])