好吧,我已经使用php / mysql身份验证系统进行了数周,将它从教程中拼凑起来并随时学习。我现在处于一个棘手的部分,即密码forgot.php
页面。我对phpmail
的经验不多,而且这段代码似乎有很多根本性的错误。
首先,由于我添加了有关phpmail
信息的代码,因此页面只是空白。但即使它正常工作,用户点击提交后页面也会变空白。不知道是什么让表格保持均匀显示。另外,不确定我是否正确地处理了一个工作的php密码忘记了电子邮件表单。
login.php上的代码:
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
Remember me: <input type="checkbox" name="remember" /><br />
<input type="submit" name="submit" value="Login" />
<a href="forgot.php">Forgot Password?</a>
<?php
session_start();
require_once("functions.php");
require_once("db-const.php");
require("class.phpmailer.php");
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
if (logged_in() == true) {
redirect_to("profile.php");
}
?>
<html>
<head>
<title>Forgot your Username or Password?</title>
</head>
<body>
<h1>Forgot your Username or Password?</h1>
<p>Please enter your email address below.</p>
<form action="forgot.php" method="post">
Email: <input type="text" name="email" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if (isset($_POST['submit'])) {
## connect mysql server
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();
}
## query database
# fetch data from mysql database
$sql = "SELECT email FROM users WHERE email LIKE '{$_POST['email']}' LIMIT 1";
if ($result = $mysqli->query($sql)) {
$user = $result->fetch_array();
} else {
echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>";
exit();
}
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.practice.com"; // SMTP server
$mail->From = "support@practice.com";
$mail->Subject = "login information";
$mail->Body = "Hello, here is your login information. User name is: $username and your
password is $password;"
$mail->WordWrap = 50;
if ($result->num_rows == 1) {
echo "<p>Login credentials have been sent to <b>{$_POST['email']}</b></p>";
} else {
echo "<p>Sorry, no user found with this email.</p>";
}
}
?>
<a href="login.php">Login</a> | <a href="register.php">Register</a>
</body>
</html>
答案 0 :(得分:0)
遗憾的是,正如其他人所指出的那样,您的代码存在很多错误。
$mail->send();
)ini_set('display_errors', true);
一般来说,你重新发明轮子并且在这个过程中没有获得太多。如果你真的想要一个有效的auth系统(这不仅仅是一个编码练习),我建议你看一下php login project。