-1

Getting this error: Warning: Cannot modify header information - headers already sent by (output started at /blablabla) in blablabla line 21 require_once("functions.php");. However i need to redirect at that point, the last line of code below is line 21

<?php
require_once("functions.php");
 $username = trim($_POST['username']);
 $password = trim($_POST['password']);
    if ($username&&$password){
    session_start();
     require_once("Connection.php");
      mysqli_select_db($db_server, $db_database) or 
                                                  die("Couldn't find db");
       $username = clean_string($db_server, $username);
       $password = clean_string($db_server, $password);  



       $query = "SELECT * FROM UserDatabase WHERE username='$username'";
        $result = mysqli_query($db_server, $query);
        if($row = mysqli_fetch_array($result)){
        $db_username = $row['username'];
        $db_password = $row['password'];
         if($username==$db_username&&($password)==$db_password){
              $_SESSION['username']=$username;
              $_SESSION['logged']="logged";
              header('Location: Log_homepage.php');
  • Do `functions.php` or `Connection.php` print anything to output? – Flight Odyssey Jan 13 '14 at 22:31
  • Connection prints out a message saying connected, however connection is needed for the whole log in page. – user3182814 Jan 13 '14 at 22:33
  • Infact, connection does not print a message – user3182814 Jan 13 '14 at 22:41
  • You'll have to make sure that there are no echo or print commands called out before the header() change, both in this file and in the require/included ones. If that is not the problem, check for empty spaces or line breaks OUTSIDE of the tag, before the header change. It's very often an astray single space that is hard to detect, but messes up the entire code. – Victoria Ruiz Jan 17 '14 at 15:12

3 Answers3

2

That error comes up when you've sent body data prior to calling header(). Here, it looks like the culprit is somewhere further up in your program. The only thing I see echoed here is the sql error, and since it seems unlikely that you'd be missing that, I'd guess the problem is in functions.php or Connection.php.

jameslafferty
  • 2,152
  • 2
  • 21
  • 25
2

This usually happens when there is already some html before you make a call to change the header(). Make sure there is no HTML output, or even spaces, above it.

Victoria Ruiz
  • 4,913
  • 3
  • 23
  • 40
1

As others have said, that error message is usually caused by having some kind of output prior to the call to header. It could be that the file has text prior to the opening <?php tag, or maybe you called a function like printf that writes to the file. Whatever it is, that sort of thing doesn't work if you're going to call header.

Additionally, when you use header('Location... you're supposed to put an absolute URL according to the HTTP standard. Some browsers will accept a relative URL, but it's still bad practice. If you're trying to modularize your code (and you should) put something like

header('Location: http://' . $_SERVER['SERVER_NAME'] . '/Log_homepage.php');

If you were on, say, example.com, that would be equivalent to

header('Location: http://example.com/Log_homepage.php');
user2752467
  • 864
  • 5
  • 16