PHP Velho Oeste 2024

미리 선언된 변수

PHP는 실행되는 스크립트에 적용이되는 상당량의 미리 선언된 변수를 제공한다. 하지만, 이 변수의 대부분은 운영되는 서버, 서버의 버전, 서버의 설정, 다른 팩터와 관련되어 있어서 완벽하게 문서화되지 않았다. 이 중 몇개의 변수는 커맨드 라인에서 실행되는 PHP에서는 유효하지 않다. 예약된 미리 선언된 변수의 섹션을 참고.

Warning

PHP 4.2.0 이후 버전부터, PHP 디렉티브 register_globals의 기본값은 off가 되었다. register_globals를 off로 놓으면, 전역 유효영역안의 미리 선언된 변수 집합들에 영향을 미친다. 예를 들면, DOCUMENT_ROOT값을 얻기 위해서는 $DOCUMENT_ROOT 대신에 $_SERVER['DOCUMENT_ROOT']를, http://www.example.com/test.php?id=3에서 $id 대신에 $_GET['id']를, $HOME 대신에 $_ENV['HOME']을 사용해야 한다.

이와 관련된 변경사항은 register_globals의 설정 엔트리를 읽거나, 전역 등록 사용하기에 관한 보안 챕터는 물론, » 4.1.0» 4.2.0 Release Announcements도 참고하기 바란다.

superglobal arrays와 같은 가용한 PHP의 예약된 미리 선언된 변수를 사용하는 것을 추천한다.

4.1.0 버전 이후에, PHP는 웹서버, 환경, 유저 입력과 관련된 미리 선언된 배열 변수 집합을 추가적으로 제공한다. 이 새로운 배열은 자동적으로 전역화되기 때문에 더 특별해진다. 즉, 자동적으로 모든 유효영역안에서 적용이 가능하다. 이런 이유로, 이런 변수를 '자동전역변수'라고 한다. (PHP에서는 사용자-선언 슈퍼전역변수가 허용되지 않는다.) 슈퍼전역변수는 아래에 열거한다. 하지만, 이 변수 목록과 논의는 예약된 미리선언된 변수섹션을 참고한다. 또한 구버전의 미리선언된 변수($HTTP_*_VARS)가 아직도 존재한다는 것에 주의한다. PHP 5.0.0부터, 긴 형태의 PHP 예약 변수 배열을 register_long_arrays 지시어로 제거할 수 있습니다.

Note: 가변 변수

슈퍼전역변수는 함수나 클래스 메쏘드 안에서 가변 변수로는 쓰일수 없다.

Note:

자동 전역과 HTTP_*_VARS가 동시에 존재할 수 있습니다; 하지만 동일하지 않으며, 하나를 변경해도 다른 하나는 바뀌지 않습니다.

variables_order내의 특정 변수들이 설정되어있지 않으면, 적합한 PHP의 미리선언된 배열도 비워있게 된다.

add a note add a note

User Contributed Notes 4 notes

up
111
johnphayes at gmail dot com
18 years ago
I haven't found it anywhere else in the manual, so I'll make a note of it here - PHP will automatically replace any dots ('.') in an incoming variable name with underscores ('_'). So if you have dots in your incoming variables, e.g.:

example.com/page.php?chuck.norris=nevercries

you can not reference them by the name used in the URI:
//INCORRECT
echo $_GET['chuck.norris'];

instead you must use:
//CORRECT
echo $_GET['chuck_norris'];
up
12
DD32=theonly_DD32[&]yahoo.com.au
18 years ago
I have this function in my main files, it allows for easier SEO for some pages without having to rely on .htaccess and mod_rewrite for some things.
<?php
   
function long_to_GET(){
       
/**
        * This function converts info.php/a/1/b/2/c?d=4 TO
        * Array ( [d] => 4 [a] => 1 [b] => 2 [c] => )
        **/
       
if(isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != ''){
           
//Split it out.
           
$tmp = explode('/',$_SERVER['PATH_INFO']);
           
//Remove first empty item
           
unset($tmp[0]);
           
//Loop through and apend it into the $_GET superglobal.
           
for($i=1;$i<=count($tmp);$i+=2){ $_GET[$tmp[$i]] = $tmp[$i+1];}
        }
    }
?>

Its probably not the most efficient, but it does the job rather nicely.

DD32
up
10
lopez dot on dot the dot lists at yellowspace dot net
21 years ago
- Security Issue and workaround -
If You use "eval()" to execute code stored in a database or elsewhere, you might find this tip useful.

Issue:
By default, all superglobals are known in every function.
Thus, if you eval database- or dynamically generated code (let's call it "potentially unsafe code"), it can use _all_ the values stored in _any_ superglobal.

Workaround:
Whenever you want to hide superglobals from use in evaluated code, wrap that eval() in an own function within which you unset() all the superglobals. The superglobals are not deleted by php in all scopes - just within that function. eg:

function safeEval($evalcode) {
    unset($GLOBALS);
    unset($_ENV);
    // unset any other superglobal...
    return eval($evalcode);
}

(This example assumes that the eval returns something with 'return')

In addition, by defining such a function outside classes, in the global scope, you'll make sure as well that the evaluated ('unsafe') code doesn't have access to the object variables ($this-> ...).
up
0
LouisGreen at pljg dot freeserve dot co dot uk
21 years ago
It seems that when you wish to export a varible, you can do it as return $varible, return an array(), or globalise it. If you return something, information for that varible can only travel one way when the script is running, and that is out of the function.

function fn() {
   $varible = "something";

  return $variable;
}

echo fn();
OR
$newvariable = fn();

Although if global was used, it creates a pointer to a varible, whether it existed or not, and makes whatever is created in the function linked to that global pointer. So if the pointer was global $varible, and then you set a value to $varible, it would then be accessible in the global scope. But then what if you later on in the script redefine that global to equal something else. This means that whatever is put into the global array, the information that is set in the pointer, can be set at any point (overiden). Here is an example that might make this a little clearer:

function fn1() {

   global $varible; // Pointer to the global array
   $varible = "something";
}

fn1();
echo $varible; // Prints something
$varible = "12345";
echo $varible; // Prints 12345

function fn2() {

   global $varible; // Pointer to the global array
   echo $varible;
}

fn2(); // echos $varible which contains "12345"

Basically when accessing the global array, you can set it refer to something already defined or set it to something, (a pointer) such as varible you plan to create in the function, and later possibly over ride the pointer with something else.
To Top