Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting warning "Header may not contain more than a single header, new line detected"

Tags:

oop

php

I am doing coding in oops for uploading image in PHP. But After submit image, it's giving warning

"Header may not contain more than a single header, new line detected"

Below is my function, on which its giving error

public function ft_redirect($query = '') {

    if (REQUEST_URI) {

        $_SERVER['REQUEST_URI'] = REQUEST_URI;

    }

    $protocol = 'http://';

    if (HTTPS) {

        $protocol = 'https://';
    }

    if (isset($_SERVER['REQUEST_URI'])) {

        if (stristr($_SERVER["REQUEST_URI"], "?")) {

            $requesturi = substr($_SERVER["REQUEST_URI"], 0, strpos($_SERVER["REQUEST_URI"], "?"));

            $location = "Location: {$protocol}{$_SERVER["HTTP_HOST"]}{$requesturi}";
        } else {


            $requesturi = $_SERVER["REQUEST_URI"];

            $location = "Location: {$protocol}{$_SERVER["HTTP_HOST"]}{$requesturi}";

        }

    } else {

        $location = "Location: {$protocol}{$_SERVER["HTTP_HOST"]}{$_SERVER['PHP_SELF']}";

    }

    if (!empty($query)) {

        $location .= "?{$query}";

    }

    header($location);

    exit;

}
like image 715
nav Avatar asked May 01 '13 14:05

nav


5 Answers

in "Illuminate\Auth\Middleware\Authenticate" The method "redirectTo" should return an url path, not the Redirect response.

...
protected function redirectTo()
{
    if(\Auth::user()->hasRole('copy')){
        return '/copy/dashboardCopy';
    }       
}
...
like image 186
Mali M Avatar answered Nov 19 '22 12:11

Mali M


You shouldn't put more than two lines in URL address. Check you URL.

Good URL - "http://mail.google.com"  - 1 line

Bad URL - "http://mail.              - 2 lines
            google.com/"
like image 34
Jake Avatar answered Nov 19 '22 10:11

Jake


This warning occurs to indicate that you might have a new line [/n] in the string content of your variables. Example

  header("Location: ../control.php?post='$title1'&sample='$val'");

here there are 2 variables

$title1 and & $val

so while running if This warning occurs warning

“Header may not contain more than a single header, new line detected”

The solution is To strip out the passable new line contents of the variable Like this

    $val=str_replace(PHP_EOL, '', $val);
    $title1=str_replace(PHP_EOL, '', $title1);

Then you can include the variables in the header


The ideal way of solving it is like this

$url="../control.php?post='$title1'&sample='$val'";
 $url=str_replace(PHP_EOL, '', $url);
 header("Location: $url");

** This will work 100%;**

like image 3
ITC ADU Avatar answered Nov 19 '22 12:11

ITC ADU


Trouble could be in your phpMyAdmin, table wp_options, option_value.

If there is a space before the URL it will generate the ERROR: warning: header may not contain more than a single header, new line detected in...

like image 2
CPTNnimo Avatar answered Nov 19 '22 11:11

CPTNnimo


Seems like the variables you are using to create the Location attribute has a new line character in them. Pass them through urlencode()

like image 1
raidenace Avatar answered Nov 19 '22 12:11

raidenace