Skip to content

Instantly share code, notes, and snippets.

@stalmok
Created December 5, 2012 15:03
Show Gist options
  • Select an option

  • Save stalmok/4216203 to your computer and use it in GitHub Desktop.

Select an option

Save stalmok/4216203 to your computer and use it in GitHub Desktop.

Revisions

  1. stalmok created this gist Dec 5, 2012.
    38 changes: 38 additions & 0 deletions restrict-access
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    /**
    * Original post: http://www.thebigblob.com/how-to-restrict-user-access-to-content-in-folders-using-php-and-apache-htaccess-files/
    */

    # The .htaccess file
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_URI} ^\/(path\/to\/some\/folder|dummy)\/.*$
    RewriteRule !^((.*.php)|(.*\/))$ authorize.php

    # The PHP file (authorize.php)
    <?php
    // Perform authentication and authorization here
    if($hasAccessToFolder)
    {
    // Get the file
    if(file_exists($_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI']))
    {
    // Open the file for reading
    $fp = fopen($_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'], 'r');

    // Set mime type to header
    header('Content-type: '.mime_content_type($_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI']));

    // Send the contents of the file the browser
    fpassthru($fp);
    fclose($fp);
    }
    else
    {
    // File not found
    die('File not found');
    }
    }
    else
    {
    die('Access denined');
    }