开发者

How can I block users from accessing a section of my website at a specific time of day?

Okay, so I have开发者_如何转开发 a chat feature on my website that I've mentioned in other questions. There are some kids at my school who would love to chat during school, but I'd really rather they'd just chat during study hall so I don't get in trouble for interrupting class time. How can I restrict user movement on my website during specific time periods during the day? Thanks! I don't know what type of scripting language I might need for this, but I'm assuming PHP since this is a server issue.


<?php date("H"); ?>

returns the current hour in a 24 hour format with leading zeroes.

That means, you can write something like this:

<?php

if (date("H") == 14)){
   die("You cannot get here right now");
}

?>

This will "die" echoing the page, and will return the error instead, exactly during the 14th hour of the day (2:00 pm).

Give us more code/details to get a deeper answer.

EDIT: This is an expanded answer to the new question, asking,

Study hall at my school is 2:30 to 3:15, so how would I do that?

SOLUTION: Assuming study hall is the time when the students are allowed to use the (section of) the site, you would do something like this:

<?php

$bottomValue = 60*14+30;
$upperValue = 60*15+15;
$currentValue = date("G")*60+date("i");

if (($currentValue >= $bottomValue) && ($currentValue <= $upperValue)){
   // everything is OK = put some code here, or ignore this
}
else{
   // well, this is the time they are not allowed to see this, so, let's try:
   die("Sorry, you are not allowed to be here right now.");  
}

?>

To explain how this works: your time base is counted into minutes as $bottomValue and $upperValue. Notice how they are defined, it's 60 minutes multiplied by hour (14) plus minutes (30).

So, 14:30 is 60*14+30, therefore, 16:20 would be 60*16+20.

Then the base is compared to the current time and an appropriate code is executed according to the if/else statements.


You can also control access via your web server; Apache, for example, allows time-based access controls in mod_rewrite:

RewriteEngine On
RewriteCond %{TIME_HOUR} >20 [OR]
RewriteCond %{TIME_HOUR} <07
RewriteRule ^/chat - [F] 

@RiMMER's PHP-based solution may be easier to read, write, and modify, but server-based mechanisms won't require inserting a check into every page.


Just to note, also consider your server's date/time settings as you use date() function because even you put in date and time checks on your site if the date and time of your server is not the same as your specified restricted time of the day (in your school) they may still view stuff.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜