Jump to content

php login


whisperkay

Recommended Posts

Hello everyone. I am currently working on my first website, and i am encountering some problems with logging in users after registration. the site url is - www.eastlogicsystems.com. The error i am recieving goes thus -

Warning: Cannot modify header information - headers already sent by (output started at /home/eastlogi/public_html/login.php:50) in /home/eastlogi/public_html/login.php on line 143

Warning: Cannot modify header information - headers already sent by (output started at /home/eastlogi/public_html/login.php:50) in /home/eastlogi/public_html/login.php on line 145

 

the funniest thing is that the code is working fine on my local server, using xamp, but when it is hosted on an internent server, it refuses to work

 

 

This is my login.php code-

<?php
 // Connects to your Database
 
require_once("connect.php");
 //Checks if there is a login cookie
 if(isset($_COOKIE['ID_my_site']))
//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM userinfo WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['pass1'])
{
}
else
{
header("Location: index2.php");

;
}
}
}

 //if the login form is submitted

 if (isset($_POST['submit'])) { // if form has been submitted



 // makes sure they filled it in

     if(!$_POST['username'] | !$_POST['pass1'])
     {

         die('You did not fill in a required field.');

     }

     // checks it against the database



     if (!get_magic_quotes_gpc())
     {

         $_POST['email'] = addslashes($_POST['email']);

     }

     $check = mysql_query("SELECT * FROM userinfo WHERE username = '".$_POST['username']."'")or die(mysql_error());



 //Gives error if user dosen't exist

 $check2 = mysql_num_rows($check);

 if ($check2 == 0) {

         die('That user does not exist in our database. <a href=register.php>Click Here to Register</a>');

                 }

 while($info = mysql_fetch_array( $check ))     

 {

 $_POST['pass1'] = stripslashes($_POST['pass1']);

     $info['pass1'] = stripslashes($info['pass1']);

     $_POST['pass1'] = md5($_POST['pass1']);



 //gives error if the password is wrong

     if ($_POST['pass1'] != $info['pass1']) {

         die('Incorrect password, please try again.');

     }
else

 {

 
 // if login is ok then we add a cookie

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

      $hour = time() + 3600;

line 143 - setcookie(ID_my_site, $_POST['username'], $hour);

line 145 -  setcookie(Key_my_site, $_POST['pass1'], $hour);    

line 147 - header("Location: index2.php");


 //then redirect them to the members area


 }

 }

 }

 else

{    

 

 // if they are not logged in

 ?>
 <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
        <table width="80%" border="5" cellspacing="5" cellpadding="5">
  <tr>
    <td colspan="2" align="center">Login here</td>
   
  </tr>
  <tr>
    <td>Username</td>
    <td> <input name="username" type="text" value="" size="30" maxlength="20" /></td>
  </tr>
  <tr>
    <td>Password</td>
    <td> <input name="pass1" type="password" value="" size="30" maxlength="20" /></td>
  </tr>
  <tr>
    <td><input name="submit" type="submit" value="login" /></td>
    <td><a href="register.php">New User? Register</a></td>
  </tr>
</table>
 </form>
<?php

 }

 

 ?>

 Please i need help.

Edited by whisperkay
Link to comment
Share on other sites

the suggestion to use ob_end_flush won't have any effect on your live server and at best is putting a band-aid on top of a special case condition on your development system.

 

did you even try to change the output_buffering setting in your php.ini?

 

btw - changing the setting won't fix anything, it will just make your development system operate the same as your live server and allow you to fix this problem in your code and avoid this problem in any future code you write and test on your development system.

 

edit: and i just noticed the forum section where this thread is at and the code/problem has nothing to do with the php application, frameworks... forum section. moving the php help forum section....

Edited by mac_gyver
Link to comment
Share on other sites

  • 3 weeks later...

The error you are getting is only the beginning of your challenge for building a log-on script.  You should take a look at or copy another log-on script and modify it to fit your user table.

 

My first google search turned up a reasonably simple start here:

http://www.phpeasystep.com/phptu/6.html

 

Read through it, modify it, run it, debug your changes until it works and you understand every line.  It took me a very long time to get it right and I am still perfecting my own log-in script today.

 

It is very easy to get it wrong.  Don't be discouraged.  When you are starting out it is sometimes much easier to cut and paste an example and then learn from it as you make it your own.

 

Also... Take time to indent and be consistent with your indentations and { } placement.  It makes debugging so much easier.

 

And lastly... It could just be that you have an extra space or line outside of your php tags causing the "Warning: Cannot modify header information" error.  That is why you need to clean up those indentations and brackets.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.