PHP注册用户名/密码不正确 [英] PHP Registration Username/Password Incorrect

查看:70
本文介绍了PHP注册用户名/密码不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这里的问题是什么。用户数据在我的MySQL数据库中,并且正确无误。但是,当我尝试登录时,我收到一条错误消息,指出用户/密码不正确。我正在尝试使用用户的电子邮件地址登录。另外我想在会话中添加名字和用户ID。

I am not sure what the problem is here. The user data is in my MySQL database, and correct. However when I try to login I get an error saying user/password is incorrect. I am trying to login using the users email address. In addition I want to add the first name, and user id to the session.

    <?php
session_start();
include_once 'dbconnect_new.php';

if(isset($_SESSION['user'])!="")
{
    header("Location: ../index.php");
}

if(isset($_POST['btn-login']))
{
    $s_email = mysql_real_escape_string($_POST['email']);
    $s_password = mysql_real_escape_string($_POST['password']);

    $s_email = trim($s_email);
    $s_password = trim($s_password);

    $res=mysql_query("SELECT student_id, student_password, student_firstname FROM studentdata WHERE student_email='$s_email'");
    $row=mysql_fetch_array($res);

    $count = mysql_num_rows($res); // if uname/pass correct it returns must be 1 row

    if($count == 1 && $row['student_password']==md5($s_password))
    {
        $_SESSION['user'] = $row['student_id'];
        header("Location: ../index.php");
    }
    else
    {
        ?>
    <script>
        alert('Username / Password Seems Wrong !');

    </script>
    <?php
    }

}
?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">

        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>New Reg Page</title>
            <link rel="stylesheet" href="style.css" type="text/css" />
        </head>

        <body>
            <center>
                <div id="login-form">
                    <form method="post">
                        <table align="center" width="30%" border="0">
                            <tr>
                                <td>
                                    <input type="text" name="email" placeholder="Your Email" required />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input type="password" name="password" placeholder="Your Password" required />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <button type="submit" name="btn-login">Sign In</button>
                                </td>
                            </tr>
                            <tr>
                                <td><a href="register_new.php">Sign Up Here</a></td>
                            </tr>
                        </table>
                    </form>
                </div>
            </center>
        </body>

        </html>


推荐答案

试用此代码: -

    $s_email = mysql_real_escape_string($_POST['email']);
    $s_password = mysql_real_escape_string($_POST['password']);
    $s_email = trim($s_email);
    $s_password = md5(trim($s_password));

    $res=mysql_query("SELECT student_id, student_firstname FROM studentdata WHERE student_email='$s_email' AND student_password = '$s_password'");

    if (!$res) {
        // Debug query result by below code
        //echo 'Could not run query: ' . mysql_error();
        //exit;
        echo '<script language="javascript">';
        echo 'alert("Username / Password Seems Wrong !")';
        echo '</script>';
    }else{
      $row = mysql_fetch_row($result);
      $stu_id = $row[0];         
      $stu_fname =  $row[1];
      $_SESSION['user'] = $stu_id;
      header("Location: ../index.php");
    }  

希望这会对你有所帮助:)

Hope this will help you :)

这篇关于PHP注册用户名/密码不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆