header function

DATE : 2023/11/3

[PHP#func : header]

이번 PHP function 편에서는 header 함수에 관한 내용을 다루어볼 것이다. 😎

header 함수는 아래와 같이

header("Location: DESTINATION_PAGE");

괄호 안에 Location: 이동할 페이지 형태로 작성한다.

header 함수는 redirection 하도록 괄호 안에 작성된 값을 response에 넣어주는 역할

쉽게 말해 페이지 이동을 하기 위해 사용하는 함수이다. (이때의 status code는 302 found!)

간단하게 예시를 보면

사용자가 입력한 값이 "segfault"일 때만 hello.html로 이동하는 페이지가 있다고 상상해보자.

이때 작성된 코드는 다음과 같은 형태일 것이다.

$value = $_GET['value'];

if($value == "segfault") {
    header("Location: hello.html");
} else {
    ...
}

<input>에 "segfault"를 넣고 Enter를 누르면

hello.html로 이동한 걸 볼 수 있다.

HTTP/1.1 302 Found
Date: Fri, 03 Nov 2023 03:12:54 GMT
Server: Apache/2.4.56 (Win64) OpenSSL/1.1.1t PHP/8.2.4
X-Powered-By: PHP/8.2.4
Location: hello.html
Content-Length: 0
Connection: close
Content-Type: text/html; charset=UTF-8

burp suite으로 response 내용을 확인해보면

hello.html 값을 가지는 Location header가 들어가 있다.

이렇게 header function을 사용하면 header 안에 작성한 내용이 response header에 들어가면서

결과적으로 redirection 되는 302 Found response를 받을 수 있게 되는 것이다~

[ summary ]

(1) header() : 페이지 이동을 위해 사용할 수 있는 함수

(2) 괄호 안에 작성한 내용을 response header에 추가 시키는 방식으로 동작한다.

Last updated