I implemented an age verification system where the user can only view the website pages if they check the "I am 21" checkbox on the开发者_开发技巧 home page.
They way I did it was to set a PHP session variable when they check the box. Then for all the php webpages my code checks to make sure the "I am 21" session variable is set.
So this works for any php webpages I want to block. But the drawbacks are that I have to add the age block php snippet to the top of each php file and it can't work for any non php files.
So my question is, is there anyway I can use the "I am 21" session variable to simply block/allow access to an entire directory? So that I don't have to put the age block in each PHP file and so that I can block something that isn't a PHP file, like for example a PDF. For example, is it possible to access PHP session variables in .htaccess?
You can set it in a cookie, and then add a few httpd.conf and/or .htaccess rules to check for the existence of that cookie. Something like
RewriteCond %{HTTP_COOKIE} !/IsOldEnough=1/ [NC]
RewriteRule ^.* http://www.disney.com
which would (If I got the syntax right) take anyone whose IsOldEnoughCookie isn't set and/or not equal to 1 and redirect them to the Disney site.
Have PHP wrap the pdf. Make a script that first checks if the age variable is in the session and then if so, loads the pdf, mimetypes the header and outputs it to the browser. If the session value is absent, send a location header to redirect.
define('PDF_PATH', '../pdf/path');
if(isset($_SESSION['age_enough']) && $_SESSION['age_enough']==TRUE){
$contents = file_get_contents(PDF_PATH . 'naughty.pdf');
header('Content-type: application/pdf');
echo $contents;
}else{
header('Location:http://www.yourdomain.com/tooyoung.php');
}
You may also want to set the content disposition header if you want to prevent certain plugins for PDF reading from activating. Depending on your webserver config, you may need to add the mimetype to your .htaccess file as well.
This method can be used with numerous filetyes.
精彩评论