简体   繁体   English

PHP,MySQL使用电子邮件验证和登录系统(历史记录)进行用户注册,插入日期为最近一次

[英]PHP, MySQL User registration with email verification and login system (history) with insert date of last seen

I wish to create a log in system where every time a user logs in, this activity is logged in another table. 我希望创建一个登录系统,在该系统中,每次用户登录时,此活动都会记录在另一个表中。 How is this possible? 这怎么可能?

I am using PHP and MySQL, I have an ok knowledge of the two and currently have a system that I have designed using various tutorials. 我正在使用PHP和MySQL,我对这两个都有一定的了解,并且目前拥有一个使用各种教程设计的系统。

It is very simple as that is all I require at the moment however I may look to streamline the code at a later date by making it object oriented however this is above my skill level at this time. 这很简单,因为这是我目前所需要的,但是以后我可能会希望通过使其面向对象来简化代码,但这超出了我的技能水平。

The user registers with their name, username, password and email. 用户使用其名称,用户名,密码和电子邮件进行注册。 The user is then emailed their log in details - what I would like is for the user to have to click a link in within the email lot activate their account.*1 然后,通过电子邮件向用户发送其登录详细信息-我想要用户必须单击电子邮件批中的链接来激活其帐户。* 1

Once the user has activated their account the user logs in with a username and password - I would like their 'activity' to be stored, ie the date/time that they log in.*2 用户激活帐户后,用户将使用用户名和密码登录-我希望存储他们的“活动”,即他们登录的日期/时间。* 2

*1: Currently, when the user registers, they receive an email with their log in details, I would like the email to contain a link so that they can 'activate' their account. * 1:目前,当用户注册时,他们会收到一封包含登录详细信息的电子邮件,我希望该电子邮件包含一个链接,以便他们可以“激活”其帐户。 How can I implement this. 我该如何实施。 I know that I should store a unique code in the database but I do not know how to send that information to the user and to check it against the database. 我知道我应该在数据库中存储一个唯一的代码,但是我不知道如何将该信息发送给用户并针对数据库进行检查。

*2: I would like the date and time stored in a table when the user logs in, so that their 'activity history' can be checked. * 2:我希望用户登录时将日期和时间存储在表格中,以便可以检查其“活动历史记录”。 How do I go about this. 我该怎么做。 I know that I need to create a new table called something like 'log' which merely stores the username and date, but I do not know how to populate it when the user logs in. 我知道我需要创建一个名为“ log”之类的新表,该表仅存储用户名和日期,但是当用户登录时我不知道如何填充它。

If anyone can help that's be more than appreciated. 如果有人可以提供帮助,那就太好了。

here i have explained earlier how to create a login system. 在这里,我已经解释了如何创建登录系统。 check this post. 检查这篇文章。 this might help you. 这可能对您有帮助。

Open-source rich HTML editor 丰富的开源HTML编辑器

and regarding sending mails for activation. 以及有关发送邮件进行激活的信息。 you need to append the following to the above explained code. 您需要将以下内容附加到上述代码中。

add an extra column activation_string to the table users. 向表用户添加额外的一列activation_string now your final table should look like. 现在您的决赛桌应该看起来像。

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `activation_string` varchar(50) NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
);

The activation_string column will hold our activation string that is to be sent to the user's email. activation_string列将保存我们的激活字符串,该激活字符串将发送到用户的电子邮件。 so that when user click on it, users account get activated. 这样,当用户单击它时,将激活用户帐户。 now to generate an activation string use the below function. 现在要生成激活字符串,请使用以下函数。

function generateActivationString() {
    $randomSalt = '*&(*(JHjhkjnkjn9898';
    $uniqId = uniqid(mt_rand(), true);
    return md5($randomSalt.$uniqId);
}

Now represent the user with the registration form. 现在用注册表格代表用户。 and while submitting the form you have to do the following. 在提交表单时,您必须执行以下操作。

$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string(sha1($_POST['password']));
$activationString = generateActivationString();
mysql_query("INSERT INTO users (email,password, activation_string) VALUES('$name','$password','$activationString')");

and at the same time send an email to the user with the activation string represented as a URI. 并同时向用户发送一封电子邮件,其中包含以URI表示的激活字符串。 check the below code on how to do it. 检查以下代码,了解操作方法。

$email = $_POST['email'];
$message = 'http://mysite.com/activate.php?confirm='.$activationString;
$to = $email;
$subject = 'User Account Confirmation';
$body =     $message . PHP_EOL;
mail($to, $subject, $body);

and then write another script to verify the string and update the value in status to 1 if the activation string is verified. 然后编写另一个脚本来验证字符串,如果验证了激活字符串,则将状态值更新为1。

hope this helps you in understanding what you should be doing. 希望这可以帮助您了解应该做什么。

If you want to log the user activity (ie their last login date), you can just make another field in the users table and name it something like last_login or last_activity (whichever suits you). 如果要记录用户活动(即,他们的上次登录日期),则可以在users表中创建另一个字段,并将其命名为last_loginlast_activity (适合您的名称)。 And then when they're logging in, after checking the password they entered, update the field with the current date. 然后,当他们登录时,在检查了输入的密码后,用当前日期更新该字段。

if ($password === $storedpassword)
{

  // Password correct.
  $currentDate = date('Y-m-d g:i:s a');
  mysql_query('UPDATE `users` SET `last_login` = "' . $currentDate . '"');

}

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

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