$_GET的用法详细

操作方法

  • 01

    $_GET 说明 通过 URL 参数传递给当前脚本的变量的数组 Example #1$_GET范例 <?php          echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';     ?> 假设用户访问的是 http://example.com/?name=Hannes 以上例程的输出类似于: Hello Hannes! 注释 Note: "Superglobal"也称为自动化的全局变量。这就表示其在脚本的所有作用域中都是可用的。不需要在函数或方法中用global $variable;来访问它。 Note: GET 是通过urldecode()传递的。 例子2:要求:I need write       -. $_GET[actor]        instead of       -. $_GET["actor"]     or        -. $_GET['actor'] 以下为代码:E.G. URL "http://127.0.0.1/test2.php?actor=14" <?php// CONNECT TO THE DATABASE$DB_HOST='';$DB_USER='';$DB_PASS='';$DB_NAME='';$mysqli= newmysqli($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME);    if (mysqli_connect_errno()) {printf("Connect failed: %s\n",mysqli_connect_error());        exit();    }// A QUICK QUERY ON A FAKE USER TABLE$query="SELECT DISTINCT Title FROM movie WHERE ID_movie IN ( SELECT DISTINCT ID_Movie FROM actor_scene WHERE ID_actor=$_GET[actor]) ";$result=$mysqli->query($query) or die($mysqli->error.__LINE__);// GOING THROUGH THE DATAif($result->num_rows>0) {        while($row=$result->fetch_assoc()) {            echostripslashes($row['Title'])."<br>";            echo" ";        }    }    else {        echo'NO RESULTS';    }// CLOSE CONNECTIONmysqli_close($mysqli);?> Maarten Schroeven25-Jul-2011 10:09 You can use this function to remove any $_GET variables out of your URL, it takes an array off strings(the names keys of the $_GET you wish to remove) and returns the url with the ones specified removed <?phpfunctiongetUrlWithout($getNames){$url="http". ((!empty($_SERVER['HTTPS'])) ?"s":"") ."://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];$questionMarkExp=explode("?",$url);$urlArray=explode("&",$questionMarkExp[1]);$retUrl=$questionMarkExp[0];$retGet="";$found=array();      foreach($getNamesas$id=>$name){            foreach ($urlArrayas$key=>$value){                if(isset($_GET[$name]) &&$value==$name."=".$_GET[$name])                    unset($urlArray[$key]);          }      }$urlArray=array_values($urlArray);      foreach ($urlArrayas$key=>$value){          if($key<sizeof($urlArray) &&$retGet!=="")$retGet.="&";$retGet.=$value;      }      return$retUrl."?".$retGet;  }?>Examplecurrent url ishttp://www.example.net/index.php?getVar1=Something&getVar2=10&getVar3=ok<?phpechogetUrlWithout(array("getVar1","getVar3"));//result will be "http://www.example.net/index.php?getVar2=10"?> chris at bjelleklang dot org18-Dec-2010 11:40 Please note that PHP setups with the suhosin patch installed will have a default limit of 512 characters for get parameters. Although bad practice, most browsers (including IE) supports URLs up to around 2000 characters, while Apache has a default of 8000.To add support for long parameters with suhosin, addsuhosin.get.max_value_length = <limit> in php.ini Alberto Lepe dev at alepe dot com05-Oct-2009 06:23 This Function will help you to manage your GET parameters to facilitate coding and prevent duplication. This is a basic version but it can be easily extended.<?php// Author: Alberto Lepe (www.alepe.com)    /* Process $_GET to preserve user custom parameters     * the arguments is a list of URL parameters that should be removed/changed from URL     * for example:     *     * URL = "index.php?s=1&fi=2&m=4&p=3     *     * if called: fixGet("s"); the result has to be: ?fi=2&m=4&p=3     * if called: fixGet("s&m"); the result has to be: ?fi=2&p=3     * if called: fixGet("s=4"); the result has to be: ?s=4&fi=2&m=4&p=3     * if called: fixGet("s=2&m"); the result has to be: ?s=2&fi=2&p=3     * if called: fixGet("s=&m=3"); the result has to be: ?s=&fi=2&m=3&p=3     * if called: fixGet("s=2&m="); the result has to be: ?s=2&fi=2&m=&p=3     * Special: when it ends with a =":" its to leave it open at the end     * (just first occurrence) to facilitate concatenation:     * if called: fixGet("s=2&m:"); the result has to be: ?s=2&fi=2&p=3&m     * if called: fixGet("s=2&m:="); the result has to be: ?s=2&fi=2&p=3&m=     *     * Usage with HTML (using the URL example above and $id = 99):     *     * <a href="index.php<?php echo fixGet('m=2&s&fi:=').$id ?>" >Link</a>     * Explanation: change "m" to 2, delete "s" and "fi" gets the $id value. ("p" is kept as it is not specified)     * will output: <a href='index.php?m=2&p=3&fi=99'>Link</a>     */public functionfixGet($args) {        if(count($_GET) >0) {            if(!empty($args)) {$lastkey="";$pairs=explode("&",$args);                foreach($pairsas$pair) {                    if(strpos($pair,":") !==false) {                        list($key,$value) =explode(":",$pair);                        unset($_GET[$key]);$lastkey="&$key$value";                    } elseif(strpos($pair,"=") ===false)                        unset($_GET[$pair]);                    else {                        list($key,$value) =explode("=",$pair);$_GET[$key] =$value;                    }                }            }            return"?".((count($_GET) >0)?http_build_query($_GET).$lastkey:"");        }?>To test, copy+paste the following code into testFixGet.php<?php/* * Unit Test for fixGet() */$cases= array (0=> array("s"=>1,"fi"=>2,"m"=>4,"p"=>3),1=> array("s"=>"","fi"=>"","m"=>4,"p"=>3),);$test[0] = array("s"=>"fi=2&m=4&p=3","s&m"=>"fi=2&p=3","s=4"=>"s=4&fi=2&m=4&p=3","s=2&m"=>"s=2&fi=2&p=3","s=&m=3"=>"s=&fi=2&m=3&p=3","s=2&m="=>"s=2&fi=2&m=&p=3","s=2&m:="=>"s=2&fi=2&p=3&m=","z=9"=>"s=1&fi=2&m=4&p=3&z=9","z:"=>"s=1&fi=2&m=4&p=3&z","s:&m=3"=>"fi=2&m=3&p=3&s","s&m=3"=>"fi=2&m=3&p=3",);$test[1] = array("s"=>"fi=&m=4&p=3","s&m"=>"fi=&p=3","s=4"=>"s=4&fi=&m=4&p=3","s=2&m"=>"s=2&fi=&p=3","s=&m=3"=>"s=&fi=&m=3&p=3","s=2&m="=>"s=2&fi=&m=&p=3","s=2&m:="=>"s=2&fi=&p=3&m=","z=9"=>"s=&fi=&m=4&p=3&z=9","z:"=>"s=&fi=&m=4&p=3&z",);foreach($casesas$x=>$value) {    echo"<hr> CASE:$x<hr>\n";    foreach($test[$x] as$arg=>$expected) {$_GET=$cases[$x];$res=myForm::fixGet($arg);        echo (($res==="?".$expected)?"OK":"NG ($res)")." [$arg]<br>\n";    }}?> slavik032919-Mar-2009 04:01 the addget function below actually has more use when you dont use the recursive merge as such:<?phpfunctionAddGet($ArrayOrString){if(is_array($ArrayOrString))returnhttp_build_query(array_merge($GLOBALS['_GET'],$ArrayOrString));parse_str($ArrayOrString,$output);returnhttp_build_query(array_merge($GLOBALS['_GET'],$output));}?>In this case, if the key is added again with a different value it will be replaced with the new value.addget("change=true"); // ?change=trueaddget("change=false"); // ?change=false timberspine _AT_ gmail _DOT_ com15-May-2008 01:38 Note that named anchors are not part of the query string and are never submitted by the browser to the server.Eg.http://www.xyz-abc.kz/index.php?title=apocalypse.php#doomecho $_GET['title'];// returns "apocalypse.php" and NOT "apocalypse.php#doom"you would be better off treating the named anchor as another query string variable like so:http://www.xyz-abc.kz/index.php?title=apocalypse.php&na=doom...and then retrieve it using something like this:$url = $_GET['title']."#".$_GET['na'];Hope this helps someone...

(0)

相关推荐