drxv39706 2018-05-03 17:21
浏览 78
已采纳

仅允许已登录的用户查看托管在同一服务器和域上的外部html站点(在子文件夹中)

I have a PHP site (site A, CakePHP 2.3) with its own login system. Then I have another "site" (it's actually an html generated ebook with its own index.html) in the same server, but on a different folder, let's call it site B.

I'm trying to allow only users that have a valid session on site A (have logged in with valid credentials) to view that ebook (access that index.html file). The main idea behind this is to prevent users from directly sharing site B's URL.

This would be easy if I could check the user's session on Site A from Site B, I could just check the $_SESSION variable, but that's not possible.

What's the simplest way to accomplish this? While not preferably, it's okay if I have to edit that index.html file from site B to add any code that could help with this.

One way I thought about was to do some javascript redirect from site A to site B that includes a POST variable, if the variable doesn't exist, then nothing is shown. This would require adding some php on that index.html on site B but I'm not sure it's the best solution, I wonder if there's something better.

Also, I have 100s of these ebooks so if it's something I can apply massively it would be much better.

EDIT:

For clarification, both sites are in the same server and have same "domain". To open site B I use a symlink from site A. For example:

  • 写回答

1条回答 默认 最新

  • douxing5199 2018-05-03 18:49
    关注

    Create a proxy

    I would use .htaccess to redirect any url pointing to pages in the book to a custom action in the CakePHP application.

    This action checks for credentials and if OK then reads from disk the actual requested file and sends it to the browser. Do not redirect back or you will cause a redirect loop!

    Of course you need to create a redirect that passes the original requested page as a parameter so you know what file to read.

    Granted this is not supper efficient but it works. I had to solve the exact same issue in an old project.

    Notes

    Make sure your .htaccess rules only intercept/redirect HTML links or else you need to pay attention to setting up proper response headers for CSS or Image files.

    Example of .htaccess

    This needs to be in the ROOT folder of the book

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule    ^(.*html)$    http://[FULL_LINK_TO_CAKE_APP]/proxy/load/$1
    </IfModule>
    

    Example of the proxy controller

    namespace App\Controller;
    
    /**
     * Static content controller
     *
     * This controller will render a html file 
     *
     */
    class ProxyController extends AppController
    {
    
        public function load($file=null){
            if( !$file ){
                return $this->response->body( "Error: no file specified" );
            }
    
            //THIS NEEDS TO RESOLVE THE FULL DISK PATH OF YOUR PROTECTED FILES
            $pathToFiles = WWW_ROOT . '/subfolder/';
    
            if( file_exists( $pathToFiles . $file )){
                $this->response->body( file_get_contents( $pathToFiles.$file) );
                return $this->response;
            }
    
            $this->response->body('Could not load the file: ' . $pathToFiles . $file);
            return $this->response;
        }
    }
    

    Security

    Of course I assume you have setup the Auth component correctly in your AppController so the controller above will only execute if the user is logged in!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里