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
- What are the potential pitfalls of not including a FORM tag in PHP code when creating interactive elements like buttons or input fields?
- What are the potential risks of storing PHP code in a database and executing it using eval()?
- What is the purpose of using classes/objects in PHP, aside from making the code more organized?