English 中文(简体)
一般事务人员
原标题:$_GET with Switch Statement
  • 时间:2012-04-19 23:18:41
  •  标签:
  • php

i 有一个固定的数值,用来界定用户角,例如,“插图”显示简介页等等。

• 寻找显示我试图做以下事情的网页:

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

但它没有结果。

i 发送GET申请,如索引.php?profile 。

这里的问题是什么,以及我如何能够做同样的工作。 事先感谢你!

最佳回答

To make your example work, you could replace $_GET with key($_GET)

请注意,key(>将回到阵列的关键first,这样,如果你改变URL的变序,该线即停止运行。

问题回答

• GET是圆顶座中发现的关键价值层的阵列(封面名称和问号之后的圆顶部分)。

例如,插座标注test=1&foo=bar将转化为:

Array(
    test => 1
    foo =>  bar 
)

在欧佩特例,index.php?profile上,您最后将使用美元-GET阵列,如:

Array(
    profile => null
)

象这样做的难题是它不符合标准。 当你以非标准的方式做事时,你必须拿出非标准的解决办法来解决这些问题。

下面是几个选择,还有每个选择的问题:

  1. 页: 1 SERVER[questRY_STRING] which will have all after the ? in the url. 如果在卢尔通过的唯一东西仅仅是,profile<>/code>(或其他一些单一价值)的话,则罚款。 在这种情况下,$_SERVER[ questRY_STRING ]将无一例外,但profile。 但是,你也丧失了通过额外参数的能力。

  2. 你可以采用@stewe描述的方法。 The php 职能将恢复目前阵列中的位置的关键。 如果你没有做任何选择,目前的立场是第一个因素。 还将用多种参数进行罚款。 您的问询将仅看index.php?profile&test=1&foo=bar。 问题是,<条码>引人瞩目的/代码(或无论哪一页)必须是第一种或其它关键因素,而无论关键点是第一种参数。

  3. 另一种选择是只采用关键和价值的标准方法。 不管页数,你都使用同样的钥匙,只是价值变化。 那么,你有像<代码>index.php?page=profile这样的斜体,你可以总是通过<代码>_GET[ page]/code>进入网页。

  4. 您可使用。 简言之,大多数东道方都支持这一机制(或其他一些类似的机构),还有数百万名教员和实例说明如何开展工作。 你最后用最清洁的陶器,并用电梯参数开展工作。 例如,<代码>/profile/ 可改写到/index.php?page=profile。 用户见/profile/和php。 这使您能够使用<代码>_GET[ page ],以获得所要求的网页,而不必在实验室内做其他数值的外加。

页: 1 我恳请大家看一看钥匙,如一页,正确吗? 还有责任宣布违约价值。

因此。

$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 );
}

$_GET is a super global variable, where the data are sent as stored as array. So you have to access it using Index

假设你一页,在发送数据时,你试图列入一页:

domain.com?page=product

然后,你们必须像现在这样使用开关。

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

<><>>>说明: 请允许我提醒大家,这一守则很容易遭到注射。

它是一个阵列,因此,你们需要利用一种 lo机,在价值观上 it:

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

各位可以坐在钥匙上和价值上。

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

i 在正常导致液化/液化之后,实际使用以下物品。 下面的解决办法通过“欢迎”方案提交给我,并做了大量工作。 注意列入<代码>default属性。 非常重要。 以下是唯一可接受的答案。 给飞行间谍agh(His Noodly Appendage)的信贷

                    switch($_GET[ page ]) {

                        case  foo :

                            include( pages/foo.php );

                            break;

                        case  bar :

                            include( pages/bar.php );

                            break;
                        default:

                            include( pages/home.php );

                    }

如前所述,似乎想到的第一件事是传递信息的非标准方式。 如果你贬低价值,就会带来一些困难。 虽然对我来说,主要问题不是核对/核对/清理/清理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>

Of course you can do a few more things, but if you prepare your form properly, including the character set, you have less worries, or at least a few more known elements. It s not failproof, but it helps.

此外,我前面提到的机械师们在一份白色名单的思想集上工作,即画家的思想,以检查你在伐木后会得到的东西和抛弃其他东西。

你们正试图以错误的方式将SEO urls归档。

index.php?profile is better subsequently index.php?page=profile<>/code>,但该文本忽略了本案的行为方式。

你们应当使用<代码>index.php?page=profile,然后适用沉积规则,以创建小规模企业。

RewriteEngine On

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

用户将以此方式使用:

http://example.com/profile

页: 1

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

您可以确定一个变量,而不是根据钥匙(GET)的提议,用“GET(url)”来界定,其名称为“GET(url)”类行动,其中将包含公开的简介,或团队或未来希望的团队。 那么,您的开关就是:

switch ($_GET[ action ])

无论你给这一关键人物采取什么行动,你都可以在您的开关中使用。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签