PHP header() Function - ProgrammerTech


PHP header() Function

  • Share this:
PHP header() Function

PHP header Function

The ( header ) function is used to move the user to the desired location. For example, a file that is in the workflow or is directed to a website, depending on what we specify the path to. As soon as the browser reads the header command, it will be executed and thus transferred to the specified path directly, and we can set a time period for the conversion.


header("Location: The path we want to go");
header("refresh:5;url=The path we want");

We write header, then we open parentheses and put two quotes, and inside the quotes we put. The word Location, then two dots, and after the colon we put the path we want to go to, whether a website or an internal file. And if we want to set a time period for the path to be converted, we write header and then parentheses with quotes inside. And inside these two tags, we write refresh, then two dots, and after the two dots, we write the number of seconds it is waiting for. And then the user switches our position at the top 5 seconds and then closes it with the simi-colin tag and writes the word url and then the = then sign. We write the path we want to go to after the time period we have specified.


Suppose we want to direct the user to our site this current site, the code will be as follows.


The http protocol is responsible for the communication between a web server and a web browser and is used to send a request. From your device to the web server and back the request in the form of web pages from the server to the client browser. The https protocol is responsible for the secure communication between the web server and the web browser. It performs credit card transactions and sensitive data.

When you run the above code, the result will be.

php code output window to direct the user to the path


In the event that you did not set the protocol and put the link directly, for example, you wrote, it will. It recognizes it as a second file that exists in the same work path and will say that it does not exist so you must put the protocol.

Let's take an example of this for a clearer understanding.


And when you run the code in the server, the result will be.

Routing code without https mode via header php


In the event that we want to convert the user to another file path located in the same project file. Suppose we have another file with the same work path and its name is kaissar.php and we wrote the tag h1 in it and inside it is Programmer Tech.

The code in the first file will be as follows.

header("Location: kaissar.php");

Here we wrote the word header and between the brackets we wrote Location, then we wrote him the path that he should go to, and the file was named kaissar.php.

And when we run this code in the server, we will get the result.

print result on screen by header php


As we can see, we have printed the data in the first file and we have put the tag h1 and inside it the name of our site Programmer Tech.. Sometimes we want to set a time after running the first page. In order to convert to the requested page or path via refresh as in the example.


We typed header and then in parentheses we typed the word refresh and chose the time is 5 seconds. After five seconds, the user is automatically transferred to the kaissar.php file and printed on the screen.

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.