javascript - 用户授权/未授权访问网页并导致错误

标签 javascript php jquery ajax

我的页面是main.php,它是这样编写的

<?php
  include_once('createtables.php');
  include('function.php');

?>

<!DOCTYPE html>
<html lang="en">
<head>
  <title></title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="../includes/css/bootstrap.min.css">
  <link rel="stylesheet" href="../includes/css/main.css">
  <script src="../includes/js/jQuery.js"></script>
  <script src="../includes/js/login.js"></script>
  <script src="../includes/js/bootstrap.min.js"></script>
</head>
<body>
  <div class="container-fluid">
    <form id="lgform" action="checklogin.php" method="post">
      <h4 class="text-primary" id="llc"><img src="../includes/img/chatballoon.png"> Network Chat </h4>

      <div class="input-group" id="firstone">
        <span class="input-group-addon">
          <span class="glyphicon glyphicon-user"></span>
        </span>
        <input type="text" class="form-control" placeholder="Enter Account name" id="username" name="username" autofocus>
      </div>
      <div class="input-group" id="secondtwo">
        <span class="input-group-addon" id="password-addon">
          <span class="glyphicon glyphicon-lock"></span>
        </span>
        <input type="password" class="form-control" placeholder="Enter your password" aria-decribedby="password-addon" id="password" name="password" autofocus>
      </div>
      <a href="createaccount.php" id="signup">Create Account</a>
      <input type="submit" class="pull-right btn btn-primary btn-sm" name="submit" id="submit" value="Enter now">
    </form>
  </div>
</body>
</html>

这是我的 checklogin php 是这样的:

<?php
    ob_start();
    session_start();

    include("function.php");

    $conn = new Functions();
    $conn = $conn->db;

    //define username and password

    $username = $_POST['username'];
    $password = $_POST['password'];

    $username = stripslashes($username);
    $password = stripcslashes($password);
    $salt = "dctech2015ABcRXd";
    $password = md5($password) . $salt;
    $password = sha1($password);


    //SELECT QUERY TO FIND IF INPUT INFORMATION WAS ON DATABASE
    $stmt = $conn->query("SELECT * FROM users WHERE username ='$username' AND password='$password'");

    //LOOP ENTIRE DATABASE ROW
    $count = $stmt->rowCount();

    //IF INFORMATION FOUND SELECT STATUS IF ALREADY ONLINE OR NOT
    if($count == 1){
        $status;
        $stmt = $conn->prepare("SELECT status FROM users WHERE username='$username'");
        $stmt->execute();
        while($checkstatus = $stmt->fetch(PDO::FETCH_OBJ)){
            $status = $checkstatus->status;
        }
        if($status == 'Online'){
            echo "online";

        }else{

            echo "success";
            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;

            $stmt = $conn->prepare("UPDATE users SET status='Online' WHERE username='$username'");
            $stmt->execute();
        }

    }

    ob_end_flush();
?>

这里使用ajax:

$(document).ready(function(){
    $("#submit").click(function(e){
        e.preventDefault();

        var username = document.getElementById("username").value;
        var password = document.getElementById("password").value;

        if(username==""){
            alert("Please enter your account name");
        }else if(password == ""){
            alert("Please enter your password");
        }else{
            $.ajax({
                type: "POST",
                url: "checklogin.php",
                data: "username="+username+"&password="+password,
                success: function(data){
                    if(data == "success"){
                        window.location.href="chat.php";
                    }
                    else if(data == "online"){
                        alert("account is already online");
                    }else{
                        alert("Invalid Account name/Account password");
                    }
                },error:function(data){
                    alert("an error occured through data");
                }
            });
        }

        document.getElementById("username").value = "";
        document.getElementById("password").value = "";
    });
    return false;
});

浏览器可访问 checklogin.php 文件的问题。我想要的是避免未经授权的用户访问此页面,因为即使是登录用户,如果他们在浏览器上键入 register.php,它也会转到该页面并显示用户名错误等。

此类错误: This error

最佳答案

您应该使用标准的http状态代码进行响应:401,以便浏览器知道它加载页面失败:

if($count == 1){
   ...
} else {
    header("HTTP/1.1 401 Unauthorized");
    // or for php 5.4:
    http_response_code(401);
}

更新: 对于您后来添加的错误:

在访问 $_POST 中的值之前,请检查它们是否存在:

if (isset($_POST['username']) && isset($_POST['password'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    // ... all the rest of your code that depends on $_POST comes here
}

关于javascript - 用户授权/未授权访问网页并导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35030190/

相关文章:

javascript - 某些英特尔集成图形处理器中的 WebGL 故障

javascript - 如何阻止 YouTube 使用内置浏览器?

php - 公共(public)函数未执行

asp.net - 具有来自 ASP.NET 的多个返回值的 JQuery Ajax

javascript - 放下三个物体后触发图像变化

javascript - [Custom Directive]将 scope.$watch on link 与 scope.$watch on controller 的区别

javascript - imageView 不显示 ACS 照片 #appcelerator

javascript - 如何计算年龄?

php - 仅当满足 php 条件时才运行 javascript

javascript - 颜色框帖子表单值