Problem with change header location

OS: Linux Ubuntu 20.04
Sage 10

Hi, I’m trying to create my own login form via facebook and google. I am using the SocialConnect library.
Here are my files:

// resources/views/form-register.blade.php
        <form action="{{ REGISTER_PAGE }}" method="POST">
            <button type="submit" name="provider" value="google">google</button>
            <button type="submit" name="provider" value="fb">fb</button>
        </form>

And the file where I want to handle the event

// app/View/Composers/Register.php

<?php

namespace App\View\Composers;

use SocialConnect\Auth\Service;
use SocialConnect\HttpClient\Curl;
use SocialConnect\HttpClient\RequestFactory;
use SocialConnect\HttpClient\StreamFactory;
use SocialConnect\Common\HttpStack;
use SocialConnect\Provider\Session\Session;


class Register extends Composer
{

    public function __construct()
    {
        // COMMON
        $this->is_logged = false;
        $this->error = false;

        // GOOGLE API INFO
        $this->google_id = '100725804944-o108tp4vdcrellsd0lrfpnl59kdq4j54.apps.googleusercontent.com';
        $this->google_secret = 'GlnL98Xxhi9TlYzrUFdX-Yxi';
        $this->google_redirect = REGISTER_PAGE;
        $this->google_loggin_url = false;

        // FACEBOOK
        $this->fb_api = '320462216157040';
        $this->fb_secret = '879143dacd27e105ed711abd0755e242';
        $this->fb_login_url = false;

        $this->auth = false;
    }

    protected static $views = [
        'register',
    ];

    public function with()
    {
        return [
            'logged' => $this->is_logged,
            'error' => $this->error,
            'auth' => $this->SocialAuth(),

        ];
    }


    public function SocialAuth(){
 
        if(!isset($_POST['provider'])) {
            return false;
        }

        $httpClient = new Curl();
        $httpStack = new HttpStack(
            $httpClient,
            new RequestFactory(),
            new StreamFactory()
        );

        $collectionFactory = null;

        $configureProviders = [
            'redirectUri' => REGISTER_PAGE,
            'provider' => [
                'facebook' => [
                    'applicationId' => '',
                    'applicationSecret' => '',
                    'scope' => ['email'],
                    'options' => [
                        'identity.fields' => [
                            'email',
                            'picture.width(99999)'
                        ],
                    ],
                ],
                'google' => [
                    'applicationId' => $this->google_id,
                    'applicationSecret' => $this->google_secret,
                ],
            ],
        ];


        $service = new Service(
            $httpStack,
            new Session(),
            $configureProviders,
            $collectionFactory
        );

        $providerName = $_POST['provider'];
        $provider = $service->getProvider($providerName);
        write_log($provider->makeAuthUrl());
        header('Location: ' . $provider->makeAuthUrl());
        exit;
    }

}

This class is not complete but i have a problem on this stage. After selecting the login method, the following error appears:

Cannot modify header information - headers already sent by (output started at /home/owline/Local Sites/talenti/app/public/wp-includes/class.wp-scripts.php:237)

I have no idea if it’s my code or sage problem.

The warning indicates that the PHP script already send some body data - which would cause the HTTP headers to be send with no way of later changing them on that request. What happens on that line class.wp-scripts.php:237, is something printed or included or an error caused?

Hm, wp-scripts is default wp-includes file.

This seems to be a classic issue with UTF-8 BOM. WordPress loads a theme template file that contains a BOM which causes PHP to send it as body - together with the HTTP response headers that can’t be changed anymore afterwards.

You’re attempting to modify headers inside of a Composer, which you can’t do. By the time a Composer is being executed, the headers for whatever page you’re on have already been sent (in this case by wp-scripts, apparently). If you need to conditionally modify headers, you’ll need to hook into a action like wp_headers or send_headers, and do it outside of a Composer. Composers are meant for modifying data and passing it to views, not for containing redirect functionality, which appears to be what you’re trying to do here.

1 Like

This topic was automatically closed after 42 days. New replies are no longer allowed.