doujia2386 2016-10-26 14:03
浏览 24
已采纳

如何在过期时间后将字符串设置为零?

I am creating a login/registration-script where a user hast to activate his account with a token. Now i have a function that calls a random string.

class.functions.php

public static function generateRandomString($length = 200) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }

this will output something like this: jTq7tH4dKy66dmnhRev5EjbZDZygN1...

now my question is, how can i set this string empty after 24h, so that the user has to activate his account again.

my thought was about an update query in my database like this:

$statusN = "N" and $statusY = "Y"

the table field from the database:

`user_status` ENUM('Y','N') NOT NULL DEFAULT 'N',

the sql statment:

$sql = "UPDATE formular SET user_status='" . $statusY . "' WHERE user_status='" . $statusN . "'";

but i dont know how to handel it with the time

Here is the inserting of the userdata and the activation_token

register.php

$activationToken = functions::generateRandomString();
    $stmt = $conn->prepare("INSERT INTO formular (firstname, second_firstname, lastname, zipcode, city, street, additionaladdress, country_id_from_apps_countries, username, email, hash, dday, dmonth, dyear, religion, housenumber, activation_token) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("sssisssssssssssss", $firstname, $secondFirstname, $lastname, $zipcode, $city, $street, $additionaladdress, $country, $username, $email, $hash, $DOB, $MOB, $YOB, $religion, $houseNumber, $activationToken);
    $stmt->execute();
    if ($stmt->affected_rows) {

    }
  • 写回答

1条回答 默认 最新

  • duai4379 2016-10-26 14:07
    关注

    Checking the timestamp and creating the string are two different things.

    On one side, you have to create the string and save it with the actual timestamp.

    On a second step, probably with a cronJob you have to check wich tokens in your DB are expired (more than 24 hours old) and set them to null.

    You have two different operations in two different pieces of code.

    To disable all expired entries, just do a SELECT * from .... where token >= XXX. Foreach result, UPDATE table set token = NULL where ....

    and so on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?