简体   繁体   English

当主机在服务器中时,php 登录身份验证不起作用

[英]php login authentication is not working when host in server

I have developed a website in php in which I have a login form that checks user's authentication using user name and password.我用 php 开发了一个网站,其中有一个登录表单,可以使用用户名和密码检查用户的身份验证。 The pages to be accessible only by authenticated users checks session variables either true or false.只有经过身份验证的用户才能访问的页面检查会话变量是真还是假。 These variables are set to true if username and password of user are true and I have set that during login check and so users can't access webpages by directly writing page name in URL.如果用户的用户名和密码为真,则这些变量设置为真,并且我在登录检查期间设置了该变量,因此用户无法通过直接在 URL 中写入页面名称来访问网页。 php login authentication is working on localhost but when i hosted to the server authentication is not working. php 登录身份验证正在本地主机上运行,​​但是当我托管到服务器时,身份验证不起作用。 Please help me to solve this problem请帮我解决这个问题

<!DOCTYPE html>
<html>
<head>
    <title>Admin</title>
    <link rel="icon" href="../images/logo.png">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">

    <!-- CSS STYLES -->
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="css/login.css">

    <!-- JS SCRIPT -->

    <script type="text/javascript" src="js/respond.js"></script>
    <script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>


<div class="login-page">
    <div class="login-section">
    <div class="login-heading">
        <img src="../images/logo.png">
        <h2>Department Of Business Administration</h2>
        <div class="border"><div class="line"></div></div>
        <div class="border1"><div class="line"></div></div>
        <h5>Please, <span class="text2"></span></h5>
    </div>
    <?php 
            session_start();
            include '../inc/db.php';
            if (isset($_POST['submit'])) {
                $username = mysqli_real_escape_string($conn,$_POST['username']);
                $password = mysqli_real_escape_string($conn,md5($_POST['password']));
                $_SESSION['login'] = false;

                $query = "SELECT * FROM admin_user WHERE username='$username' AND password='$password' ";
                $rslt = mysqli_query($conn,$query);
                if (mysqli_num_rows($rslt)>0) {
                    $_SESSION['login'] = true;
                    $_SESSION['username'] = $username;
                    header('location:index.php');
                }else{
                    echo  "<div class='alert alert-danger inputmsg-txt'>Username or Password does not matched</div>";
                }
            }
        ?>
    <form  method="POST" class="form-horizontal" action="">
      <div class="form-group">
        <label for="Username" class="col-sm-2 control-label">Username</label>
        <div class="col-sm-10">
          <input type="text" class="form-control" name="username" id="Username" placeholder="Username">
        </div>
      </div>
      <div class="form-group">
        <label for="Password" class="col-sm-2 control-label">Password</label>
        <div class="col-sm-10">
          <input type="password" class="form-control" name="password" id="Password" placeholder="Password">
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button type="submit" name="submit" class="btn btn-success">Sign in</button>
          <a href="" class="btn btn-info">Forgot Password</a>
        </div>
      </div>
    </form>
</div>
</div>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/typed.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>

Try this试试这个

I tested this code and working on the localhost and server both.我测试了这段代码并在本地主机和服务器上工作。

Don't use MD5 for a password.不要使用 MD5 作为密码。

I will suggest you use the Password verify and password hash我建议您使用密码验证和密码哈希

http://php.net/manual/en/function.password-verify.php http://php.net/manual/en/function.password-verify.php

http://php.net/manual/en/function.password-hash.php http://php.net/manual/en/function.password-hash.php

if (isset($_POST['submit'])) {
    $username = $conn->real_escape_string($_POST['username']);
    $password = $conn->real_escape_string(md5($_POST['password']));

    $_SESSION['login'] = false;
    $query = "SELECT * FROM admin_user WHERE username='$username' AND password='$password' ";
    $rslt = mysqli_query($conn,$query);
    $check_user = mysqli_num_rows($rslt);
    if($check_user>0){
    echo "<script>window.open('index.php','_self')</script>";
    }else{

    echo  "<div class='alert alert-danger inputmsg-txt'>Username or Password does not matched</div>";
       }
        $conn->close();
     }

请使用 php artisan tinker,输入密码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP登录身份验证不起作用 - PHP login authentication not working PHP中的登录身份验证系统不起作用 - Login authentication system In PHP not working 切换到LIVE主机时HTTP身份验证不起作用 - HTTP authentication not working when switched to LIVE host Php:登录无法将文件托管到Web服务器。 在本地主机上运行良好 - Php: Login not working on hosting the file to a web server. Works well on local host NTLM身份验证 - 使用PHP获取Windows登录,域和主机 - NTLM Authentication - Get Windows login, domain and host in PHP Symfony 4 登录表单服务器身份验证失败不起作用 - Symfony 4 login form server authentication failed not working 内置服务器的PHP无法与主机中的Vagrant一​​起使用 - PHP Built in Server Not Working with Vagrant in Host Machine 在 LAN 主机不工作的情况下运行 php artisan 服务器 - Running php artisan server with LAN host not working PHP链接可在本地主机中运行,但在Web服务器上托管应用程序时会断开 - PHP links working in local host but breaking when hosting application on web server php require语句在上传到主机时不起作用 - php require statement not working when uploaded to host
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM