我是一名PHP初学者,我刚开始创建自己的网站。我遇到了mail()无理由返回FALSE的问题。
我的想法是实现密码重置机制。当用户输入他的电子邮件时,HTML表单会将信息传递给php脚本,php脚本将执行验证并通过随机生成的密码重置密码,然后将此临时密码发送到用户电子邮件。以下是我的代码:
<?php
session_start();
$email = $_POST['email'];
function random_string_generator ($character_options, $length_of_generated_string)
{
$random_string = "";
//count the number of characters available for picking
$num_valid_charactor = strlen($character_options);
for ( $i = 0 ; $i < $length_of_generated_string; $i++ ){
$random_pick = mt_rand(1, $num_valid_charactor);
$random_char = $character_options[$random_pick-1];
$random_string .= $random_char;
}
return $random_string;
}
function createSalt()
{
$string = md5(uniqid(rand(), true));
return substr($string, 0, 3);
}
$dbhost = 'localhost';
$dbname = 'my_db';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);
$query = "SELECT password, salt FROM users WHERE email = '$email';";
$result = mysql_query($query);
if(mysql_num_rows($result) < 1)
{ //no such user exists
$_SESSION['error'] = '1';
$_SESSION['message'] = 'user does not exist';
header("Location: Forgot_password.php");
}
else
{
$random_pick_options = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$temporary_password = random_string_generator($random_pick_options, 12);
$hash = hash('sha256', $temporary_password);
$salt = createSalt();
$hash = hash('sha256', $salt . $hash);
$query = "UPDATE users set password=$hash, salt=$salt where email=$email;";
mysql_query($query);
//Email user the temporary password
$email_from = "auto@alemc.org";
$email_to = $email;
$message = "Dear user,\n\n your temporary passowrd is: " . $temporary_password . "\n This is an auto-generated email. Please do not reply to this email address.\n\n ALEMC\n";
mail($email_to, "ALEMC: Password Reset", $message, "From: $email_from");
$_SESSION['message'] = "password reset";
header("Location: Login_form.php");
}
?>
知道出了什么问题吗?或者有没有更好的方法来发送邮件然后使用PHP?
答案 0 :(得分:2)
你的php设置一定有问题。我个人喜欢使用PHPMailer从php发送电子邮件时你有更多的选择。
答案 1 :(得分:0)
我个人建议您使用swiftmailer(http://swiftmailer.org/)之类的东西,它只是有效。看看你的PHP邮件/服务器配置是如何设置的。并采取措施保护自己免受SQL注入。