-1

I'm currently working on a CMS and got to a stage where I got the basic functions that I needed for it, so I decided to upload it to my server and test out if everything works on there, because everything did work on my localhost (XAMPP).

Now I keep getting this error when I log in, as I want the user to be re-directed to the index page when he logs in:

Warning: Cannot modify header information - headers already sent by (output started at $/MyCMS/registration.php:29) in $/MyCMS/includes/forms/login_form.php on line 10

I've looked around several posts of people that have had the same problem as I did, yet they always had some extra space or something which made the header fail.

Anyhow, here is the part of the registration.php

<?php
    session_start();
    require_once('includes/db_connect/config.php');
    require('includes/db_connect/database.inc.php');
    require_once('includes/functions/functions.php');

    if(isset($_POST['email-register']) && isset($_POST['password-register'])){
        $email = secureFormInput((isset($_POST['email-register']))) ? $_POST['email-register'] : '';
        $password = secureFormInput((isset($_POST['password-register']))) ? $_POST['password-register'] : '';

        if(checkIfExists($email)){
            header('location: ?status=user%20already%20exists');
        } else {
            doRegister($email,$password);
        }
    }

    if(isLoggedIn()){
        header('location: index.php');
    };
?>

<!DOCTYPE html>
<html>
    <head>
        <title>MyCMS - register</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="styles/main.css" type="text/css" rel="stylesheet"/>
        <?php include('includes/bootstrap_links_for_head.php'); //line 29?>
    </head>

This is the bootstrap_links_for_head.hp, not sure if it's needed but since it throws the error on that part I'll just post it.

<link href="styles/bootstrap-theme.css" type="text/css" rel="stylesheet"/>
<link href="styles/bootstrap-theme.min.css" type="text/css" rel="stylesheet"/>
<link href="styles/bootstrap.css" type="text/css" rel="stylesheet"/>
<link href="styles/bootstrap.min.css" type="text/css" rel="stylesheet"/>

This is the login_form part that is throwing the error

if(isset($_POST['email']) && isset($_POST['password'])){
        $email = secureFormInput((isset($_POST['email']))) ? $_POST['email'] : '';
        $password = secureFormInput((isset($_POST['password']))) ? $_POST['password'] : '';
        // line 10
        header('location: index.php');
        doLogIn($email,$password);
    }

I've been trying ways to work around this problem but I can't seem to find the solution to this problem..

Hope someone could point out what I'm doing wrong here and tell me how to fix it.

Thanks in advance!

Handige Harrie
  • 147
  • 3
  • 13
  • Are you `include`ing `registration.php` inside `login_form.php`? – deceze Nov 20 '13 at 17:33
  • Why don't you have any exit after your header redirects? – Mark Baker Nov 20 '13 at 17:33
  • @deceze The registration.php is actually a seperate page from login_form, though the login_form is included in my navbar, so they do run along eachother. – Handige Harrie Nov 20 '13 at 17:36
  • @MarkBaker I didn't know that exit() was a necessary to be used after header, I'll try it out and see if it might work – Handige Harrie Nov 20 '13 at 17:38
  • header() only sets the headers to be sent, but doesn't terminate actually send them (you may wish to set several different headers, so actually terminating wouldn't be the right thing for PHP to do itself); but if you don't want the html output to be sent to the browser with the header redirects, then you should have an exit after issuing those header() redirect statements – Mark Baker Nov 20 '13 at 17:40
  • Hmm I see, that might be actually quite useful in the future. Though it doesn't really reflect on this situation – Handige Harrie Nov 20 '13 at 17:48

2 Answers2

1

The three Spaces in front of the <?php are the problem. And Maybe if you have UTF-8 and a old version of PHP, the BOM will make problems.

schnawel007
  • 3,982
  • 3
  • 19
  • 27
  • The three spaces were actually put there by stackoverflow code, so they are not actually there (edited it). Second, I do use UTF-8 though my server uses PHP 5.3, which shouldn't cause any problems – Handige Harrie Nov 20 '13 at 17:35
1

Your problem is this, When it says headers already sent, that means there is an output to the page anywhere before you call this header() function. And what is it? it is this:

<!DOCTYPE html>
<html>
    <head>
        <title>MyCMS - register</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

as you see before you initiate the header function there is already something on the page. remove those lines or call your function before anything gets printed on the page.

Justin
  • 172
  • 2
  • 15