0

I am still getting that error when I run my code. Here is my code.I removed all white spaces as well.my form on my html goes to my php (action="lib/login.php") file to check if user exists. If it exist, then redirect. dbconnect.php just connects to my database. can anyone help?

FOLDER Layout:
lib
-dbconnect.php
-login.php (code located below)
index.html

<?php
 require_once("dbconnect.php"); 

 $loginuser = $_POST['username'];
 $loginpw = $_POST['password']; 
 $usertable = "Users";
 $users="Username";
 $pw="Password";
 $query = "SELECT $users, $pw FROM $usertable WHERE $users='$loginuser' AND $pw='$loginpw'";           
 $result = mysql_query($query);
 $num_rows = mysql_num_rows($result);                                  
 if (!$result) {
    die('Invalid query: ' . mysql_error());
}

 if ($num_rows>0) {
    header("Location:../index.html");
    exit();       
}                                      

?>

  • 1
    Welcome to Stack Overflow! Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun the [deprecation process](http://news.php.net/php.internals/53799). See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Instead you should learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you pick PDO [here is a good tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – War10ck Mar 06 '13 at 21:28
  • 1
    Sent by what? The error tells you exactly where the problem is. – Mike B Mar 06 '13 at 21:28
  • Check if username and password are really being posted. If they're not, PHP may be issuing an error when you try to grab them from `$_POST`, thus causing content to be output before the headers. – bfavaretto Mar 06 '13 at 21:29
  • Try checking to see if the variables are being posted as @bfavaretto said. You could do a `var_dump` or `print_r($_POST);` – War10ck Mar 06 '13 at 21:31
  • What is the content of dbconnect.php? Have you stripped the whitespace from that file as well? Is your code generating warning messages before the `header()` call? – Sammitch Mar 06 '13 at 21:38
  • @War10ck I have variables declared $hostname, $username, $password and connecting to database with mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect to database! Please try again later."); mysql_select_db($dbname); – user2141802 Mar 06 '13 at 22:04
  • the values are coming through for $_Post @bfavaretto – user2141802 Mar 06 '13 at 22:05
  • The answer to the linked duplicate explain all possible causes of that error. In short, something is being echoed before your redirect header. You have to find out what it is. – bfavaretto Mar 06 '13 at 22:07

1 Answers1

0

This message appears if you try to send a HTTP header after you already made some output. This is because in HTTP there comes first the headers and then the body. As ouput goes to the body, it is impossible to send headers anymore after output.

As there is no echo, print or whatever output function used in your code, output can be generated by

  1. A PHP errror message
  2. Content before the opening <?php

To 1:

The ob_* set of functions is a good way to catch all output a script regardless if this are error messages or regular output. So you could add the line:

ob_start();

direct after the opening <?php tag.

Then before the call to header() add:

if(ob_get_size() > 0) {
    // there was an error, display it and exit
    ob_end_flush();
    die();
}

Note that in production environment you should disable the display of error messages at all as they can contain sensitive information. You can do by settings display_errors in php.ini to 0 or per script by:

ini_set('display_errors', 0);

To 2:

Assuming that the code you have posted is the complete content of the php file (no spaces or empty lines before <?php ) it could be that you have a problem with the UTF-8 BOM char. BOM means byte order mark and is used internally by the utf-8 encoding. And it is not visible in a text editor. :)

To remove it, there are several solutions out there.. will google one for you ..... http://www.w3.org/International/questions/qa-byte-order-mark.en.php

hek2mgl
  • 152,036
  • 28
  • 249
  • 266