nav
nav

Reputation: 319

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

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;

}

Upvotes: 17

Views: 113648

Answers (10)

Muhammad
Muhammad

Reputation: 1350

If you encounter the "Header may not contain more than a single header, new line detected" error in your Laravel application, it's often related to how you handle redirection in your authentication middleware. This error occurs when there are multiple headers being sent to the response.

In your authentication middleware, specifically the redirectTo method, avoid using return redirect()->route('somewhere') as it can lead to this error. When you use redirect(), Laravel internally adds HTTP headers for the redirection.

protected function redirectTo($request)
{
    if (! $request->expectsJson()) {
        // return redirect()->route('signin');  <<<< this is bad
        return route('signin');
    }
}

Upvotes: 2

Nejmeddine Jammeli
Nejmeddine Jammeli

Reputation: 1037

maybe you have to add in the route

'middleware' => ['auth']

in your routes/web.php

Upvotes: 0

erovere
erovere

Reputation: 65

This happened to me when I copy/pasted the URL of my site in my PhpmyAdmin, when I intended to change wp_options URLs (siteurl and home).

I solved it by removing white spaces in my site URL at wp_options!

Upvotes: -1

Mali M
Mali M

Reputation: 171

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';
    }       
}
...

Upvotes: 17

ITC ADU
ITC ADU

Reputation: 56

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%;**

Upvotes: 4

CPTNnimo
CPTNnimo

Reputation: 21

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

Upvotes: 2

Suresh G
Suresh G

Reputation: 21

You should put the URL "http://example.com like this , please avoid "http://example.com/" "/" does give URL Mismatch , so avoid , Same problem will comes wordpress also. So try to use like this.

Upvotes: 0

Jake
Jake

Reputation: 1223

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/"

Upvotes: 34

karmafunk
karmafunk

Reputation: 1453

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

Upvotes: 0

raidenace
raidenace

Reputation: 12836

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

Upvotes: 1

Related Questions