仅使用令牌将直接url重定向到文件

问题描述

我正在开发一个项目并访问文件用户必须拥有一个帐户,然后,他可以访问该文件并在付款时下载该文件

例如,我有一个这样的文件here

您知道,用户可以在浏览器中复制地址,只需获取文件并下载即可。 但我希望网址是这样的:here

如果没有令牌,他将无法访问文件download。 如何在PHP中做到这一点?我正在为此项目使用Laravel。

谢谢。

解决方法

you can do it like this....
in blade 

    <a href="{{route('your route here',\Illuminate\Support\Str::random(6))}}">Download</a>
in web.php

Route::get('your_ul/{token}','HomeController@download')->name('your_route');
in controller

public function download($token)
    {
        //your operation here
    }

如果您想更安全,请先将令牌保存在数据库中,然后当用户单击按钮时,弹出一个模态并询问图钉,然后在控制器中这样做 >

public function download(Request $request)
    {
        //your operation here
        $token = Download::first();
        
        if ($token == $request->token){
            ///do you download here
        }
        else
            return abort(403);
    }