user1567935 user1567935 - 2 years ago 126
PHP Question

php header function working

I have written the following code to refresh page and print the random number but i don`t understand its logic.

<!DOCTYPE html>
<title>Random Refresh</title>


<h1>Random refresh</h1>

<p>page is refreshed every 5 seconds </p>

<br> </br>

<p>Radom number between 10 to 100 is :


header("Refresh: 5; url=randomRefresh.php");




According to my knowledge php is a server side language so code inside the php block should be executed once.

So how does header function refresh/redirect the page ?

does the output html page of php file use implicit Ajax request ?

what is happening under the hood ?

Answer Source

The "Refresh: 5; url=randomRefresh.php" function is executed browser side. The php headers() function are executed server-side

header() in PHP merely sets an additional or existing header that is sent to the client's browser upon page load but Refresh: 5; url=randomRefresh.php is interpreted by the client's browser to refresh the page after 5 seconds. Other examples of headers are like status that can be 200, 301, 302, 404.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download