如何在本地主机上实现密码重置功能

时间:2018-03-22 13:47:08

标签: php mysql

所以我尝试使用php,MySQL和使用UwAmp服务器在登录表单上添加密码重置功能。这必须以这种方式完成,因为我需要将项目放在我的讲师等可以访问的大学UwAmp文件空间中。

我已尝试过以下代码,但显然,它没有发送电子邮件。我是否必须安装邮件客户端才能将内容发送到电子邮件帐户?或者这是否真的可以在本地主机上完成,我应该在单独的页面上模仿电子邮件功能?

<?

include'config.php';
if(isset($_GET['action']))
{          
    if($_GET['action']=="reset")
    {
        $encrypt = mysqli_real_escape_string($connection,$_GET['encrypt']);
        $query = "SELECT id FROM users where md5(90*13+id)='".$encrypt."'";
        $result = mysqli_query($link,$query);
        $Results = mysqli_fetch_array($result);
        if(count($Results)>=1)
        {

        }
        else
        {
            $message = 'Invalid key please try again. <a href="http://demo.phpgang.com/login-signup-in-php/#forget">Forget Password?</a>';
        }
    }
}
elseif(isset($_POST['action']))
{

    $encrypt      = mysqli_real_escape_string($connection,$_POST['action']);
    $password     = mysqli_real_escape_string($connection,$_POST['password']);
    $query = "SELECT id FROM users where md5(90*13+id)='".$encrypt."'";

    $result = mysqli_query($connection,$query);
    $Results = mysqli_fetch_array($result);
    if(count($Results)>=1)
    {
        $query = "update users set password='".md5($password)."' where id='".$Results['id']."'";
        mysqli_query($connection,$query);

        $message = "Your password changed sucessfully <a href=\"http://demo.phpgang.com/login-signup-in-php/\">click here to login</a>.";
    }
    else
    {
        $message = 'Invalid key please try again. <a href="http://demo.phpgang.com/login-signup-in-php/#forget">Forget Password?</a>';
    }
}
else
{
    header("location:login.php");
}

重置传球。还没有被使用,还包含一些错误,比如不使用我的配置文件中的$ link变量,只是为了显示我正在尝试的内容。

<?php

//Login file, log the user checking for correct details


 require_once 'config.php';//Connect to the database


// Define variables and initialize with empty values
$email = $password = "";
$user_err = $pass_err = "";


if($_SERVER["REQUEST_METHOD"] == "POST"){

    // Check if email is empty
    if(empty(trim($_POST["email"]))){
        $user_err = 'Please enter email.';
    } else{
        $email = trim($_POST["email"]);
    }

    // Check if password is empty
    if(empty(trim($_POST['password']))){
        $pass_err = 'Please enter your password.';
    } else{
        $password = trim($_POST['password']);
    }

    // Validate credentials
    if(empty($user_err) && empty($pass_err)){

        // Prepare a select statement
        $sql = "SELECT email, password FROM userdetails WHERE email = ?";

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

            // Set parameters
            $param_email = $email;

            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Store result
                mysqli_stmt_store_result($stmt);

                // Check if email exists, if yes then verify password
                if(mysqli_stmt_num_rows($stmt) == 1){                    
                    // Bind result variables
                    mysqli_stmt_bind_result($stmt, $email, $hashed_password);
                    if(mysqli_stmt_fetch($stmt)){
                        if(password_verify($password, $hashed_password)){
                            /* Session start here to stop users from accessing 
                            welcome page before signing in with valid details*/
                            session_start();
                            $_SESSION['email'] = $email;      
                            header("location: welcomePage.php");
                        } else{
                            // Display an error message if password is not valid
                            $pass_err = 'The password you entered was not valid.';
                        }
                    }
                } else{
                    // Display an error message if email doesn't exist
                    $user_err = 'No account found with that email.';
                }
            } else{
                echo " Something went wrong. Please try again later.";
            }
        }

        // Close statement
        mysqli_stmt_close($stmt);
    }

    // Close connection
    mysqli_close($link);
}


?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Login Page</title>
    <link rel="stylesheet" href="css/style.css">
    <script src="js/script.js"></script>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  </head>
  <body>





      <div class="parent">

      <div class="form_login">

               <h2 class="title">Sign in</h2>


<form name ="credentials" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" onsubmit="return validateForm()" onKeyPress="return checkSubmit(event)">
            <div class="formGroups">

                <input type="text" pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,63}$" name="email" class="form-control" placeholder="email">
                <span id="errmsg"><?php echo $user_err; ?></span>
            </div>    
            <div class="formGroups">

                <input type="password" name="password" class="form-control" placeholder="password">
                <span id="errmsg"><?php echo $pass_err; ?></span>
            </div>
            <div class="form-group">
                <input type="submit" class="btn btn-primary" value="Submit">
                <input type="reset" class="btn btn-default" value="Reset">
            </div>
            <p>Not got an account? <a href="register.php">Register for a new account here</a>.</p>
            <p>Forgot Password? <a href="send_link.php">Reset it here</a></p>
        </form>


          </div>




      </div>


  </body>
</html>

登录表单

{{1}}

0 个答案:

没有答案