我正在按照教程说明如何使用用户名和密码。但是,我喜欢挑战自己,以便通过将概念更改为保留电子邮件和密码等方式来学习所教授内容的逻辑(因为与教程中涵盖的内容相比,代码中的要求会有所不同) )。这可能听起来很直接,我知道这是经验丰富和精明的程序员,但我正在慢慢地,但肯定地学习编程的交易。我想在你们的帮助下做得更好,因为我已经进一步没有任何错误,但是到目前为止我应该从代码中得到的输出没有发生。我已经包含了我在教程中遵循的所有代码,我不知道如何解决它。如果代码按预期工作,那么我应该收到“OK”消息或“抱歉”错误。感谢大家的帮助。我应该提到我得到一个空白页面!
<br /> <br /><p3>Retrieve Email</p3> <br /> <br />
<?php
function user_data($user_id){
$data = array();
$user_id = (int)$user_id;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if($func_num_args > 1){
unset($func_get_args[0]);
$fields = '`' . implode('`, `', $func_get_args). '`';
$data = mysql_fetch_array(mysql_query("SELECT $fields FROM `users` WHERE `user_id` = '".mysql_real_escape_string($user_id)."'"));
return $data;
}
}
?>
<?php
$mode_allowed = array('email', 'passwd');
if (isset($_GET['mode']) === true && in_array($_GET['mode'], $mode_allowed) === true){
if(isset($_POST['first_name'], $_POST['last_name']) === true && empty($_POST['first_name']) === false && empty($_POST['last_name']) === false){
if((($_POST['first_name'] === $user_data['first_name']) && ($_POST['last_name'] === $user_data['last_name'])) && $user_data['user_id'] === true){
$message[] = '<p class="postad_msg">ok</p>';
}
else{
$errors[] = '<p class="postad_msg">Sorry, we could not find your records!</p>';
}
}
?>
<form method="post" action="retrieve">
<fieldset>
<label for="first_name">First Name * : </label>
<input placeholder="Your first name" type="text" name="first_name" size="30" maxlength="30" value="<?php echo htmlentities($first_name); ?>" /><br /><br />
<label for="last_name">Last Name * : </label>
<input placeholder="Your last name" type="text" name="last_name" size="30" maxlength="30" value="<?php echo htmlentities($last_name); ?>" /><br /><br />
</fieldset>
<fieldset class="center1">
<input class="submit" type="submit" name="submit" value="Retrieve Email " />
</fieldset>
<?php echo output_errors($errors); ?>
<?php echo output_message($message);?>
</form>
<?php
}
else
{
//Redirect user to index for example
}
?>
答案 0 :(得分:0)
好的,所以我试图从我遵循教程的方式开始,并决定使用我从之前的教程中学到的知识来创建一个不同的方法。我测试了这个,它100%工作。请为那些精通PHP的人提供任何反馈/评论代码,我们非常感谢。此外,我决定不检索电子邮件信息,而是此代码显示重置密码。但是,我相信使用此代码检索电子邮件信息也应该是直截了当的。谢谢!这是代码
---- ----类函数
function retrieve($email){
$email = sanitize($email);
$user_data = user_data(user_id_from_email($email), 'user_id', 'first_name');
if ($mode = 'passwd'){
$generated_passwd = substr(sha1(rand(999, 999999)), 0, 8);
change_passwd($user_data['user_id'], $generated_passwd);
email($email, 'Account Password Recovery', "Hello " . $user_data['first_name'] . ",\n\n Your new password is:" . $generated_passwd . " \n\nPlease change your password next time you login \n\n- Admin Team.");
}
}
function user_data($user_id){
$data = array();
$user_id = (int)$user_id;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if($func_num_args > 1){
unset($func_get_args[0]);
$fields = '`' . implode('`, `', $func_get_args). '`';
$data = mysql_fetch_array(mysql_query("SELECT $fields FROM `users` WHERE `user_id` = '".mysql_real_escape_string($user_id)."'"));
return $data;
}
}
function email_exists($email){
$email = sanitize($email);
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email'"), 0) == 1) ? true : false;
}
function user_id_from_email($email){
$email = sanitize($email);
return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `email` = '$email'"), 0, 'user_id');
}
-----检索PHP文件----
require_once ("core/init.php");
redirect_registered();
if(empty($_POST) === false)
{
$required_fields = array('user_id', 'email');
foreach($_POST as $key => $value)
{
if (empty($value) && in_array($key, $required_fields) === true)
{
$errors[] = '<p class="postad_msg">Fields marked with * are required</p>';
break 1;
}
}
if(isset($_POST['email']) === true && !empty($_POST['email']))
{
if(email_exists($_POST['email']) === true)
{
output_message($message);
}
else
{
$errors[] = '<p class="postad_msg">Sorry, your email is not in the database. Please try another email!</p>';
}
}
}
?>
<?php require ("includes/overall/header.php");?>
<div id="mainInfo">
<br /> <br /><p3>Reset Password</p3> <br /> <br />
<?php
if(isset($_GET['success']) && empty($_GET['success']))
{
$message[] = '<p class="postad_msg"> Your Password has been reset. Please check your email.</p>';
}
if(!empty($_POST) && empty($errors) === true)
{
retrieve($_POST['email']);
header('Location: retrieve?success');
exit();
}
else if(empty($errors) === false)
{
output_errors($errors);
}
?>
<form method="post" action="retrieve">
<fieldset>
<label for="email">Email* : </label>
<input placeholder="Enter your email" type="email" name="email" size="30" maxlength="50" value="<?php echo htmlentities($email); ?>" /><br /><br />
</fieldset>
<fieldset class="center1">
<input class="submit" type="submit" name="submit" value="Reset Password" />
</fieldset>
<?php echo output_errors($errors); ?>
<?php echo output_message($message);?>
</form>
</div>
感谢youtube上的原始导师(Alex Garrett)以及所有让PHP学习有趣且具有挑战性的人。我希望这可以有所帮助。
干杯!