-5

This is my user log in page. After the user enter log in details, I want to store cookie files and direct the user to home page. However, I keep getting this error I have tried every possible way to solve it and I have read other questions posted on here but nothing did solve my problem... am I missing something here? what could it be?

<?php //<--- line 4

error messages:

Warning: Cannot modify header information - headers already sent by (output started at /www/99k.org/p/h/o/phoneclassmate/htdocs/login.php:4) in /www/99k.org/p/h/o/phoneclassmate/htdocs/login.php on line 6
ElSS
  • 329
  • 5
  • 16
  • as I have said above, I did read previous questions that was asked but I still did not figure it out yet... so I posted a question of my own. – ElSS May 18 '13 at 16:22
  • 1
    You have to look at the error message. It tells you where the output was started. – Janos Pasztor May 18 '13 at 16:23
  • 1. This is too localized. It will closed anyway. 2. This is basic PHP troubleshooting. 3. That duplicate question tells you how to fix this. – John Conde May 18 '13 at 16:23
  • 1
    Your script may be vulnerable to SQL injections. – Gumbo May 18 '13 at 16:23
  • Why is there an "@" character in front of your mysql_connect() function? – imulsion May 18 '13 at 16:25
  • @Gumbo Cannot modify header information - headers already sent by – ElSS May 18 '13 at 16:29
  • @JohnConde, I've read that earlier and tried to fix it but I still get the same error.. It's frustrating this is why I decided to post it here – ElSS May 18 '13 at 16:31
  • @Janoszen, (output started at /www/99k.org/p/h/o/phoneclassmate/htdocs/login.php:4). I don't know why it gives me this though I checked white spaces. – ElSS May 18 '13 at 16:57
  • As @Janoszen says, you'll have a line number from where output started, in your error message. You've not added that to your question, or indicated where that line number points - that is important information that we cannot do without. – halfer May 18 '13 at 17:02
  • OK, now added; great. Have you _viewed source_ in your browser to see if there is any output you're not aware of? – halfer May 18 '13 at 17:04
  • just checked, there isn't any output except for the form – ElSS May 18 '13 at 17:08
  • Ah, so the ` – halfer May 18 '13 at 17:12
  • I had this "" before – ElSS May 18 '13 at 17:23
  • Yep, that's HTML output - it's very well covered in the link that @John gave you. Glad you fixed it, anyway. – halfer May 18 '13 at 17:26

2 Answers2

2

The issue is that you're trying to send HTTP headers (Location:) after HTML code.

You should refactor your code to avoid that; or as a hack you can work around it using output buffering, that'll allow you to modify HTTP headers after you started outputting your HTML content. Not a great way to handle the problem IMHO but good to know anyway; see http://ca.php.net/manual/en/function.header.php#refsect1-function.header-notes

Patrice Levesque
  • 2,079
  • 17
  • 16
1

You need to enclose your if statement block as shown below:

if($_POST['submit']) {
    $hour = time() + 3600;
    setcookie('username', $_POST['username'], $hour,'/');
    setcookie('password', $_POST['pass'], $hour,'/');
    header("Location: tutorhomepage.php");
}

Also, you need to move connection etc. part just after the above segment.

<?php

if($_POST['submit']){
    $hour = time() + 3600;
    setcookie('username', $_POST['username'], $hour,'/');
    setcookie('password', $_POST['pass'], $hour,'/');
    header("Location: tutorhomepage.php");
}
//------ server and mysql connection
$connection = mysql_connect('mywebsite', 'username', 'password') or die('Unable to connect');
mysql_select_db('myDB') or die('unbable to select DB');
//------log in cookie check
if (isset($_COOKIE['username'])) {
    $username = $_COOKIE['username'];
    $pass = $_COOKIE['password'];
    $check = mysql_query("SELECT * FROM tutors WHERE username = '$username'") or die(mysql_error());
    while ($info = mysql_fetch_row($check)) {
        if ($pass != $info[1]) {    
        } else {
            header("Location: /tutorhomepage.php");
        }
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login</title>
    </head>    
    <body><?php
//------- if form is submitted
if (isset($_POST['submit'])) {
    // check if the user enetered login details
    if (!$_POST['username'] | !$_POST['pass']) {
        die('You must enter the required data.');
    }
// and so on
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • It still gives me the error – ElSS May 18 '13 at 16:29
  • @ElShaikhaSue Check recent edit – hjpotter92 May 18 '13 at 16:46
  • I get this for line 6,7 and 8. Warning: Cannot modify header information - headers already sent by (output started at /www/99k.org/p/h/o/phoneclassmate/htdocs/login.php:4) in /www/99k.org/p/h/o/phoneclassmate/htdocs/login.php on line 6 – ElSS May 18 '13 at 17:00