Redirect direct url to the file only with token

Viewed 405

I'm working on a project and to access the files, the user must have an account and then, he can access to the file and download it when he pay for that.

For example, I have a file like this: here

As you know, the user can copy the address in browser and simply get the file and download it. But I want the url be like this: here

And without the token, he cannot access to the file and download it. How can I do that in PHP? I'm using Laravel for this project.

Thank you.

1 Answers
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
    }

And if you want more secure then first save a token in the database and when a user clicks into the button pop up a modal and ask a pin and in the controller do it like this

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