PHP header function usage example
Example 2, prohibit the page from being cached in IE
Description: If the web pages on the server change frequently, set Expires to -1, indicating immediate expiration. If a web page is updated at 1 am every day, you can set Expires to 1 am the next day. When the HTTP1.1 server specifies CacheControl = no-cache, the browser will not cache the web page. Legacy HTTP 1.0 servers cannot use the Cache-Control header. So for backward compatibility with HTTP 1.0 servers, IE provides special support for HTTP using the Pragma:no-cache header. If the client communicates with the server over a secure connection (https://) and the server returns the Pragma:no-cache header in the response, Internet Explorer does not cache the response. Note: Pragma:no-cache prevents caching only when used in a secure connection. If used in a non-secure page, the handling is the same as Expires:-1. The page will be cached but marked as expired immediately. http-equiv meta tag: You can use http-equiv meta to mark the specified http message header in the html page. Older versions of IE may not support html meta tags, so it's best to use http message headers to disable caching. Example 3: Let the user's browser display a file not found message. Online information shows: PHP’s function header() can send the Status header to the browser. For example:
. In fact, the response returned by the browser is:
The first part is the version of the HTTP protocol (HTTP-Version); The second part is the status code (Status); The third part is Reason-Phrase. Example 4, allowing users to download files (hidden file location) The html tag can be used to download ordinary files. If you want to keep the file confidential and you cannot tell others the file link, you can use the header function to download the file.
Example 5, enter the content before the header function Generally speaking, HTML content cannot be output before the header function. Similarly, there are setcookie() and session functions. These functions need to add message header information to the output stream. If there are echo statements before header() is executed, when header() is encountered later, a “Warning: Cannot modify header information – headers already sent by….” error will be reported. There cannot be any text, blank lines, carriage returns, etc. in front of these functions, and it is best to add the exit() function after the header() function. For example, the following incorrect writing has a blank line between the two php code snippets:
Cause analysis: When a PHP script starts executing, it can send http message header (title) information and body information at the same time. The http message header (from the header() or SetCookie() function) is not sent immediately, instead, it is saved to a list. In this way, the title information can be modified, including the default title (such as the Content-Type title). However, once the script sends any non-header output (for example, using HTML or a print() call), then PHP must first send all headers and then terminate the HTTP header. Then continue to send the main data. From this point on, any attempt to add or modify Header information is not allowed, and one of the above error messages will be sent. Solution: Modify php.ini to turn on caching (output_buffering), or use the caching functions ob_start(), ob_end_flush(), etc. in the program. Principle analysis: When output_buffering is enabled, PHP does not send HTTP headers when the script sends output. Instead, it pipes this output into a dynamically growing cache (only available in PHP 4.0, which has a centralized output mechanism). You can modify/add headers, or set cookies, because headers are not actually sent. When all scripts terminate, PHP will automatically send HTTP headers to the browser, and then send the contents of the output buffer. Attached are some other examples of php header functions.
|

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
