Dear friends
I am developing a php online registration form.I created login form which is without database becz admin user in only one to check enquiries.But some thing is wrrong here ..my login.php page code is below.

<!DOCTYPE html>
<html>

<head>
  <title>Login page</title>
</head>

 <center><h1>Admin Login</h1></center>
      <center> <fieldset style="width: 30%; background-color: #F0FFF0"><br>
<body style="background-color: #A9A9A9">
  <form method="post" >

  Username:<input type="text" name="username" placeholder="username" />  <br></br>
  Password:<input type="password" name="password" placeholder="********" /><br></br>

  <input type="submit" name="login" value="login" /> <br></br>
  </form>

</center>
</body>
  </fieldset>
   <p>Copyright 2013. National Industrial Group. All rights reserved.</p>
  <center> <p>Last updated DEC 2014. by Softuniverse-Tech.</p></center>

<?php

   if(isset($_POST['login'])){
$uname = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
$pword = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS);
        if ($uname=="nitcoadmin" && $pword=="nitco@bahrain"){


        //$_SESSION['user']=$uname;
        //$_SESSION['passw']=$pword;
       // $_SESSION['login']='1';
        header('location:enqrep.php');
}
   else
   {
     echo "<center>"."<p  style='color: #FF0000'>"."Invalid username or password"."</p>"."</center> ";
   }
      }
?>



</html>

Warning: Cannot modify header information - headers already sent by (output started at /home/content/62/10830362/html/registration1/login.php:22) in /home/content/62/10830362/html/registration1/login.php on line 33
this error i got.

You can not send headers after you have sent HTML output. Move the following block of PHP code to the top of the script and you'll be fine:

if(isset($_POST['login'])) {
    $uname = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
    $pword = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS);
    if ($uname=="nitcoadmin" && $pword=="nitco@bahrain"){
        //$_SESSION['user']=$uname;
        //$_SESSION['passw']=$pword;
        // $_SESSION['login']='1';
        header('location:enqrep.php');
    }
}

there same problem ..code is here...

<!DOCTYPE html>
<?php

   if(isset($_POST['login'])){
$uname = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
$pword = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS);
        if ($uname=="nitcoadmin" && $pword=="nitco@bahrain"){


        //$_SESSION['user']=$uname;
        //$_SESSION['passw']=$pword;
       // $_SESSION['login']='1';
        header('location:enqrep.php');
}
   else
   {
     echo "<center>"."<p  style='color: #FF0000'>"."Invalid username or password"."</p>"."</center> ";
   }
      }
?>

<html>

<head>
  <title>Login page</title>

</head>

 <center><h1>Admin Login</h1></center>
      <center> <fieldset style="width: 30%; background-color: #F0FFF0"><br>
<body style="background-color: #A9A9A9">
  <form method="post" >
  Username:<input type="text" name="username" placeholder="username" />  <br></br>
  Password:<input type="password" name="password" placeholder="********" /><br></br>
  <input type="submit" name="login" value="login" /> <br></br>
  </form>
</center>
</body>
  </fieldset>
   <p>Copyright 2013. National Industrial Group. All rights reserved.</p>
  <center> <p>Last updated DEC 2014. by Softuniverse-Tech.</p></center>
</html>

if you run your code in the console environment like CLI in PHP, this

<!DOCTYPE html>
<?php

?>

can create problem. You just need to reconsider what you wrote on your code editor. Think about which comes first and last?

so, if submit button is set, the user gets redirected, and what should be shown to the user if it is not set? Should we wrap the html codes inside the else statement or should we not? Think about it and you will be solving this problem like pro.

As I said: no HTML can be output before the header function so put the <!DOCTYPE html> part after the first php block.

<?php
if(isset($_POST['login'])){
$uname = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
$pword = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS);
if ($uname=="nitcoadmin" && $pword=="nitco@bahrain"){
//$_SESSION['user']=$uname;
//$_SESSION['passw']=$pword;
// $_SESSION['login']='1';
header('location:enqrep.php');
}
...
?>
<!DOCTYPE html>
...

And the second part of the else block will have to be changed, too. You will have to store the error message into a variable and display it later on on the page.

frinds i am facing same problem :( code is below...

<?php
   if(isset($_POST['login'])){
$uname = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
$pword = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS);
        if ($uname=="nitcoadmin" && $pword=="nitco@bahrain"){
        //$_SESSION['user']=$uname;
        //$_SESSION['passw']=$pword;
       // $_SESSION['login']='1';
        header('location:enqrep.php');
}
   else
   {
     echo "<center>"."<p  style='color: #FF0000'>"."Invalid username or password"."</p>"."</center> ";
   }
      }
?>
<!DOCTYPE html>
<html>
<head>
  <title>Login page</title>
</head>
 <center><h1>Admin Login</h1></center>
      <center> <fieldset style="width: 30%; background-color: #F0FFF0"><br>
<body style="background-color: #A9A9A9">
  <form method="post" >
  Username:<input type="text" name="username" placeholder="username" />  <br></br>
  Password:<input type="password" name="password" placeholder="********" /><br></br>
  <input type="submit" name="login" value="login" /> <br></br>
  </form>
</center>
</body>
  </fieldset>
   <p>Copyright 2013. National Industrial Group. All rights reserved.</p>
  <center> <p>Last updated DEC 2014. by Softuniverse-Tech.</p></center>
</html>

yes i solved this problem with

include function 

insteade of header

now i m trying to build a session login logout form

yet not solved i include enqrep.php in login.php page... :(#
in how to open enqrep.php when username and password are valid?

Can you post the last version of the code.

yes of course

<?php
   if(isset($_POST['login'])){
$uname = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
$pword = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS);
        if ($uname=="nitcoadmin" && $pword=="nitco@bahrain"){
        //$_SESSION['user']=$uname;
        //$_SESSION['passw']=$pword;
       // $_SESSION['login']='1';
        header('location:enqrep.php');
}
   else
   {
     echo "<center>"."<p  style='color: #FF0000'>"."Invalid username or password"."</p>"."</center> ";
   }
      }
?><html>
<head>
  <title>Login page</title>
</head>
 <center><h1>Admin Login</h1></center>
      <center> <fieldset style="width: 30%; background-color: #F0FFF0"><br>
<body style="background-color: #A9A9A9">
  <form method="post" >
  Username:<input type="text" name="username" placeholder="username" />  <br></br>
  Password:<input type="password" name="password" placeholder="********" /><br></br>
  <input type="submit" name="login" value="login" /> <br></br>
  </form>
</center>
</body>
  </fieldset>
   <p>Copyright 2013. National Industrial Group. All rights reserved.</p>
  <center> <p>Last updated DEC 2014. by Softuniverse-Tech.</p></center>
</html>

The code looks OK. You just have to make sure there is no any character before the <?php tag, not even a single space (or a !DOCTYPE tag). Also if you include other files make sure that they do not contain any output before the header function.

If you still get an error please describe it (error message, expected output, what you really get...).

yes , i solved ...i removed space before <?php
thx broj

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.