PHP Tutorial Register and Login

Post here is you are having problems with any of the tutorials.
Post Reply
loner999
Posts: 2
Joined: Sun Nov 18, 2012 5:07 pm

PHP Tutorial Register and Login

Post 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>
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: PHP Tutorial Register and Login

Post 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.
<?php while(!$succeed = try()); ?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial Register and Login

Post 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.
Image
loner999
Posts: 2
Joined: Sun Nov 18, 2012 5:07 pm

Re: PHP Tutorial Register and Login

Post by loner999 »

I have a question you do not mind if I used these codes to my site??? :D :D
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: PHP Tutorial Register and Login

Post by ExtremeGaming »

I'm pretty sure if he minded, he wouldn't use it in a tutorial.
<?php while(!$succeed = try()); ?>
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: PHP Tutorial Register and Login

Post 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
Post Reply