php 登录表单 - 安全基础

标签 php mysql authentication

我又问了一个简单但不是很明显的问题。这次是在 PHP 和 session 中登录。

据我了解它的安全方面:

  • 在发送到 MySQL 之前检查所有变量

  • 使用 $_POST 隐藏信息

  • 并记录 session

好吧,我有一些“学习/制作”的代码,但如果您能找出我遗漏的东西并提供一些解释,我将不胜感激,并且对像我这样的许多初学者也很有用。 (我已经阅读了很多关于它的问题,但大多数时候答案开始了——尽管事实上代码在很多方面都是灾难性的,但具体的答案是……)

所以这里有:

索引.php

<html>
<head>
<title>Website</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<ul><li><a href="index.php">HOME</a></li><li><a href="menu1.php">menu1</a></li><li><a href="logout.php">logout</a></li></ul>
</body>
</html>

session .php:

<?php 
session_start(); 
if (!isset($_SESSION["txtUserId"])) { 
require "login.php"; 
exit; 
}

登录.php

require_once('db_connect.php');

$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
 // check if the user id and password combination is correct
 $random = '$%hgy5djk3tgbG^bhk';;
 $logname=htmlspecialchars($_POST['txtUserId']);
 $pass=sha1(($_POST['txtPassword']).$random)

 $sql = "SELECT user, pass FROM users WHERE username= :login";
 $stmt = $db->prepare($sql);

  $stmt->bindvalue( ':login', $logname);
  $stmt->execute();
 if $stmt['pass']==$pass {

   // set the session
   $_SESSION['basic_is_logged_in'] = true;
   header('Location: main.php');
  exit;
 }
else {
 $errorMessage = 'Sorry, wrong user id / password';
 require "login.php"; 
 } 
}
?>
 <html>
 <head>
 <title>Login ...</title>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 </head>

 <body>
 <?php
 if ($errorMessage != '') {
  ?>
<p align="center"><strong><font color="#990000"><?php echo $errorMessage; ?></font></strong></p>
<?php
}
?>
<form method="post" name="frmLogin" id="frmLogin">
<table width="400" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td width="150">User Id</td>
<td><input name="txtUserId" type="text" id="txtUserId"></td>
</tr>
<tr>
<td width="150">Password</td>
<td><input name="txtPassword" type="password" id="txtPassword"></td>
</tr>
<tr>
<td width="150">&nbsp;</td>
<td><input type="submit" name="btnLogin" value="Login"></td>
</tr>  
</table>
</form>
</body>
</html>

db_connect.php:

<?php
$hostname = "localhost";
$username = "name";
$password = "pass";
 try {
    $pdo = new PDO("mysql:host=$hostname; dbname=dbnamehere", $username, $password);
//echo "Connected to database"; // check for connection
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

最佳答案

对于初学者来说,如果发生登录错误,您的代码将运行一个无限且中断的循环。您需要一个来自其自身的文件,这是行不通的。然而,您花时间正确理解一些安全基础知识并将 PDO 实现到您的数据库调用中并使用 prepare 语句正确转义您的用户输入,这给我留下了深刻的印象。

编辑

我假设您的登录页面实际存在于 index.php 上。与您的 login.php 脚本中的“回显”语句相反,我将允许整个脚本进行处理,捕获已知 $_SESSION 变量中的任何错误,也许:

$_SESSION['login_valid_error'] = "some text".

在脚本的最后,

if(isset($_SESSION['login_valid_error']) && !empty($_SESSION['login_valid_error'])) {
    header(...) //take the person to logged in page
} else {
   header('Location: index.php');
}

然后回到 index.php,就在你的表单上方创建一个错误通知部分(带有一些 css),如果页面已加载并且变量存在,则回显它:

<?php
if(isset($_SESSION['login_valid_error'])) { 
  echo '<div class="error">'. $_SESSION['login_valid_error'] .'</div>'; 
  unset($_SESSION['login_valid_error']);
}
?>

确保在页面加载后取消设置变量(页面上的任何新尝试都会收集新错误。

我也会考虑使用像 phpass link is here 这样的东西.乍一看似乎有点不知所措,但请下载代码并查看示例。它实际上非常容易集成,并且让您无需担心对用户输入的密码等内容进行加盐/散列处理。

关于php 登录表单 - 安全基础,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7521795/

相关文章:

php - 为大型公司在 phpmyadmin 中创建大型 Web 应用程序

php - 如何设置 Laravel 中间件的执行顺序?

mysql - 您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在 '-db' 附近使用的正确语法

javascript - 如何产生动态div id?在 PHP/JAVASCRIPT/MYSQL 中

c# - System.Security.Claims 命名空间的成员不可用?

authentication - 如何在 XPage 中捕获用户上次登录并在下次登录网站时显示?

php - 无法使用password_verify验证密码

php - 无法安装 Composer - 缺少 json 扩展名

php - 将数据库表中的分组值与关联数组进行比较

mysql - 如何将带有Where条件和大小写的SQL查询转换为MDX