-1

Ok, so I am trying to make a login script but get this two errors when i execute it:

Warning: session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent in /home/henrirkm/public_html/login/login.php on line 29

Warning: Cannot modify header information - headers already sent by (output started at /home/henrirkm/public_html/res/mysql.php:2) in /home/henrirkm/public_html/login/login.php on line 47

My code goes like this:

<?php

session_start();

// MYSQL database connection

include "../res/mysql.php";

$username = mysql_real_escape_string($_POST['username']);

$password = mysql_real_escape_string($_POST['password']);



//select from login table

$qry = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";

$result=mysql_query($qry);



//Check whether the query was successful or not

    if($result) {

        if(mysql_num_rows($result) == 1) {

            //Login Successful

            session_regenerate_id();

            $member = mysql_fetch_assoc($result);

            $_SESSION['id'] = $member['id'];

            $_SESSION['username'] = $member['username'];

            $_SESSION['email'] = $member['email'];

            session_write_close();







            header("location: ../home/index.php");

            exit();

        }else {

            //Login failed

            header("location: index.php?info=loginerror");

            exit();

        }

    }else {

        die("Query failed");

    }

?>

Please help me! Thanks :-)

4 Answers4

3

It is output here: res/mysql.php line 2.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

If you echo any content (unless you are using output buffering), the headers will be sent as well.

In the code you posted, I don't see any echoed content, but you're also including ../res/mysql.php. If that echoes content, your headers will be sent by merely including that file.

Mind that you don't actually need to echo anything. Any whitespace before the opening <? or after the closing ?> might also cause the problem. Therefor, it is commonly advised to remove the closing ?> altogether.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

As per the PHP documentation stated at: http://php.net/manual/en/function.header.php

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Just pass your headers at the beginning of the script, this will ensure that the headers are passed properly and the file can be downloaded.

aaron
  • 697
  • 4
  • 11
  • OP's `location:` header is dependent on the PHP logic and can't simply be moved to the beginning of the script. Can you identify which line in the above code would be sending output that would affect the `header` call? – Joseph Oct 08 '13 at 19:38
  • @Joseph: I agree that header is dependent on the PHP logic, but by placing it right at the beginning you avoid having any output echoed out which would send the headers. Here, the `include` file could also have an output sent which will then render the header declaration else where in the script useless. Hence, in my personal opinion i would always work with logic keeping in mind the headers should not get modified. – aaron Oct 08 '13 at 19:53
  • I understand, but that solution is no applicable to this problem, and as noted by AbraCadaver, it can be solved another way. Sometimes it is not feasible to relocate the `header` call. – Joseph Oct 08 '13 at 19:55
-1

THE ERROR CLEARLY TELL U WHERE THE OP HAS STARTED

output started at /home/henrirkm/public_html/res/mysql.php:2
user2092317
  • 3,226
  • 5
  • 24
  • 35