我对这段代码所做的是检查数据库中是否有结束编辑的日期(例如,今天的日期是12/30/11编辑的最后日期是12/12/10 =已锁定,或者今天的日期是12/30/11编辑的最后日期是12/12/13 =解锁并转发到编辑站点)
因此,考虑到这一点,这里有一个问题:我总是说,无论锁定日期如何,您的帐户都是锁定的,而我正迷失于解决方案:(.
顺便说一句,请记住,标头已经发送到这一点。
<?php
$id = $_GET['id'];
// Define MySQL Information.
$mysqlhost="***************"; // Host name of MySQL server.
$mysqlusername="**********"; // Username of MySQL database.
$mysqlpassword="*********"; // Password of the above MySQL username.
$mysqldatabase="*************"; // Name of database where the table resides.
// Connect to MySQL.
mysql_connect("$mysqlhost", "$mysqlusername", "$mysqlpassword")or die("Could not connect to MySQL.");
mysql_select_db("$mysqldatabase")or die("Could not connect to selected MySQL database.");
$infosql = "SELECT * FROM premiersounds_users WHERE customer_id = $id";
$inforesult = mysql_query($infosql) or die(mysql_error());
$info = mysql_fetch_array($inforesult);
$l_date=$info['lockout_date'];
//Get current date from server
$format="%m/%d/%y";
$c_date=strftime($format);
//set sessions
$_SESSION['current_date'] = $c_date;
$_SESSION['lockout_date'] = $l_date;
//Check is Current date = lockout date
if ($c_date <= $l_date) { header("location:/planner_scripts/documnet_editors /edit_weddingplanner.php?id=$id"); } else {echo 'Whoops! Were sorry your account has been locked to edits because your event is less than 48 hours from now or your event has passed. To make changes to your event please contact your DJ.'; echo'<br/>'; echo ' Todays Date: ';echo $c_date; echo ','; echo ' Last Date for edits: '; echo $l_date;}
?>
<?php
//Destroy Session for Lockout Date to prevent by passes
unset($_SESSION['lockout_date']);
?>
发布于 2011-12-31 12:54:26
有几件事。
mysql_escape_string()
调用,以防止出现这种情况,同时还提到了一个简单的整数转换。还有其他方法可以做到这一点。你可以通过搜索DateTime
类。下面的代码创建DateTime
的实例...一个用于当前日期,另一个用于从数据库中检索的锁定日期。一旦你有了这些对象,你就可以比较这两个对象了。<?php
$id = $_GET['id'];
// Define MySQL Information.
$mysqlusername=""; // Username of MySQL database.
$mysqlpassword=""; // Password of the above MySQL username.
$mysqldatabase=""; // Name of database where the table resides.
// Connect to MySQL.
mysql_connect("$mysqlhost", "$mysqlusername", "$mysqlpassword")or die("Could not connect to MySQL.");
mysql_select_db("$mysqldatabase")or die("Could not connect to selected MySQL database.");
// IMPORTANT: PREVENT SQL INJECTION
$id = mysql_escape_string($id);
// Or, if $id is supposed to be an integer just do this ...
// $id = (int) $id;
$infosql = "SELECT * FROM premiersounds_users WHERE customer_id = $id";
$inforesult = mysql_query($infosql) or die(mysql_error());
$info = mysql_fetch_array($inforesult);
//Get current date from server
$c_date = new DateTime();
$l_date = new DateTime($info['lockout_date']);
//Check is Current date = lockout date
if ($c_date->format('Y-m-d') <= $l_date->format('Y-m-d')) {
header("location:/planner_scripts/documnet_editors/edit_weddingplanner.php?id=$id");
} else {
echo 'Whoops! Were sorry your account has been locked to edits because your event is less than 48 hours from now or your event has passed. To make changes to your event please contact your DJ.';
echo'<br/>';
echo ' Todays Date: ';
echo $c_date;
echo ',';
echo ' Last Date for edits: ';
echo $l_date;
}
?>
发布于 2011-12-31 12:53:28
您正在将日期作为字符串进行比较。您正在将2011年12月30日之类的事情与2011年12月11日之类的事情进行比较。PHP可以也将会这样做,但它会像对待字符串一样对待它们。
这样做的主要奇怪之处在于,0不像数字类型那样隐含。
此外,您的日期格式将不匹配。MySQL返回类似于2011-12-30的内容,而你的strftime返回类似于30/12/2011的内容。
试试像这样的东西
$c_date_stamp = strtotime($c_date);
$today = strtotime('today');
if($c_date_stamp <= $today) { }
这将在比较之前将日期转换为unix时间戳。另一种选择是将它们保留为字符串形式,但对可能产生的影响感到厌倦。
例如,如果以字符串形式执行,则日期部分的大小需要按降序排列:
if($c_date <= date('Y-m-d'))
还要注意,如果一个人在小于10天的时间内使用前导零,另一个人也需要这样做。
https://stackoverflow.com/questions/8686324
复制相似问题