php login - error

Discussion in 'PHP' started by ankur602, Oct 23, 2009.

  1. #1
    <?php

    include 'dbc.php';

    if ($_POST['doLogin']=='Login')
    {
    $user_email = mysql_real_escape_string($_POST['usr_email']);
    $md5pass = md5(mysql_real_escape_string($_POST['pwd']));


    if (strpos($user_email,'@') === false) {
    $user_cond = "user_name='$user_email'";
    } else {
    $user_cond = "user_email='$user_email'";

    }

    $sql = "SELECT `id`,`full_name`,`approved` FROM users WHERE
    $user_cond
    AND `pwd` = '$md5pass' AND `banned` = '0'
    ";

    $result = mysql_query($sql) or die (mysql_error());
    $num = mysql_num_rows($result);
    // Match row found with more than 1 results - the user is authenticated.
    if ( $num > 0 ) {

    list($id,$full_name,$approved) = mysql_fetch_row($result);

    if(!$approved) {
    $msg = "Account not activated. Please check your email for activation code";
    header("Location: login.php?msg=$msg");
    exit();
    }

    // this sets session and logs user in

    session_start();
    // this sets variables in the session
    $_SESSION['user_id']= $id;
    $_SESSION['user_name'] = $full_name;

    //set a cookie witout expiry until 60 days

    if(isset($_POST['remember'])){
    setcookie("user_id", $_SESSION['user_id'], time()+60*60*24*60, "/");
    setcookie("user_name", $_SESSION['user_name'], time()+60*60*24*60, "/");
    }


    header("Location:myaccount.php");
    }
    else
    {
    $msg = urlencode("Invalid Login. Please try again with correct user email and password. ");
    header("Location: login.php?msg=$msg");
    }

    }

    ?>

    <html>
    <head>
    <title>Members Login</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript" type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
    <script language="JavaScript" type="text/javascript" src="js/jquery.validate.js"></script>
    <script>
    $(document).ready(function(){
    $("#logForm").validate();
    });
    </script>
    <link href="styles.css" rel="stylesheet" type="text/css">

    </head>

    <body>
    <table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
    <tr>
    <td colspan="3">&nbsp;</td>
    </tr>
    <tr>
    <td width="160" valign="top"><p>&nbsp;</p>
    <p>&nbsp; </p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p></td>
    <td width="732" valign="top"><p>&nbsp;</p>
    <h3 class="titlehdr">Login Users
    </h3>
    <p>
    <?php
    /******************** ERROR MESSAGES*************************************************
    This code is to show error messages
    **************************************************************************/
    if (isset($_GET['msg'])) {
    $msg = mysql_real_escape_string($_GET['msg']);
    echo "<div class=\"msg\">$msg</div>";
    }
    ?>
    </p>
    <form action="login.php" method="post" name="logForm" id="logForm" >
    <table width="65%" border="0" cellpadding="4" cellspacing="4" class="loginform">
    <tr>
    <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
    <td width="28%">Username / Email</td>
    <td width="72%"><input name="usr_email" type="text" class="required" id="txtbox" size="25"></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><input name="pwd" type="password" class="required password" id="txtbox" size="25"></td>
    </tr>
    <tr>
    <td colspan="2"><div align="center">
    <input name="remember" type="checkbox" id="remember" value="1">
    Remember me</div></td>
    </tr>
    <tr>
    <td colspan="2"> <div align="center">
    <p>
    <input name="doLogin" type="submit" id="doLogin3" value="Login">
    </p>
    <p><a href="register.php">Register Free</a><font color="#FF6600">
    |</font> <a href="forgot.php">Forgot Password</a> <font color="#FF6600">
    |</font> <a href="activate.php">Activate Account</a></p>

    </div></td>
    </tr>
    </table>
    <div align="center"></div>
    <p align="center">&nbsp; </p>
    </form>
    <p>&nbsp;</p>

    </td>
    <td width="196" valign="top">&nbsp;</td>
    </tr>
    <tr>
    <td colspan="3">&nbsp;</td>
    </tr>
    </table>

    </body>
    </html>
     
    ankur602, Oct 23, 2009 IP
  2. ankur602

    ankur602 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    code is not retrieving any information from tables ... though i have checked the tables entries ... values are there.... but no results when executed by this code.
     
    ankur602, Oct 23, 2009 IP
  3. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #3
    Try changing the doLogin3 in the submit form part to doLogin so it matches with the

    if ($_POST['doLogin']=='Login')
    {
     
    Silver89, Oct 23, 2009 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Add
    echo '<pre>'.print_r($_POST, true).'</pre>';
    after the include line at the top, and post the output here
     
    JAY6390, Oct 23, 2009 IP
  5. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
    #5
    what exactly the error you are getting.
     
    olddocks, Oct 23, 2009 IP
    Silver89 likes this.
  6. ankur602

    ankur602 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Array
    (
    [usr_email] => anku602@gmail.com
    [pwd] => ankurgupta
    [doLogin] => Login
    )


    Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\homepage\login\login.php:4) in C:\xampp\htdocs\homepage\login\login.php on line 57

    " whole login form "
     
    ankur602, Oct 23, 2009 IP
  7. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #7
    The warning is just because you echo information before trying to use headers, I'm not sure what the problem is check the line above where you assign $md5pass
     
    Silver89, Oct 23, 2009 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I think that error has only occurred due to the line I asked him to add...
     
    JAY6390, Oct 23, 2009 IP
  9. ankur602

    ankur602 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    @danthorpe2002 and @JAY6390, yes, u were right.. i used headers earlier and lil problem with md5pass .... now its ok... thank you dear....
     
    ankur602, Oct 23, 2009 IP
  10. ankur602

    ankur602 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    yes i changed that line .. there was md5(); encryption coversion function ... which i removed ... now its working ok on local server ... but i think it would not b ok, while using the same script over the internet.
     
    ankur602, Oct 23, 2009 IP