在特定日期登录到系统



我有以下代码。

$Start = "2020-07-27 15:15";
$End = "2020-07-28 20:20";
else {
$_SESSION['IsAdmin'] = "False";
if (time() > strtotime($Start) && time() < strtotime($End)) {
$Strona = 'Location: ../Start.php';
}
else {
$error1 = 'Logging is not activated';
$_SESSION['error'] = $error;
header('Location: ../Index.php');
}
}

添加CCD_ 1。脚本显示:

object(DateTime)#5 (3) { ["date"]=> string(26) "2020-07-27 16:23:39.222480" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Warsaw" }
object(DateTime)#6 (3) { ["date"]=> string(26) "2020-07-27 16:23:39.222485" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Warsaw" }

我希望用户只能在变量$Start和$End中指定的范围内登录。

用户选择"星期一"的日期和时间;打开";系统和日期和时间;关闭";系统

<div class="element">
<div class="input-group-prepend input-group-prepend-custom">
<span class="input-group-text" id="">Turn on logging</span>
</div>
<div class="input-group input-check">
<input type="date" class="form-control" name="StartDate" required>
</div>
<div class="input-group input-check">
<input type="time" class="form-control" name="StartTime" required>
</div>
</div>
<div class="element">
<div class="input-group-prepend input-group-prepend-custom">
<span class="input-group-text" id="">Turn off logging</span>
</div>
<div class="input-group input-check">
<input type="date" class="form-control" name="EndDate" required>
</div>
<div class="input-group input-check">
<input type="time" class="form-control" name="EndTime" required>
</div>
</div>

我用这个代码将日期添加到DB

$StartTime = $_POST["StartTime"];
$EndTime = $_POST["EndTime"];
$StartDate = $_POST["StartDate"];
$EndDate = $_POST["EndDate"];
$Start = $StartDate . " " . $StartTime;
$End = $EndDate . " " . $EndTime;
$Number = "1";   $stmt = $conn->prepare("UPDATE Config SET StartDateTime=?, EndDateTime=? WHERE ID =?");
$stmt->bind_param("ssi", $Start, $End, $Number);
$stmt->execute();
$stmt->close();  echo "Data added successfully";

您可以使用简单的if条件:

<?php
session_start();
$start = "2020-07-27 15:15";
$end   = "2020-07-28 20:20";
$now = new DateTime();
$startDateTime = new DateTime($start);
$endDateTime = new DateTime($end);
unset($_SESSION['error']);
unset($_SESSION['success']);
if ($startDateTime <= $now && $now <= $endDateTime) {
$_SESSION['success'] = 'You can login now';
header('Location: /login.php');
} else {
$_SESSION['error'] = 'Login is not possible yet';
header('Location: /login-not-possible.php');
}

最新更新