将变量设置为$get值时出现问题

wdebmtf2  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(310)

刚刚开始接触sql和php,并试图建立一个简单的用户帐户系统-一个忘记密码的电子邮件链接。
我下面有一个脚本,我试图用它来更新用户的密码。他们收到一封带有链接的电子邮件。链接如下所示: http://localhost:8887/email-password-reset.php?id=1 .
我知道这是不安全的,很容易破解!我计划使用随机生成的数字串,一旦我得到这个工作!
我正在尝试使用 $_GET 函数来获取 id . 但是我不相信 $_GET["id"] 正在被传递到 $param_id 变量。
我是新来的,所以不知道如何纠正它!
如何将参数id设置为 $_GET 价值?
至少我认为问题出在控制台的错误上( Undefined variable: param_id!) 提前谢谢!

<?php
//session_start();
require_once "config.php";

// Define variables and initialize with empty values
$new_password = $confirm_password = "";
$new_password_err = $confirm_password_err = "";
$param_id = ''; 

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

    // Validate new password
    if(empty(trim($_POST["new_password"]))){
        $new_password_err = "Please enter the new password.";
    } elseif(strlen(trim($_POST["new_password"])) < 6){
        $new_password_err = "Password must have atleast 6 characters.";
    } else{
        $new_password = trim($_POST["new_password"]);
    }

    // Validate confirm password
    if(empty(trim($_POST["confirm_password"]))){
        $confirm_password_err = "Please confirm the password.";
    } else{
        $confirm_password = trim($_POST["confirm_password"]);
        if(empty($new_password_err) && ($new_password != $confirm_password)){
            $confirm_password_err = "Password did not match.";
        }
    }

    // Check input errors before updating the database
    if(empty($new_password_err) && empty($confirm_password_err)){
        // Prepare an update statement
        $sql = "UPDATE users SET password = ? WHERE id = ?";

        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "si", $param_password, $param_id);

            // Set parameters
            $param_password = password_hash($new_password, PASSWORD_DEFAULT);
            $param_id = (isset($_GET['id']) ? $_GET['id'] : '');

            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Password updated successfully. Destroy the session, and redirect to login page
                session_destroy();
                header("location: login.php");
                exit();
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }

        // Close statement
        mysqli_stmt_close($stmt);
    }

    // Close connection
    mysqli_close($link);
}
?>
avkwfej4

avkwfej41#

他们收到一封带有链接的电子邮件。链接如下所示:

http://localhost:8887/email-password-reset.php?id=1

我假设如果用户点击链接将有一个“新密码”的形式。然后表单将数据发布到包含您使用的sciprt的某个url。
使用 $_GET['id'] 您需要确保表单上的url(可能看起来像 action="http://localhost:8887/process.php" )有get参数。所以看起来 action="http://localhost:8887/process.php?id=1" 以及错误 Undefined variable: param_id! 是因为你用 param_id 在申报之前。所以,假设它看起来像:

if($stmt = mysqli_prepare($link, $sql)){

  // Set parameters
  $param_password = password_hash($new_password, PASSWORD_DEFAULT);
  $param_id = (isset($_GET['id']) ? $_GET['id'] : '');

  // Bind variables to the prepared statement as parameters
  mysqli_stmt_bind_param($stmt, "si", $param_password, $param_id);

  // Attempt to execute the prepared statement
  if(mysqli_stmt_execute($stmt)){
     // Password updated successfully. Destroy the session, and redirect to login page
     session_destroy();
     header("location: login.php");
     exit();
  } else{
     echo "Oops! Something went wrong. Please try again later.";
  }
2guxujil

2guxujil2#

粗略一瞥:
它只会进入你的大脑 if 如果它是一个 POST 类型,根据定义是 GET 类型。您可能需要删除/更改它。
其次,您正在使用 $param_id 分配前 $param_idGET 变量。
如果唯一的地方,似乎是这样,你正在设置 $param_id 为什么不在第一行声明呢 '' ?

相关问题