Saving URL parameters during PHP redirection

This does not work:

<?php header('Location: www.mysite.com/index.php?foo=bar&var=abc'); ?> 

I can eventually find www.mysite.com/index.php?foo=bar. I think HTML may try to interpret the & var character as a character. The initial variable is passed (after?), But all subsequent ones are not (after &).

+6
source share
2 answers
 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

+3
source

I tested this case with this:

  <? php
     if (isset ($ _ GET ['foo'])) { 
         echo '<pre>';
         print_r ($ _ GET);
         echo '</pre>';
         exit (); 
     }
     $ fp = 'http: //server/header.php? foo = bar & var = abc';
     header ("Location:". $ fp);
     exit ();
     ?>
    

I am calling the address: http: //server/header.php and the redirection works fine with http: //server/header.php? foo = bar & var = abc 'and _GET completed:

  Array
 (
     [foo] => bar
     [var] => abc
 ) 

Note:

  • capitalized first letter.
  • colon and space after "Location"
  • full link
  • call exit ().
On the other hand, make sure that nothing is displayed in the browser BEFORE TRANSFERRING.
0
source

Source: https://habr.com/ru/post/896962/


All Articles