php - Laravel 代码仅在开发工具打开时才工作

标签 php mysql laravel google-chrome devtools

我正在尝试生成一个 URL,该 URL 可以将下载次数限制为输入 count_url 中输入的特定数量,并且链接/URL 应在特定时间(可能是 1 或2分钟)

对于我使用时的临时 URL (temporarySignedRoute),它工作正常。为了控制链接的点击次数,我编写了一些代码(基本上是来自不同地方的集合,因为我对 Laravel 和编程非常陌生),如果 devtools 打开并更新每次点击时的数据库。很快你就达到了最大数量,它说你已经超出了限制!很好。

但非常烦人的是,当您关闭开发工具时,它不起作用,仅在第一次更新数据库后才允许文件下载,但不更新数据库,因此不会”不要踩刹车,可以无限下载。逻辑非常简单,只有几行,如果有人能帮助我,我将非常感激!

我希望它可以在打开/不打开开发工具的情况下工作。

在刷新/后退按钮上,它不应更新 (temporarySignedRoute),而链接/url 应永远保持不变或直到有人关闭页面。

----index.blade.php----

<a href="{{ route('models.create') }}" class="btn btn-primary">Add new Models ss</a>
<br />
<table style="width:100%">                
    <tr>
        <th scope="col">Title</th>
        <th scope="col">Image</th>
        <th scope="col">No. of minutes</th>
        <th scope="col">Download file</th>
    </tr>
    @forelse($canaanmodels as $canaanmodel)
    <tr>
        <td >{{ $canaanmodel->title }}</td>
        <td > <img src= "canaanmodels/{{ $canaanmodel->img_name }}" class="img" height="80" width="100"> </td>
        <!-- <td> {{ $canaanmodel->photo ? $canaanmodel->photo->file : "image not found" }} </td> -->
        <td halign="bottom">
            <?php
            $user = Auth::user();
            $path_email = route('models.download', $canaanmodel->uuid);                                          
            ?>
            <form action='/display_links' method=post >
                @csrf

                <input name=uuid type=hidden value="{{$canaanmodel->uuid}}">
                <input name="link_to_email" value="{{$path_email}}" type="hidden">
                <input class="num_input" type=number name=count_url min=1 max=5 required>
                <input class="sub_input" type=submit value = "Download Link" class="btn btn-primary" style="float: right">
            </form>   
            <form action='/payment' method=post>
                @csrf

                <input name=uuid type=hidden value="{{$canaanmodel->uuid}}">
                <input name="link_to_email" value="{{$path_email}}" type="hidden">
                <input type=submit value = " BUY ME" class="btn btn-primary" style="float: right;">
            </form>
            <form action='/home' method=get>
                @csrf        
                <input type=submit value = " BUY ME" class="btn btn-primary" style="float: right;">
            </form>
        </td>   
    </tr>
    @empty
    <tr>
        <td colspan="2">No Models found.</td>
    </tr>
@endforelse
</table> 

----shop.blade.php----

<body>
    <button id="{{ $temp_url }}" name= "{{ $count }}" onclick="refresh_page(this.id, this.name)" target="_blank">
      Download
    </button>
     <script>
      function refresh_page(val,nam) {
          if (document.getElementById(val).style.color != "red")
            {
              location.href=(val);
              document.getElementById(val).style.color = "red";
            } else if (document.getElementById(val).style.color == "red"){
              location.href=(val);
              document.getElementById(val).style.color = "green";
      } 
    </script>

---- 路线 ----

Route::resource('/models', 'CanaanController');
Auth::routes();
Route::get('/models/{uuid}/download', 'CanaanController@download')->name('models.download')->middleware('signed');
Route::post('display_links', 'CanaanController@display_link');

---- CanaanController ----

public function index() {
        $canaanmodels = Canaanmodel::all();
        return view('canaanmodels.index', compact('canaanmodels'));
    } 
    public function create() {
        return view('canaanmodels.create');
    }
    public function store(Request $request) {
        $canaanmodel = $request->all();
        $canaanmodel['uuid'] = (string)Uuid::generate();
        if ($request->hasFile('cover')) {
            $canaanmodel['cover'] = $request->cover->getClientOriginalName();
            $request->cover->storeAs('canaanmodels', $canaanmodel['cover']);
        }
        Canaanmodel::create($canaanmodel);
        return redirect()->route('models.index');
    }
    public function download($uuid, Request $request) {
            if (! $request->hasValidSignature()) {
                echo "URL expired";
         }
        else {
            $canaanmodel = Canaanmodel::where('uuid', $uuid)->firstOrFail();
            $user = Auth::user();
            $count = $user->download_count;
            $pathToFile = storage_path('app/canaanmodels/' .$canaanmodel->cover);
            if($count != 0) {
                $count = $count - 1;
                \DB::table('users')->where('email', $user->email)->update(['download_count'=>$count]);
                return response()->download($pathToFile); 
            }
            else{
                 echo "You exceeded your limit";
            }
        }   
    }

    public function display_link(Request $request) {
        $count = Input::get('count_url');
        $user = Auth::user();
        $myFlag = $user->flag;
        \DB::table('users')->where('email', $user->email)->update(['download_count'=>$count, 'flag'=> $count]);
        $tempUrl= URL::temporarySignedRoute('models.download',now()->addMinutes(1), ['uuid' => $request->uuid]); 
        return view('shop',['temp_url' => $tempUrl, 'requesty' => $request, 'count' => $count]);
    }

我想要实现的目标是将下载次数限制为 count_url 输入中输入的特定数量。一旦完成,我将尝试从管理员/用户的角度来完成它,其中用户/客户可以购买文件,并且管理员可以允许客户/用户在特定的时间段以及特定的时间内下载该文件。下载次数。

我正在使用mysql(使用xampp)并将文件存储到storage/app/canaanmodels文件夹中并使用webpatser/laravel-uuid包 生成uuid以隐藏实际ID。剩下的就是非常简单的编码,我正在分享路由器/ Controller / View 代码,并乐意分享更多!

非常感谢!

最佳答案

1)我只需要告诉 Controller 中的下载功能不要通过设置 header 来缓存它,为此使用以下代码:

$headers =[
            'Content-Type' => 'application/text',
            'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0',
            'Cache-Control' => 'post-check=0, pre-check=0, false',
             'Pragma' => 'no-cache',                               ];
return response()->file($pathToFile, $headers);

2) 为了解决刷新/重新加载时临时 URL 重新生成的问题,我首先将该链接存储到数据库 (mysql) 中,然后从那里提供它。 感谢所有曾经或试图帮助我的人。

同样的解决方案也适用于 PhP。

干杯。

关于php - Laravel 代码仅在开发工具打开时才工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58344960/

相关文章:

javascript - angularjs 表单输入建议

php - 维护活跃用户列表并在虚拟主机上检索它

mysql - RAIL_ENV=生产 rake db :schema:load aborted with rails 3. 1.0 和 ubuntu 10.04

laravel - 在 laravel 中将数据添加到 elasticsearch 的最佳方法是什么

php - 在 PHP 中从 while 循环构建单个数组时遇到问题

php - 我如何在包含文件中包含 namespace ?

php - 通过 phpmyadmin Arduino 到 MySQL

php - 未连接mysql时更改正文

mysql - 如何在下次启动 vagrant 时保留 mysql 表

php - Docker 图像推送但我的音量不持久