php – 在设定的到期时间后删除数据库行(例如5分钟)
背景:我正在设计一个在线虚拟教室管理系统……当教师创建教室并将其存储在数据库中时,它可以生成随机会话密钥(md5(时间)).
为了访问教室,学生访问唯一的教室URL,代码将URL中的会话密钥(使用GET)与数据库中的会话密钥进行比较.如果匹配则教室显示. 网址通常如下所示:/classroom.php?Instance = a529501db8373609f2d47a7843a461ea 需要编码帮助:我希望我的老师也能够设置会话“长度”,因此教室可以访问15分钟,25分钟或50分钟. 当创建教室以来的时间超过例如25分钟时,会话密钥将从数据库中删除,并且无法再访问教室. 到目前为止我所拥有的: 当教师单击按钮创建一个classroo时,下面的PHP将会话密钥($instance)和会话长度($duration)存储在数据库中…… <?php session_start(); if (isset($_SESSION['id'])) { if (isset($_POST['hidden'])) { // Connects to the database include_once("$_SERVER[DOCUMENT_ROOT]/classes/includes/dbconnect.php"); mysql_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME); // Sets the session duration $currentTime = time(); $duration = $currentTime + $_POST['duration']; // Session variables $uid = $_SESSION['id']; $usname = $_SESSION['username']; // To generate the random hash from the current time $time = time(); // time to hash $instance = md5($time); // hash stored in variable // Stores the session hash (instance) and duration in the instance database $query = ("INSERT INTO `my-db-name-foo`.`instances` (`instance`,`expiry`) VALUES (`$instance`,$duration`);"); mysql_query($query) or die(mysql_error()); // Closes the database connection mysql_close(); // Redirects the teacher header('Location:classroom.php?instance='.$instance); } } else { echo 'Please login'; die(); } ?> 然后在实际的classroom.php页面上,一段代码检查会话是否已过期……如果是,则将其从数据库中删除. <?php $currentTime = time(); if ($currentTime > $duration){ // Connect to database and delete the row } else { // show the classroom } ?> 任何帮助或建议将不胜感激! 更新—- 感谢所有伟大的答案,这就是目前这一切的工作原理…… 在createclassroom.php页面上,我存储实例以及NOW()日期和时间以及持续时间为NOW()$_POST [‘duration’]; … <?php session_start(); if (isset($_SESSION['id'])) { if (isset($_POST['duration']) && !EMPTY($_POST['duration'])) { // Connects to the database include_once("$_SERVER[DOCUMENT_ROOT]/classes/includes/dbconnect.php"); // Session variables $uid = $_SESSION['id']; $usname = $_SESSION['username']; // To generate the random hash from the current time $time = time(); // time to hash $instance = md5($time); // hash stored in variable // $duration = $_POST['duration']; $duration = $_POST['duration']; // Stores the hash (instance) in the instance database $sql = ("INSERT INTO `xxx_xxxx`.`instances` (`id`,`teacher`,`instance`,`startdate`,`expiredate`) VALUES ('$uid','$usname','$instance',NOW(),NOW() + $duration);"); $query = mysqli_query($dbConnect,$sql)or die(mysql_error()); // Redirects the teacher header('Location:classroom.php?instance='.$instance); } else if (isset($_POST['hidden'])) { echo 'Please select a duration'; } } else { echo 'Please login'; die(); } ?> 在实际的classroom.php页面上,我只检查未过期的会话实例. <?php session_start(); // Connects to the database include_once("$_SERVER[DOCUMENT_ROOT]/classes/includes/dbconnect.php"); $instance = $_GET['instance']; // GETs instance from URL $usname = $_SESSION['username']; // Gets teacher name // script to retrieve all the Instances in the database and store them in a variable '$dbInstance' $sql = "SELECT instance,expiredate FROM instances WHERE instance = '$instance' AND instances.expiredate > NOW()"; $query = mysqli_query($dbConnect,$sql); $row = mysqli_fetch_row($query); $dbInstance = $row[0]; if ($dbInstance == $instance){ echo $dbInstance.'<br>'; echo $instance; } else { echo $dbInstance.'<br>'; echo $instance.'<br>'; die('Instance not initiated'); } ?> 现在我只需要决定如何经常清理数据库.真的,我想感谢你们的帮助,正是我需要的! 解决方法
这种行到期通常按如下方式处理:
>将到期日期DATETIME放在每一行. INSERT INTO tbl(id,expiration)VALUES(无论如何,NOW()INTERVAL 5 MINUTE)>查找行时,请使用AND tbl.expiration>之类的内容.在查询中NOW().这将使过期的行显得不见了.>方便的时候,也许是在一夜之间的工作或每小时的EVENT,摆脱过期的行:DELETE FROM tbl WHERE tbl.expiration< = NOW() 这比在过期时尝试实际删除过期的行要容易得多且时间精确得多.它还可以抵御即时DELETE操作的失败,并且可以很好地扩展. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |