Superglobal variables

2022. 3. 17. 23:50Programing Language/Php

php를 공부하는 와중에 중요한 중요한 내용일것 같아서 작성했습니다...

워낙 기본적인 내용이라 아시는 분들은 그냥 넘어가셔도 좋고 공부를 하는 입장으로 복습겸 기록하는 거니 틀린 것이있더라도 양해 부탁드립니다


  • Superglobal variables?

Php안에 미리 정해놓은 변수들이고 범위에 상관 없이 항상 접근이 가능하며  함수, 클래스, 파일 등으로 부터도 어떤 특별한 수행을 할 필요 없이 접근이 가능합니다...


  • Kinds of Superglobal variables
$GLOBALS 어느 전역변수를 접근할수있는 배열변수
$_SERVER 헤더, 경로, 그리고 스크립트 위치에 대한 정보를 담고 있는변수
$_REQUEST HTML form 제출이후 데이터수집변수[POST, GET, COOKIE방식으로 넘어온 변수]
$_POST POST방식으로 넘어온 변수
$_GET GET방식으로 넘어온 변수
$_FILES 업로드된 파일 변수를 받은 변수
$_ENV 환경 메서드를 통해 현재 스크립트에 전달 된 변수의 연관 배열변수
$_COOKIE 쿠키변수
$_SESSION 세션변수

  • $GLOBALS

Php 스크립트 내의 어디서나 전역변수를 접근할 때 사용하는 PHP super global variable이다. Php는 모든 전역변수들을 $GLOBALS[index] 라는 배열에 저장합니다. 여기에서  index는 변수의 이름을 말합니다. 

 

ex)

<?php
$x = 75;
$y = 25;
function addition()
{
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
} 
addition();
echo $z;
?>

result -> 100


  • $_SERVER

헤더, 경로, 그리고 스크립트 위치에 대한 정보를 담고 있는변수

 

ex)

$_SERVER 안에 올 수 있는 가장 중요한 요소들 목록

$_SERVER['PHP_SELF'] Returns the filename of the currently executing script
$_SERVER['GATEWAY_INTERFACE'] Returns the version of the Common Gateway Interface (CGI) the server is using
$_SERVER['SERVER_ADDR'] Returns the IP address of the host server
$_SERVER['SERVER_NAME'] Returns the name of the host server (such as www.w3schools.com)
$_SERVER['SERVER_SOFTWARE'] Returns the server identification string (such as Apache/2.2.24)
$_SERVER['SERVER_PROTOCOL'] Returns the name and revision of the information protocol (such as HTTP/1.1)
$_SERVER['REQUEST_METHOD'] Returns the request method used to access the page (such as POST)
$_SERVER['REQUEST_TIME'] Returns the timestamp of the start of the request (such as 1377687496)
$_SERVER['QUERY_STRING'] Returns the query string if the page is accessed via a query string
$_SERVER['HTTP_ACCEPT'] Returns the Accept header from the current request
$_SERVER['HTTP_ACCEPT_CHARSET'] Returns the Accept_Charset header from the current request (such as utf-8,ISO-8859-1)
$_SERVER['HTTP_HOST'] Returns the Host header from the current request
$_SERVER['HTTP_REFERER'] Returns the complete URL of the current page (not reliable because not all user-agents support it)
$_SERVER['HTTPS'] Is the script queried through a secure HTTP protocol
$_SERVER['REMOTE_ADDR'] Returns the IP address from where the user is viewing the current page
$_SERVER['REMOTE_HOST'] Returns the Host name from where the user is viewing the current page
$_SERVER['REMOTE_PORT'] Returns the port being used on the user's machine to communicate with the web server
$_SERVER['SCRIPT_FILENAME'] Returns the absolute pathname of the currently executing script
$_SERVER['SERVER_ADMIN'] Returns the value given to the SERVER_ADMIN directive in the web server configuration file (if your script runs on a virtual host, it will be the value defined for that virtual host) (such as someone@w3schools.com)
$_SERVER['SERVER_PORT'] Returns the port on the server machine being used by the web server for communication (such as 80)
$_SERVER['SERVER_SIGNATURE'] Returns the server version and virtual host name which are added to server-generated pages
$_SERVER['PATH_TRANSLATED'] Returns the file system based path to the current script
$_SERVER['SCRIPT_NAME'] Returns the path of the current script
$_SERVER['SCRIPT_URI'] Returns the URI of the current page

  •  $_REQUEST

 HTML 양식에 의해 제출 된 데이터를 수집하는 데 사용됩니다

ex)

<?php
$data = $_REQUEST['data'];
echo $data;
?>


 

  • Difference between GET and POST

- GET 과 POST 모두 배열(e.g. array( key => value, key2 => value2, key3 => value3, ...))을 생성합니다.

 이 배열은 key/value 쌍을 보유하며, keys 는 폼 을 컨트롤하는 이름을, values 는 사용자로부터 입력된 자료를 말합니다.

- GET 과 POST 모두  $_GET 과 $_POST 로 처리되며, 이들은 슈퍼전역변수로서 범위에 상관없이 항상 접근 가능하며, 특별한 작업을 수행할 필요 없이 함수, 클래스, 또는 파일을 접근할 수 있습니다. 

- $_GET 는 URL 파라미터를 통해서 현재 스크립트에 전달된 변수의 배열입니다

- $_POST 는 HTTP POST 방법을 통해서 현재 스크립트에 전달된 변수의 배열입니다 

 

cf)

GET

전송정보가 URL에 표시되기 때문에 보안상추천[x] 

정보의 양 제한[2000자정도]

정보가 URL에 나타탐으로 북마크가능함

POST

전송정보가 URL에 표시가 안되기 때문에 GET보다는 보안성 높음

정보의 양 제한[x]

정보 URL 안나타남 -> 북마크[x] 

 

reference check : www.w3schools.com

'Programing Language > Php' 카테고리의 다른 글

Php wrapper  (0) 2022.05.17
about addslashes & stripslashes  (0) 2022.04.13
about magic_quotes_gpc  (0) 2022.04.12