【问题标题】:Change password page for one user with session更改具有会话的一位用户的密码页面
【发布时间】:2018-09-18 21:24:40
【问题描述】:

我正在尝试为练习制作更新密码页面。我创建了一个旧密码字段、一个新密码字段和一个重复密码字段。 我自己创建了这个。如果你们能告诉我我的代码中的错误是什么,我会很高兴,因为我无法使页面正常工作。此外,知道在安全方面我可以做得更好会很有趣。(我还有一个登录、注册、欢迎页面都可以) 你好 session.php:

<?php
include('connection.php');
session_start();

$user_check = $_SESSION['login_user'];


$ses_sql = mysqli_query($db,"select * from clients where email = '$user_check'");

$row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

$_SESSION['email']=$row['email'];
$_SESSION['username']=$row['username'];
$_SESSION['firstname']=$row['firstname'];
$_SESSION['lastname']=$row['lastname'];
$_SESSION['birthdate']=$row['birthdate'];
$_SESSION['street']=$row['street'];
$_SESSION['streetnr']=$row['streetnr'];
$_SESSION['city']=$row['city'];
$_SESSION['plzz']=$row['plzz'];

if(!isset($_SESSION['login_user'])){
  header("location:http://localhost:81/Left_over_youth_website/pages/login.php");
}

?> Connection.php:

  <?php
   define('DB_SERVER', 'localhost');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', '');
   define('DB_DATABASE', 'leftoveryouth');  
   $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

改变pd:

    <?php
 include("../php/session.php");
?>
<html>
    <head>
        <title>Forgot Password</title>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE" />
        <meta content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui" name="viewport">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script type="text/javascript" src="../scripts/newpd.js"></script>
        <link rel="stylesheet" href="../css/changepd.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    </head>
    <body class="img">
        <div class="placeholder">
                <h1 class="logo"><a href="/index.html"class="alogo">Leftover Youth</a></h1>
                <img class="logoo" src="../img/logoo.png" alt="firstimage">
            <form class="form">
                <hr class="verticalline">
                <input class="oldpd" id="oldpd" value="Old Password"
                            onblur="this.value'Old Password':this.value;"
                            onfocus="this.select()"
                            onclick="if (this.value=='Old Password'){this.value=''; this.type='password'}">
                <input class="newpd shine" id="newpd" value="New Password"
                            onblur="this.value'New Password':this.value;"
                            onfocus="this.select()"
                            onclick="if (this.value=='New Password'){this.value=''; this.type='password'}">
                <input class="repeatpd shine" id="repeatpd" value="Repeat Password"
                            onblur="this.value'Repeat Password':this.value;"
                            onfocus="this.select()"
                            onclick="if (this.value=='Repeat Password'){this.value=''; this.type='password'}">
                <p hidden style="color:red;" id="pdontmatch">&#x2612 Password doesn't match</p>
                <p hidden style="color:lightgreen;" id="pmatch">&#x2611 Password matches</p>
                <?php
                    if($_SERVER["REQUEST_METHOD"] == "POST") {
                          $myoldpassword = sha1($_POST['oldpd']);
                          $newpassword = sha1($_POST['newpd']);
                          $repeatpassword = sha1($_POST['repeatpd']);

                          $sql = "SELECT password FROM clients WHERE password = '$myoldpassword'";
                          $result = mysqli_query($db,$sql);
                          if($result){
                              if($newpassword===repeatpassword){
                              $_SESSION["password"] = $newpassword;
                              $update = "UPDATE CLIENTS SET password = mynewpassword";
                              header("location:http://localhost:81/Left_over_youth_website/php/logout.php");
                              }
                              else{
                                  echo('<p>password not updated</p>');
                              }
                          }                            
                       }
                ?>
                <input id="button" type="button" value="Submit" onclick="ausgabe(); marginn();">
                <script>
                function marginn(){
                    document.getElementById('button').style.marginTop = "5px";
                }
                </script>
            </form>
        </div>
    </body>
</html>

如果您需要进一步的解释或代码,请告诉我。

【问题讨论】:

  • 针对初学者的 SQL 注入。在您的登录表单中输入x'; DROP TABLE clients; --。实际上,不要那样做。 unixwiz.net/techtips/sql-injection.html
  • 您应该在每次查询将 $POST 变量传递到数据库之前对其进行清理(防止 SQL 注入)。

标签: php sql session login passwords


【解决方案1】:

-编辑-

因为我无法使页面正常工作。

您编辑了您的问题并添加了该行。我以为你只是在寻求安全方面的建议。究竟是什么不工作?

  • 每次将变量嵌入 您的查询。 where email = '$user_check'")。 您应该改用参数化查询。 http://php.net/manual/en/mysqli.quickstart.prepared-statements.php https://phpdelusions.net/pdo

  • 不要使用 SHA1 进行密码散列 - 它不安全。 Secure hash and salt for PHP passwordsHow to use password_hash

  • 不要在会话中存储非常敏感的数据(如密码)。 $_SESSION["password"] = $newpassword; 即使会话 驻留在服务器上,数据通常存储在文件中 由其他用户访问,尤其是在共享主机中使用时 环境。

  • email 是主键吗?如果没有,您的查询将返回多个 行,然后您将访问 PHP 中的随机行。
    mysqli_query($db,"select * from clients where email = '$user_check'");

  • 在使用之前确保$row 存在且不为空。什么 如果您输入一个不存在的电子邮件地址,会发生什么情况?

  • 通过使用 JS 检查该值是否为默认值,您增加了不必要的复杂性。 onclick="if (this.value=='Old Password'){this.value=''; this.type='password'}" 相反,只需使用占位符属性。 https://html.com/attributes/input-placeholder/

【讨论】:

  • 您可以使用电子邮件和密码登录,我从未使用过主键。
  • @Hazaki - 关键是您的数据库将允许多行使用相同的电子邮件,对吧?如果是这样,您的 PHP 代码没有考虑到这一点,您将访问 some row 您不能保证是您期望的行。您需要更改数据库以使电子邮件唯一,或者您的 PHP 需要通过使用查询中的其他字段来检索您期望的行。具体来说,我指的是这一行$ses_sql = mysqli_query($db,"select * from clients where email = '$user_check'");
猜你喜欢
  • 1970-01-01
  • 2013-07-06
  • 2012-01-19
  • 1970-01-01
  • 2013-05-14
  • 2021-03-29
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多