开发者

How to implement an activate.php script?

I am trying to create a script that verifies (activates) the users account from a variable that is sent to them in their registration email.

the url will look like

activate.php?confirm=h47H35gGdh7G6dh3j

and I will $_GET["confirm"] and check it against the database, but I don't kno开发者_如何学Gow how to catch all of the errors that I could encounter for example:

  • User already activated
  • activation code wrong
  • no activation code at all etc

How can I do this?


To detect no activation code:

if (!isset($_GET['confirm']) || empty($_GET['confirm'])) {
    die('Error: an activation code is required.');
}

To detect user already activated, do a database lookup for User ID to activate ($uid), then check in database to see if user is already activated. If you want to avoid the additional lookup and you don't mind running an extra update (and you're not updating something like activation time), you can run the update, then check how many rows were affected ($stmt->rowCount() in PDO). If that is zero but there's no database error, just the user was already activated and you can throw the error.

Activation code is wrong will be if you get an empty set (zero rows) back from the database when looking up the confirm code.


I have managed to answer it myself.

Basically I pulled the value from the $_GET array and used that value to see if it exists within the database, if it did, then I used another if statement to see whether that user's status was already set to 'active', if so, it would not do anything but if the account wasn't active then it would set it to active and confirm this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜