Page 1 of 1

PHP Tutorial Register and Login

Posted: Sun Nov 18, 2012 5:24 pm
by loner999
Hi, please help me i have this error:
Warning: Cannot modify header information - headers already sent by (output started at C: \ Downloads \ domains \ Registration.local \ Core \ inc \ user.inc.php: 1) in C: \ Downloads \ domains \ Registration.local \ register.php on line 22

and

Warning: Cannot modify header information - headers already sent by (output started at C:\Downloads\domains\Registration.local\Core\inc\user.inc.php:1) in C:\Downloads\domains\Registration.local\login.php on line 20

Register.php
<?php
 
include('Core/init.inc.php');
 
if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password']))
{
        if (empty($_POST['username'])){
                $errors[] = 'The username cannot be empty.';
        }
        if (empty($_POST['password']) || empty($_POST['repeat_password'])){
                $errors[] = 'The password cannot be empty.';
        }
        if ($_POST['password'] !== $_POST['repeat_password']){
                $errors[] = 'Password verfication failed.';
        }
        if (user_exists($_POST['username'])){
                $errors[] = 'The username you enetered is already taken.';
        }
        if (empty($errors) == false){
                add_user($_POST['username'], $_POST['password']);
                $_SESSION['username'] = htmlentities($_POST['username']);
                header('Location: protected.php');
                die();
        }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<link href="Ext/Css/style.css" type="text/css" rel="stylesheet">
</head>
<body>
     <div>
           <?php
              if (empty($errors) === false){
              ?>
              <ul>
             <?php
            
              foreach($errors as $error){
                echo"<li>{$error}</li>";
              }
             ?>
             </ul>
              <?php
             }
          
             ?> 
     </div>
     <form action="" method="post">
       <p>
          <label for="username">Username:</label>
          <input type="text" name="username" id="username" value="<?php if(isset($_POST['username'])) echo htmlentities ($_POST['username']); ?>"/>
       </p>
       <p>
          <label for="username">Password:</label>
          <input type="password" name="password" id="password"/>
       </p>
       <p>
          <label for="username">Repeat Password:</label>
          <input type="password" name="repeat_password" id="repeat_password"/>
       </p>
       <p>
           <input type="submit" value="Register" />
       </p>
     </form>
</body>
</html>
login.php
<?php
include('Core/init.inc.php');
$errors = array ();
if(isset($_POST['username'], $_POST['password'])){
  if (empty($_POST['username'])){
      $errors[] = 'The username cannot be empty.';
  }
  if (empty($_POST['password'])){
      $errors[] = 'The password cannot be empty.';
  }
  if (valid_credentials($_POST['username'],sha1($_POST['password']))=== false){
      $errors[] = 'Username/Password incorect.';
  }
  if (empty($errors)){
      if (isset($_POST['set_cookie']) && $_POST['set_cookie'] =='1'){
         set_cookie('username', $_POST['username'], time()+604800);
         set_cookie('password', sha1($_POST['password']), time()+604800);
      }
      $_SESSION['username'] = htmlentities($_POST['username']);
      header('Location:protected.php');
      die();
  }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<link href="Ext/Css/style.css" type="text/css" rel="stylesheet">
</head>
<body>
     <div>
          <?php
           if (empty($errors) === false){
          ?>
           <ul>
               <?php
                   foreach ($errors as $error){
                       echo "<il>{$error}</il>";
                   }
               ?>
          </ul>
          <?php
           }else{
           echo 'Need an account? <a href="register.php">Register here</a>';
           }
           ?>
     </div>
     <form action="" method="post">
       <p>
          <label for="username">Username:</label>
          <input type="text" name="username" id="username" value="<?php if(isset($_POST['username'])) echo htmlentities ($_POST['username']); ?>"/>
       </p>
       <p>
          <label for="username">Password:</label>
          <input type="text" name="password" id="password"/>
       </p>
       <p>
           <label for="set_cookie">Remeber Me:</label>
           <input type="checkbox" name="set_cookie" id="set_cookie" value="1"/>
       </p>
       <p>
          <input type="submit" value="Login">
       </p>
      </form>
</body>
</html>

Re: PHP Tutorial Register and Login

Posted: Mon Nov 19, 2012 1:25 pm
by ExtremeGaming
You can not have any output before a header. This includes any html or echo/printing. I suspect this is what is causing your error.

Re: PHP Tutorial Register and Login

Posted: Tue Nov 20, 2012 12:09 am
by jacek
Further to the above the key part of that message is "output started at C:\Downloads\domains\Registration.local\Core\inc\user.inc.php:1" which tells you where the output is coming from. Since it's like 1 I would bet that you have an empty line before ethe <?php tag.

Re: PHP Tutorial Register and Login

Posted: Wed Nov 21, 2012 5:22 pm
by loner999
I have a question you do not mind if I used these codes to my site??? :D :D

Re: PHP Tutorial Register and Login

Posted: Wed Nov 21, 2012 8:03 pm
by ExtremeGaming
I'm pretty sure if he minded, he wouldn't use it in a tutorial.

Re: PHP Tutorial Register and Login

Posted: Fri Nov 23, 2012 4:40 am
by Helx
loner999 wrote:I have a question you do not mind if I used these codes to my site??? :D :D
I would suggest that you watch the video and do it yourself instead of copying and pasting (if you were planning on doing that).
Trust me, its rewarding.

Plus Jacek has this thing about people who copy/paste :P