jdborg jdborg - 4 months ago 17
HTML Question

Keeping URL parameters during PHP redirect

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 &).

Answer
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