What are some common header settings that can be adjusted using the header() function in PHP?
When working with HTTP headers in PHP, the header() function is commonly used to set various header settings. Some common header settings that can be adjusted using the header() function include setting the content type, redirecting the user to a different page, setting cookies, preventing caching, and setting the HTTP response code.
// Setting the content type to JSON
header('Content-Type: application/json');
// Redirecting the user to a different page
header('Location: http://www.example.com');
// Setting a cookie with a value that expires in 1 hour
setcookie('cookie_name', 'cookie_value', time() + 3600);
// Preventing caching of the page
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
// Setting the HTTP response code to 404 (Not Found)
http_response_code(404);
Related Questions
- Are there any best practices for structuring PHP code to avoid syntax errors and make debugging easier?
- In what ways can error handling and debugging techniques be implemented in PHP to effectively troubleshoot issues like the one experienced during registration in the forum thread?
- How can the issue of losing decimal places be resolved when adding GPS coordinates in PHP?