2017-02-10 47 views
-1

我想要一个密码重置请求,其中一个链接通过电子邮件发送给用户在我的网站上重置其密码,但PHP文件未从我的表单中提取值。我初始地将表单嵌入到我的PHP中,只是为了让方法起作用,但对于我的格式化,我需要将表单分开。它与嵌入的表单一起工作,但如果我的表单在我的HTML中是分开的,它将无法工作。我的形式HTML--表单没有提取值

<form action="" method="post"> 
<p>Email Address: <input type="email" name="email" size="50" maxlength="255"> 
<input type="submit" name="submit" value="Get New Password"></p> 
</form> 

我的PHP文件( “settings.php文件” 是我的数据库文件) -

<?php 

$email=$_GET['email']; 

include("settings.php"); 
connect(); 
$q="select email from users where email='".$email."'"; 
$r=mysql_query($q); 
$n=mysql_num_rows($r); 
if($n==0){echo "Email id is not registered";die();} 
$token=getRandomString(10); 
$q="insert into tokens (token,email) values ('".$token."','".$email."')"; 
mysql_query($q); 
function getRandomString($length) 
     { 
    $validCharacters = "ABCDEFGHIJKLMNPQRSTUXYVWZ123456789"; 
    $validCharNumber = strlen($validCharacters); 
    $result = ""; 

    for ($i = 0; $i < $length; $i++) { 
     $index = mt_rand(0, $validCharNumber - 1); 
     $result .= $validCharacters[$index]; 
    } 
    return $result;} 
function mailresetlink($to,$token){ 
$subject = "Forgot Password"; 
$uri = 'http://'. $_SERVER['HTTP_HOST'] ; 
$message = ' 
<html> 
<head> 
<title>Forgot Password</title> 
</head> 
<body> 
<p>Click on the given link to reset your password <a  href="'.$uri.'/reset.php?token='.$token.'">Reset Password</a></p> 

</body> 
</html> 
'; 
$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; 
$headers .= 'From: Admin<[email protected]>' . "\r\n"; 

if(mail($to,$subject,$message,$headers)){ 
    echo "We have sent the password reset link to your email id <b>".$to."  </b>"; 
}} 

if(isset($_GET['email']))mailresetlink($email,$token); 
?> 
+0

请注意,你的代码是容易受到SQL注入,并且您正在使用过时的功能'mysql_ *'[更多内容]( http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Ibu

+0

谢谢!我仍然在学习PHP,所以我很感激! – Jamie

回答

1

您正在使用$_GET,而你正在一个POST请求。

你必须使用$_POST代替

试试这个:

$email = $_POST['email']; 
+0

谢谢。我以为我尝试过,但显然我没有。 – Jamie