在php中创建一个非常简单的1用户名/密码登录名 [英] Creating a very simple 1 username/password login in php

查看:84
本文介绍了在php中创建一个非常简单的1用户名/密码登录名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想为1个用户进行一次登录而不存储在数据库中,但是我似乎无法使它正常工作.

I want to make a single login for just 1 user without storing in a database but I can't seem to get this to work.

我的代码:login.php

My code: login.php

<html>
<head>
    <title>Login</title>
</head>
    <h3>Add entry</h3>
    <p> Add another Article</p>
    <form action="trylog.php" method = "post">
        <label for="username">Username</label> <input type="username" id="usename" name="username"><br /><br />
        <label for="password">Password:</label> <input type="text" id="password" name="password"><br /><br />
        <button type = "submit">Login</button>
    </form>
</html>

trylog.php

trylog.php

<html>
    <title>Login</title>
    <body>
        <?php
        $usr = "admin";
        $psw = "password";
        $username = '$_POST[username]';
        $password = '$_POST[password]';
        //$usr == $username && $psw == $password
        session_start();
        if ($_SESSION['login']==true || ($_POST['username']=="admin" && $_POST['password']=="password")) {
            echo "password accepted";
            $_SESSION['login']=true;
        }else {
            echo "incorrect login";
        }
        ?>

        <form name="input" action="adminportal.php" method="get">
            <input type="submit" value="Home">
        </form>
    </body>
</html>

推荐答案

您的代码可能更像:

<?php
session_start();
$errorMsg = "";
$validUser = $_SESSION["login"] === true;
if(isset($_POST["sub"])) {
  $validUser = $_POST["username"] == "admin" && $_POST["password"] == "password";
  if(!$validUser) $errorMsg = "Invalid username or password.";
  else $_SESSION["login"] = true;
}
if($validUser) {
   header("Location: /login-success.php"); die();
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <title>Login</title>
</head>
<body>
  <form name="input" action="" method="post">
    <label for="username">Username:</label><input type="text" value="<?= $_POST["username"] ?>" id="username" name="username" />
    <label for="password">Password:</label><input type="password" value="" id="password" name="password" />
    <div class="error"><?= $errorMsg ?></div>
    <input type="submit" value="Home" name="sub" />
  </form>
</body>
</html>

现在,当基于header('LOCATION:wherever.php)重定向页面时,将session_start()放在页面顶部并进行测试以确保$_SESSION['login'] === true.请记住,如果同时使用$_SESSION['login'] == 1,则==也将为true. 当然,出于安全性考虑,这不是一个好主意,但是我的示例可能会教给您使用PHP的另一种方法.

Now, when the page is redirected based on the header('LOCATION:wherever.php), put session_start() at the top of the page and test to make sure $_SESSION['login'] === true. Remember that == would be true if $_SESSION['login'] == 1 as well. Of course, this is a bad idea for security reasons, but my example may teach you a different way of using PHP.

这篇关于在php中创建一个非常简单的1用户名/密码登录名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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