Question

I have the following piece of code

if ($_REQUEST['referer_url'] == "" ) {
  header("location: index.php");
  exit;
}
header('location: http://' . $_REQUEST['referer_url']);
exit;

And I get the error Warning: Header may not contain more than a single header, new line detected. This is because in the header it adds a new line. I tried doing an urlencode like suggested here on stackoverflow, but it redirect me to a server not found error with an odd url containing an undefined index within the url. What else can I try in order to maintain the $_REQUEST['referer_url']

Was it helpful?

Solution

Try this:

$redirect_url = isset($_SERVER['HTTP_REFERER']) && 
                !empty($_SERVER['HTTP_REFERER']) ?
                    $_SERVER['HTTP_REFERER'] : 'index.php';
header("Location: $redirect_url");
exit;

OTHER TIPS

First need to check what you get in $_REQUEST['referer_url'] if you have full url then remove http:// from condition if blank it will be redirect to index.php try

$redirect = (!empty($_REQUEST['referer_url']) ? 'http://'.$_REQUEST['referer_url'] : 'index.php');
header("location: $redirect");
exit;

Ever tried just to trim the referer?

http://php.net/trim

$myRefererUrl = trim($_REQUEST['referer_url']);    
header('location: '.(($myRefererUrl=='')?'index.php':$myRefererUrl));
exit;

With trim, you get rid of line breaks in strings ;)

Try encoding your URL first and it should work fine: http://php.net/manual/en/function.rawurlencode.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top