Search code examples
php.htpasswd

Password Protect Page at certain times


General Question: Is it possible password protect a page during certain hours? If so, how can this be achieved?

Thanks in advance!


Solution

  • Using PHP is easy, just add a condition in the front of the page code like

    if ( date('H',time()) < 8 || date('H',time()) > 18 ) {
       echo "Site closed. Please visit between '8:00am~7:00pm'";
    } else {
       echo "Welcome";
    }
    

    Knowing that the time is the server time, if you have different timezone, you need to add

    date_default_timezone_set("America/Los_Angeles");
    

    For .htaccess, I found this

    <Directory /var/www/htdocs/other>
      Require expr %{TIME_HOUR} -ge 8 && %{TIME_HOUR} -le 18 
    </Directory>
    

    Haven't try with the .htaccess, so not exactly how this works.....