登录功能不起作用

时间:2015-02-05 16:58:37

标签: javascript php login

好的,所以我做了一个登录按钮,这是功能代码,但它一直显示错误“登录失败,请再试一次......” 提前谢谢...
我并没有真正得到你所要求的东西,所以我把几乎所有的代码都放在原因我正在努力尝试并且无法弄明白......

<?php
include_once ("php_includesss121/check_login_statusopop.php");

// If user is already logged in, header that user away

if ($user_ok == true)
    {
    header("location: user.php?u=" . $_SESSION["username"]);
    exit();
    }

?><?php

// AJAX CALLS THIS LOGIN CODE TO EXECUTE

if (isset($_POST["e"]))
    {

    // CONNECT TO THE DATABASE

    include_once ("php_includesss121/db_conxx12.php");

    // GATHER THE POSTED DATA INTO LOCAL VARIABLES AND SANITIZE

    $e = mysqli_real_escape_string($db_conx, $_POST['e']);
    $p = md5($_POST['p']);

    // GET USER IP ADDRESS

    $ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));

    // FORM DATA ERROR HANDLING

    if ($e == "" || $p == "")
        {
        echo "login_failed";
        exit();
        }
      else
        {

        // END FORM DATA ERROR HANDLING

        $sql = "SELECT id, username, password FROM users WHERE email='$e' AND activated='1' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
        $row = mysqli_fetch_row($query);
        $db_id = $row[0];
        $db_username = $row[1];
        $db_pass_str = $row[2];
        if ($p != $db_pass_str)
            {
            echo "login_failed";
            exit();
            }
          else
            {

            // CREATE THEIR SESSIONS AND COOKIES

            $_SESSION['userid'] = $db_id;
            $_SESSION['username'] = $db_username;
            $_SESSION['password'] = $db_pass_str;
            setcookie("id", $db_id, strtotime('+30 days') , "/", "", "", TRUE);
            setcookie("user", $db_username, strtotime('+30 days') , "/", "", "", TRUE);
            setcookie("pass", $db_pass_str, strtotime('+30 days') , "/", "", "", TRUE);

            // UPDATE THEIR "IP" AND "LASTLOGIN" FIELDS

            $sql = "UPDATE users SET ip='$ip', lastlogin=now() WHERE username='$db_username' LIMIT 1";
            $query = mysqli_query($db_conx, $sql);
            echo $db_username;
            exit();
            }
        }

    exit();
    }

?>
 <!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
            <title>Log In</title>
            <link rel="icon" href="favicon.ico" type="image/x-icon">
                <link rel="stylesheet" href="style/style.css">
                    <script src="js/mainopop.js"></script>
                    <script src="js/ajaxopop.js"></script>
                    <script>
                      function emptyElement(x){
                      _(x).innerHTML = "";
                    }
                      function login(){
                       var e = _("email").value;
                       var p = _("password").value;
                         if(e == "" || p == ""){
                      _("status").innerHTML = "Fill out all of the form data";
                        } else {
                      _("loginbtn").style.display = "none";
                        _("status").innerHTML = 'Please wait...';
                      var ajax = ajaxObj("POST", "login.php");
                      ajax.onreadystatechange = function() {
                      if(ajaxReturn(ajax) == true) {
                      if(ajax.responseText == "login_failed"){
                        _("status").innerHTML = "Login unsuccessful, Please try again...";
                      _("loginbtn").style.display = "block";
                    } else {
                      window.location = "user.php?u="+ajax.responseText;
                    }
                           }
                            }
                    ajax.send("e="+e+"&p="+p);
                    }
                    }
                </script>
                </head>
                <body>
                    <?php
include_once ("template_pageTopLoginPageopop.php"); ?>
                    <div id="pageMiddle">
                        <h2>Welcome ...</h2>
                        <h3>Log In Here</h3>
                        <!-- LOGIN FORM -->
                        <form id="loginform" onsubmit="return false;">
                            <div>Email Address:</div>
                            <input type="text" id="email" onfocus="emptyElement('status')" maxlength="300">
                                <div>Password:</div>
                                <input type="password" id="password" onfocus="emptyElement('status')" maxlength="100">
                                    <br />
                                    <br />
                                    <button id="loginbtn" onclick="login()">Log In</button>
                                    <p id="status"></p>
                                    <a href="forgot_passopop.php">Forgot Your Password?</a>
                                </form>
                                <!-- LOGIN FORM -->
                            </div>
                            <?php
include_once ("template_pageBottomopop.php");
 ?>
                        </body>
                    </html> 

和Javascript文件ajaxopop.js:

function ajaxObj(meth, url) {
    var x = new XMLHttpRequest();
    x.open(meth, url, true);
    x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    return x;
}

function ajaxReturn(x) {
    if (x.readyState == 4 && x.status == 200) {
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

请注意,数据库表中的password应为md5 在PHP中加密,您正在加密它以进一步匹配。如果没有,只需从PHP代码中删除md5加密。