This isn't working:
<?php
header('Location: www.mysite.com/index.php?foo=bar&var=abc');
?>
I end up with www.mysite.com/index.php?foo=bar I think HTML might be trying to interpret the &var as a character. The initial variable is passed (after ?) but all subsequent are not (after &).
Most guides will tell you that to make a PHP redirect you can just use the header () function at the top of your pages. To do that, you use the function to send a new URL, like this: This header function should be put before you pass any HTML or text to your users’ browsers, so it should be right at the top of the page.
To do a 301 PHP redirect with parameters to another page we can use the following code: <?php $url = "landingPage2.php?".$_SERVER ['QUERY_STRING']; header ("HTTP/1.1 301 Moved Permanently"); header ("Location: $url"); ?> The entire query parameter string is stored in the following variable. Everything after the question mark.
Almost all developers at least once in their practice have wondered how to get parameters from a URL string with the help of PHP functions. In our tutorial, we will provide you with two simple and handy functions such as pase_url () and parse_str () that will allow you to do that. Read on and check out the options below.
If, in other words, you are using a header redirect to protect a particular page, it offers you no protection at all. That’s why you have to stop processing the rest of the page, in case the redirection is ignored. The way to do that is to append die () or exit () after your redirect: Next, let’s talk about relative and absolute URLs in redirects.
if( isset($_SERVER['HTTPS'] ) ) {
header('Location: https://'.$_SERVER['SERVER_NAME'].'/index.php?'.$_SERVER['QUERY_STRING']);
}
else{
header('Location: http://'.$_SERVER['SERVER_NAME'].'/index.php?'.$_SERVER['QUERY_STRING']);
}
use htmlspecialchars to prevent html injection
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With