JIYIK CN >

Current Location:Home > Learning > PROGRAM > PHP >

Header location in PHP

Author:JIYIK Last Updated:2025/04/12 Views:

This article will introduce header()the concept of the function and its syntax in PHP. It will cover the rules for writing headers in PHP. This approach also applies to Content-Typethe and Content-Dispositionheaders.

location:We will also cover header strings in this article . We will explain the usage and essence of location header in PHP. This article will demonstrate how the location header can send a response code and redirect the browser to another page.


Header()Introduction to functions and their syntax in PHP

header()The function is a built-in PHP function that allows us to send raw HTTP headers to the client. The headers sent are in raw form. We should call header()the function before sending any output. Any form of output, such as HTML tags or output sent by PHP forms, should be discarded before sending the header information. So we can control the information that the server sends to the browser before any output.

header()The syntax of the function is: header(string,replace,http_response_code);. The function accepts three parameters. The first parameter is the header string. There are two types of header strings. The first type is HTTP/a string that starts with . It specifies the HTTP code to be sent to the browser. The second type of header string is location:the header, which redirects the browser to the specified location. The next parameter in the function is replace, which represents a Boolean value. It is an optional parameter that determines whether the header should replace a previous similar header. The third parameter http_response codeis also an optional parameter that is used to force the HTTP response code to a specified value.

For example, create a header()function and use the title string as Content-Type. Content-TypeWrite the value of as application/pdf. Create another header()function again. This time, write the title string as Content-Disposition. Assign the value of the string as attachment. Don't forget to put a semicolon after it. Write another property after the semicolon filenameand provide the file name as download.pdf.

When we run the following script, a download dialog box will appear. It asks you to download a file named download.pdf. pdfThe first header indicates that the file should be pdfin format, and the second header indicates the file's file name and forces the browser to display a dialog box to save the file.

Sample code:

#php 7.x
<?php
header('Content-Type: application/pdf'); 
header('Content-Disposition: attachment; filename="downloaded.pdf"');
?>

header()Using functions and location:title strings in PHP

We can use the redirect header()function with the header string in PHP location:. The header string redirects a web page to a specified location. It is commonly used in web pages to redirect users to a specific page after submitting their input. For example, when a user enters the correct credentials at login time, we can redirect them to the home page using the header location. We can header()specify a boolean value and a response code in the redirect function. However, these parameters are optional. The default boolean value is true, which means it will replace previous similar headers. We can also provide a response code as the third parameter. The default response code is 302. For example, we can write an array to a file and redirect the current page to another page that displays a message that the file has been written. We can file_put_contents()write to a file using the redirect function.

For example, $daycreate an array on the variable. Create keys as weatherand timeand corresponding values ​​as Sunnyand 1:30 pm. Then use file_input_contents()and specify a file file.txtas the first argument. Use print_r()the function as the second argument. Provide the variable $dayand the boolean value trueas print_r()arguments to the function. ifEvaluate the entire expression using the condition. ifUse the function inside the block header(). Within the function, specify the position as message.php. Specify the position using a colon :. Note that there should not be any gap between the locationand colons. Create a PHP file . Display a message in the file stating that the file has been written.:message.php

In the above example, the array is written to the file file.txt. ifThe condition evaluates to true and header()the function redirects the location to message.php. Hence, the output is displayed. We can also see the changed URL in the address bar. If there is another header function below the existing one, the latter header will replace the previous one. This is because in header()the function, replacethe default value of the option is true.

Code example:

#php 7.x
<?php
$day = array (
    'weather' => 'Sunny',
    'time' => '1:30 pm',
);
if(file_put_contents('file.txt', print_r($day, true))){
    header("location: message.php");
}
?>

Output:

The file has been written.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Check if a Post exists in PHP

Publish Date:2025/04/13 Views:170 Category:PHP

PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss

PHP with Ajax

Publish Date:2025/04/13 Views:139 Category:PHP

We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Returns the article tag with ID from the action page

Publish Date:2025/04/13 Views:80 Category:PHP

Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at

Switching PHP versions on Ubuntu

Publish Date:2025/04/13 Views:78 Category:PHP

Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t

Resizing images in PHP

Publish Date:2025/04/13 Views:155 Category:PHP

In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using

PHP upload image

Publish Date:2025/04/13 Views:61 Category:PHP

We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image

Creating a signature from Hash_hmac() and Sha256 in PHP

Publish Date:2025/04/13 Views:107 Category:PHP

PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i

Updating PHP 7.x to 7.4 on CentOS

Publish Date:2025/04/13 Views:131 Category:PHP

This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial