dongtou9934
2019-01-25 23:38
浏览 112
已采纳

使用PHP启用/禁用htaccess?

Is there a way to disable htaccess to website users? What I need is htaccess to disable only to a select group of my website users to access a directory full of uploaded images and to the non-selected user have htaccess enabled to stop them from accessing the directory!

Any help would be great!

图片转代码服务由CSDN问答提供 功能建议

有没有办法禁用网站用户的htaccess? 我需要的是htaccess只禁用我的网站用户的一组选择来访问一个完整上传图像的目录,而非选择用户启用了htaccess来阻止他们访问目录!

任何帮助都会很棒!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxue4395 2019-01-26 00:09
    已采纳

    You'll want to do this: Basically redirect all requests to a php file which then does the access control.

    .htaccess

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /CheckAuthorizedToFile.php?file=$1 [NC,L,QSA]
    

    CheckAuthorizedToFile.php

    <?php
    
    
    $basepath = '/path/to/images/';
    $realBase = realpath($basepath);
    
    $userpath = $basepath . $_GET['file'];
    $realUserPath = realpath($userpath);
    
    if ($realUserPath === false || strpos($realUserPath, $realBase) !== 0) {
    //prevent directory traversal by exiting execution
    exit();
    } 
    
    if($_SESSSION['IsAllowedToViewFiles']===true)
    {
    
    
    $file = $_GET['file'];
    $type = 'image/jpeg';
    header('Content-Type:'.$type);
    header('Content-Length: ' . filesize($file));
    readfile($file);
    }
    else
    {
    echo "Not Autorized please login.";
    }
    
    
    已采纳该答案
    打赏 评论
  • douyakao5308 2019-01-25 23:47

    htaccess is managed outside of php (e.g. by apache environment) and is scanned and executed before the php script starts. So the answer is NO.

    This is related to "Enable/Disable htaccess with PHP?" Apache itself can manage access control by users. But this is again outside of PHP control.

    Possible PHP solution: Disable access of a directory by apache for all, and deliver content/files by php after user verification.

    打赏 评论

相关推荐 更多相似问题