Redirecting Users in PHP
Question:
Can PHP be used to redirect users to a different page without employing a meta refresh? If so, how?
Answer:
Basic Redirection Using Header():
To redirect a user, use the header() function to send an HTTP header containing the new URL. This must precede any HTML or text output.
header('Location: '.$newURL);
Important Considerations:
Alternatives:
Helper Functions:
function redirect($url, $statusCode = 303) { header('Location: ' . $url, true, $statusCode); die(); }
Workarounds for Header() Limitations:
<meta http-equiv="refresh" content="0;url=finalpage.html">
window.location.replace("https://example.com/");
The above is the detailed content of How Can I Redirect Users in PHP Without Using Meta Refresh?. For more information, please follow other related articles on the PHP Chinese website!