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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制