PASSWORD RESET

Your destination for complete Tech news

PHP

How to make a redirect in php?

424 0
< 1 min read

To make a redirect in PHP, you can use the header() function. This function sends a raw HTTP header to the browser, which can be used to redirect the user to a different page.

Here is an example of how to use the header() function to redirect the user to a different page:

<?php

// Redirect to example.com
header('Location: http://example.com');
exit;

In this example, the header() function sends a Location header to the browser, telling it to redirect to http://example.com. The exit statement is used to stop the execution of the script after the redirect.

You can also use the header() function to specify the HTTP status code for the redirect. For example, to redirect the user with a 301 “Moved Permanently” status code, you can use the following code:

<?php

// Redirect to example.com with a 301 status code
header('Location: http://example.com', true, 301);
exit;

Note that the header() function must be called before any output is sent to the browser, so it should be the first thing you do in your script. If you have already outputted content, you will need to use a JavaScript redirect instead.

Here is an example of how to use JavaScript to redirect the user to a different page:

<?php

echo '<script>window.location.href = "http://example.com";</script>';
exit;

This will output a JavaScript script that redirects the user to http://example.com.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.