How to redirect to another page
Short answer
Send the Location header and end the program:
<?php
header('Location: /login');
exit;A permanent redirect
The default code is 302 — temporary. If the page moved for good, pass 301:
<?php
header('Location: /new-address', true, 301);
exit;With parameters
Build the parameters with http_build_query() — it encodes the values by itself:
<?php
echo '/search?' . http_build_query(['q' => 'tea & coffee', 'page' => 2]);Output:
/search?q=tea+%26+coffee&page=2Functions used in this recipe
- header — Sends an HTTP response header.
- http_build_query — Builds a URL query string from an array.