Raspberry Pi_Kor_24.4.10 PHP 전송방식 및 주요 Global 변수


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


24.4.10  PHP 전송방식 및 주요 Global 변수

 

24.4.10.1  HTTP POST/GET 방식과 PHP에서의 처리

 

web browser web server를 호출하여 필요한 자료를 전송할 때는 GET 방식과 POST 방식을 사용할 수 있다.  

 

   GET 방식

 

GET 방식은 전송자료를 URL 형식으로 구성하여 다음과 같은 형식으로 요청을 보낸다.

 

http://URL/?key1=value1&key2=value2

 

URL과 전송자료 사이에 "?" 기호를 사용하여 구분한다. 전송자료는 "key=value" 형식으로  지정하고 여러 개의 자료가 있는 경우는 "&" 기호로 구분한다.

 

이 방식에서는 전송하는 전체 내용이 URL 형식으로 표시가 되며, web browser에 그대로 표시된다. 또한 GET 방식은 전송하는 자료의 양에 제한이 있지만, POST 방식에 비해 처리속도가 상대적으로 빠르다.

 

이 방식은 URL에 표시된 내용을 그대로 "즐겨찾기" 등에 함께 보관해서 나중에 다시 사용할 수 있는 편리성이 있지만, 비밀유지가 필요한 자료를 보낼 때는 사용하기 곤란하다. 따라서 GET 방식은 자료 양이 많지 않고 민감하지 않은 자료를 전송할 때 사용하는 것이 좋다.

 

이렇게 전송된 자료는 web server에서 "$_GET" array 변수와 "$_REQUEST" array 변수에 저장된다. 여기에 저장된 값은 다음과 같이 사용한다.

 

$variable = $_GET["key"]

 

$variable = $_REQUEST["key"]

 


 

[사용 Example]

다음은 HTML 문서의 "form" tag에서 GET 방식이 지정된 것을 보여준다.  

 

<form action="employee.php" method="get">

    Name:  <input type="text" name="username" /><br />

    Email: <input type="text" name="email" /><br />

    <input type="submit" name="submit" value="Submit me!" />

</form>

 

다음은 위에서 호출한 "employee.php" 파일에서 "$_GET" 변수와 "$_REQUEST" 변수를 이용해서 전송된 자료를 참조하고 있는 것을 보여준다.

 

<?php

echo $_GET['username'];

echo $_REQUEST['username'];

?>


 

   POST 방식

 

POST 방식은 전송하는 자료를 URL의 일부로 보내지 않는다. POST 방식을 사용하는 경우는 web client web server를 호출한 다음 내부적으로 POST 함수를 호출하여 함수의 argument로 전송자료를 직접 전송하게 된다.

 

POST 방식은 전송하는 자료의 양에 제한이 없어서 많은 자료 처리가 필요한 곳에서도 자유롭게 사용할 수 있다. 이 방식을 사용하면 전송하는 자료는 web browser URL형식으로 표시가 되지 않으므로 비밀유지가 필요한 자료를 보낼 때도 문제 없이 사용할 수 있다.  

 

이렇게 전송된 자료는 web server에서 "$_POST array" 변수와 "$_REQUEST" array 변수에 저장된다. 여기에 저장된 값은 다음과 같이 사용한다.

 

$variable = $_POST["key"]

 

$variable = $_REQUEST["key"]

 

다음은 HTML 문서의 "form" tag에서 POST 방식을 지정하고 있는 것을 보여준다.

 

<form action="employee.php" method="post">

    Name:  <input type="text" name="username" /><br />

    Email: <input type="text" name="email" /><br />

    <input type="submit" name="submit" value="Submit me!" />

</form>

 

다음은 위에서 호출한 "employee.php" 파일에서 "$_POST" 변수와 "$_REQUEST" 변수를 이용해서 전송된 자료를 참조하고 있는 것을 보여준다.

 

<?php

echo $_POST['username'];

echo $_REQUEST['username'];

?>

 

 


 

24.4.10.2  PHP Global 변수

 

   $_SERVER

 

"$_SERVER" 변수는 web server가 생성한 array 자료로서 header, path, script location 등과 같은 정보를 가지고 있다. 모든 web server가 모든 항목에 대한 정보를 동일한 형식으로 제공해 준다는 보장은 없고, web server에 따라 제공되는 항목이 틀려질 수 있다.

 

여기서 제공되는 주요한 정보는 다음과 같다.

 

Element/Code

Description

PHP_SELF

Returns the filename of the currently executing script

GATEWAY_INTERFACE

Returns the version of the Common Gateway Interface (CGI) the server is using

SERVER_ADDR

Returns the IP address of the host server

SERVER_NAME

Returns the name of the host server (such as www.w3schools.com)

SERVER_PROTOCOL

Returns the name and revision of the information protocol (such as HTTP/1.1)

REQUEST_METHOD

Returns the request method used to access the page (such as POST)

REQUEST_TIME

Returns the timestamp of the start of the request (such as 1377687496)

QUERY_STRING

Returns the query string if the page is accessed via a query string

HTTP_HOST

Returns the Host header from the current request

HTTPS

Is the script queried through a secure HTTP protocol

REMOTE_ADDR

Returns the IP address from where the user is viewing the current page

REMOTE_HOST

Returns the Host name from where the user is viewing the current page

REMOTE_PORT

Returns the port being used on the user's machine to communicate with the web server

SCRIPT_FILENAME

Returns the absolute pathname of the currently executing script

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_PORT

Returns the port on the server machine being used by the web server for communication (such as 80)

SCRIPT_NAME

Returns the path of the current script

SCRIPT_URI

Returns the URI of the current page

DOCUMENT_ROOT

The document root directory under which the current script is executing, as defined in the server's configuration file.

 

아래는 script 내에서 "$_SERVER" 변수를 사용한 사례이다

 

<html>
< body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_POST['fname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}
?>

< /body>
< /html>

 


 

"$_SERVER['PHP_SELF']"를 이용하여 자기 자신의 파일 이름을 결정하여 "action" 속성에 지정하고 있다. "$_SERVER["REQUEST_METHOD"]"를 이용하여 현재 HTTP 요청 method가 무엇인지를 결정하고, 그 방식이 POST 방식인 경우만 필요한 처리를 하고 있다.

 

 

   $GLOBALS

 

"$GLOBALS" 변수는 PHP super global variable로서 "$GLOBALS[index]" array에 모든 global variable를 저장하고 있다이 변수를 이용하면 PHP script의 다른 부분이나 function 내부 또는 class methods 내부와 같이 어떤 곳에서든지 global variable를 사용할 수 있다. index에는 변수 이름이 지정되어 있다.

 

해당 변수를 사용하고자 하면 다음과 같은 형식으로 사용할 수 있다.

 

$variable = $GLOBALS[variable-name]

 

아래는 super global variable $GLOBALS을 어떻게 사용하는지를 보여준다.

 

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

 

 


 

Leave a Reply