0

I am building a website that receive POST variables from a Mikrotik router Hotspot page. I used for offline test XAMPP v3.2.2 with PHP 7.1. Everything work fine offline but when i put the website on an online Webserver provided by Cpanel with PHP 7.1 (ea-php71) i got an error with header instruction for redirection.

I got this error :

PHP Warning: Cannot modify header information - headers already sent by

The source code of the page who gave me error

    <?php
    session_start();
    require_once dirname(__FILE__).'/includes/wf_DbOperations.php';
    if(!isset($_SESSION['logincust']))
    {
        header('Location: index.php');
    }
?>
<!DOCTYPE html>
<!--[if IE 9]> <html lang="en" class="ie9"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en">
<!--<![endif]-->
    <head>
        <title>...</title>
    </head>
    <body>
        <?php
            if(isset($_SESSION['oauth_providerG']))
            {
                $db = new DbOperations();
                if(!$db->checkOAUTH($_SESSION['oauth_providerG'],$_SESSION['oauth_uid']))
                {
                    if($db->addUserMAC($_SESSION['oauth_uid'],$_SESSION['mac']))
                    {
                        $db->addUserGoogle($_SESSION['oauth_providerG'],$_SESSION['oauth_uid'],$_SESSION['first_name'],$_SESSION['last_name'],$_SESSION['email'],$_SESSION['gender'],$_SESSION['locale'],$_SESSION['picture'],$_SESSION['link']);
                        header('Location: '.$_SESSION['srvLink'].'/glogin.html');
                    }
                }
                else
                {
                    if($db->addUserMAC($_SESSION['oauth_uid'],$_SESSION['mac']))
                        {
                            header('Location: '.$_SESSION['srvLink'].'/glogin.html');
                        }
                }
            }
            if(isset($_SESSION['oauth_providerF']))
            {
                $db = new DbOperations();
                if(!$db->checkOAUTH($_SESSION['oauth_providerF'],$_SESSION['oauth_uid']))
                {
                    if(isset($_SESSION['email']))
                    {
                        if($db->addUserMAC($_SESSION['oauth_uid'],$_SESSION['mac']))
                        {
                            $db->addUserFacebook($_SESSION['oauth_providerF'],$_SESSION['oauth_uid'],$_SESSION['first_name'],$_SESSION['last_name'],$_SESSION['email']);
                            header('Location: '.$_SESSION['srvLink'].'/glogin.html');
                        }
                    }
                    else
                    {
                        if($db->addUserMAC($_SESSION['oauth_uid'],$_SESSION['mac']))
                        {
                            $db->addUserFacebookNoMail($_SESSION['oauth_providerF'],$_SESSION['oauth_uid'],$_SESSION['first_name'],$_SESSION['last_name']);
                            header('Location: '.$_SESSION['srvLink'].'/glogin.html');
                        }
                    }
                }
                else{
                        if($db->addUserMAC($_SESSION['oauth_uid'],$_SESSION['mac']))
                        {
                          header('Location: '.$_SESSION['srvLink'].'/glogin.html');
                        }
                }
            }
        ?>

    </body>
</html>

I have try redirection with JS script location.replace but it doesn't keep variable send by Mikrotik router.

Thanks for your help.

T.Rudy
  • 3
  • 2

3 Answers3

0

You have a space before the first PHP tag.

The web server will send those spaces before getting to the header stuff.

Matthew Page
  • 746
  • 5
  • 15
0

In your PHP script you use header(). Somewhere you try to manipulate data from header(), but this data was already sent as you can read in this Error message

PHP Warning: Cannot modify header information - headers already sent by

So after all your header() make sure you tell the script to exit correctly. source

Sebastian Waldbauer
  • 674
  • 1
  • 10
  • 17
  • I have added exit; instruction after all header(), now no error but the script show a white page. Some debugging tips?? – T.Rudy Jan 20 '19 at 16:42
  • Your script doenst show any kind of data, its only redirecting. You can use ```echo $_SESSION['logincust']``` to check if data is present. – Sebastian Waldbauer Jan 20 '19 at 16:45
0
if(!isset($_SESSION['logincust']))
{
    header('Location: index.php');
    exit(0);
}

If you have any code (Html or Php) under header you should use exit.

Eren
  • 101
  • 2
  • 6