简体   繁体   English

PHP帐户激活问题

[英]PHP Account Activation Issues

I wrote a login system for my website. 我为我的网站写了一个登录系统。 When the user registers, the system emails an activation link to the email address the user provided. 当用户注册时,系统通过电子邮件将激活链接发送到用户提供的电子邮件地址。 The link contains two parameters, email and key. 该链接包含两个参数,电子邮件和密钥。 The email parameter has the user's email address and the key parameter has the registration code so that the registration can be verified and changed from pending to confirmed. email参数具有用户的电子邮件地址,密钥参数具有注册码,以便可以验证注册并从挂起更改为已确认。 The activation page is supposed to fetch the Status column from the row that has the email parameter set in the Email column. 激活页面应该从“电子邮件”列中设置了电子邮件参数的行中获取“状态”列。 For some reason, the script decides that any link is valid, and attempts to update the status of the account whether it exists or not. 出于某种原因,脚本确定任何链接有效,并尝试更新帐户的状态是否存在。

Here is my code: 这是我的代码:

<?php

$email = $_GET['email'];
if($email == "") {
  header("Location: http://www.zbrowntechnology.info/yard/register.php?message=Invalid Activation Link!");
  exit;
}
$key = $_GET['key'];
if($key == "") {
  header("Location: http://www.zbrowntechnology.info/yard/register.php?message=Invalid Activation Link!");
  exit;
}

$con = mysql_connect("HOST", "USER", "PASS") or die(mysql_error());
mysql_select_db("zach_yardad", $con) or die(mysql_error());
$query1 = "SELECT `Status` FROM Accounts WHERE `Email`='".mysql_real_escape_string($email)."' AND `Status`='".mysql_real_escape_string($key)."'";
$result1 = mysql_query($query1) or die(mysql_error());
if(mysql_num_rows($result1) <= 0) {
  header("Location: http://www.zbrowntechnology.info/yard/register.php?message=Invalid Activation Link!");
 exit;
} else {
  $query = "UPDATE Accounts SET `Status`='Confirmed' WHERE `Email`='$email'";
  mysql_query($query) or die(mysql_error());
  header("Location: http://www.zbrowntechnology.info/yard/login.php?message=Registration Complete!");
  exit;
}

?>

Here is a valid activation link: 这是一个有效的激活链接:

http://www.zbrowntechnology.info/yard/activate.php?email=zach@zbrowntechnology.com&key=2772190956485245

It will activate that account by following the link, but it will redirect to the login page after activation if the link is not valid. 它将通过链接激活该帐户,但如果链接无效,它将在激活后重定向到登录页面。


EDIT: 编辑:

Here is the result of the query DESCRIBE `Accounts` : 这是查询DESCRIBE `Accounts`的结果:

First Name  varchar(65) NO      NULL     
Last Name   varchar(65) NO      NULL     
Email   varchar(100)    NO      NULL     
Username    varchar(65) NO      NULL     
Password    varchar(65) NO      NULL     
Status  varchar(65) NO      NULL     

I noticed that you are selecting status, to check to see if its confirmed or not.. 我注意到你正在选择状态,检查它是否已确认。

Your Status field is where confirmed / unconfirmed is stored correct? 您的状态字段是否存储了已确认/未确认的位置?

Shouldn't you be checking for Key? 你不应该检查钥匙吗?

In other words, instead of: 换句话说,而不是:

$query1 = "SELECT `Status` FROM Accounts WHERE `Email`='".mysql_real_escape_string($email)."' AND `Status`='".mysql_real_escape_string($key)."'";

Use: 使用:

$query1 = "SELECT `Status` FROM Accounts WHERE `Email`='".mysql_real_escape_string($email)."' AND `Key`='".mysql_real_escape_string($key)."'";

Replacing Key with the name of the field you store the KEY in.. as this is what you are checking for with your $_GET request, email and key.. not email and status. Key替换为您存储KEY的字段的名称..因为这是您使用$ _GET请求,电子邮件和密钥检查的内容..而不是电子邮件和状态。

Can you try changing your code to this: 您可以尝试将代码更改为:

$query1 = mysql_query("SELECT `Status` FROM `Accounts` WHERE `Email`='".mysql_real_escape_string($email)."' AND `Status`='".mysql_real_escape_string($key)."'");
 if(mysql_num_rows($query1) <= 0) {

This should work.. 这应该工作..

If that doesn't work, try this: 如果这不起作用,试试这个:

$query1 = mysql_query("SELECT `Status` FROM `Accounts` WHERE `Email`='".mysql_real_escape_string($email)."' AND `Status`='".mysql_real_escape_string($key)."'", $con);
     if(mysql_num_rows($query1) <= 0) {

====Full Code==== ====完整代码====

<?php
if($_GET['email'] == "") {
header("Location: http://www.zbrowntechnology.info/yard/register.php?message=Invalid Activation Link!");
exit;
}

if($_GET['key'] == "") {
header("Location: http://www.zbrowntechnology.info/yard/register.php?message=Invalid Activation Link!");
exit;
}

$email = mysql_real_escape_string($_GET['email']);
$key = mysql_real_escape_string($_GET['key']);

$con = mysql_connect('HOST', 'USER', 'PASS');
mysql_select_db('zach_yardad', $con) or die(mysql_error());

$query1 = mysql_query("SELECT `Status` FROM `Accounts` WHERE `Email` = '" . $email . "' AND `Status` = '" . $key ."'", $con);
if(mysql_num_rows($query1) <= 0) {
header("Location: http://www.zbrowntechnology.info/yard/register.php?message=Invalid Activation Link!");
exit();
} else {
mysql_query("UPDATE `Accounts` SET `Status`='Confirmed' WHERE `Email`='$email'", $con);
header("Location: http://www.zbrowntechnology.info/yard/login.php?message=Registration Complete!");
exit();
}
?>

First thing I notice is, in your mysql query you are using the status column as a where field. 我注意到的第一件事是,在你的mysql查询中,你使用status列作为where字段。

$query1 = "SELECT `Status` FROM Accounts WHERE `Email`='".mysql_real_escape_string($email)."' AND `Status`='".mysql_real_escape_string($key)."'";

From the way you wrote your code it seems like it should be: 从您编写代码的方式来看,它应该是:

$query1 = "SELECT `Status` FROM Accounts WHERE `Email`='".mysql_real_escape_string($email)."' AND `Key`='".mysql_real_escape_string($key)."'";

To debug the code, how about you comment out the header and exit commands and then just after you define $query1, do a 要调试代码,如何注释掉headerexit命令,然后在定义$ query1之后,执行a

print $query1;

Re-try the page and this will help you see what you are passing to mysql. 重新尝试页面,这将帮助您查看传递给mysql的内容。

UPDATE: 更新:

Reading your recent input I think this might work for you: 阅读您最近的输入我认为这可能对您有用:

if(mysql_num_rows($result1) > 0) {
  $query = "UPDATE Accounts SET `Status`='Confirmed' WHERE `Email`='$email'";
  mysql_query($query) or die(mysql_error());
  header("Location: http://www.zbrowntechnology.info/yard/login.php?message=Registration Complete!");
  exit;

} else {
  header("Location: http://www.zbrowntechnology.info/yard/register.php?message=Invalid Activation Link!");
 exit;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM