PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 20, 2022

[FIXED] How to let PHP run some script after the connection end

 April 20, 2022     ajax, connection, php     No comments   

Issue

This is my system's flow:

                Form
User Input  ───────────►    Encrypt Library Scripts
                                           │
                                           │
Result     ◄─────────   Add DB    ◄────────┤

However, The script file is encrypted and something is missing. Also I want to add the log about this function.

So I am writing some code like this:

Frontend

$("#form").submit(function(e) {
     e.preventDefault();
     
     // Call API Here

     this.submit();
});

// Step 1: API to Save the User Input and timestamp to the log table

// Step 2: Cron job to Query the latest result of that user, and update the table id to the Step 1 log 


But it seems too complicated and need to manage the cron job.

So I want to write something like this

API

$data = $_POST['inputA']; 
...
..

// Step 1: Return the response to frontend first (Finish AJAX)
// Step 2: Wait 10s for the library adding result
// Step 3: Query the latest id, and add with the $_POST data from frontend

// Will Handle if the latest result not in 10s, assume add data failed , no need to retry

Is it possible in php ?


Solution

You can flushe all response data to the client

fastcgi_finish_request();
sleep(10);
// do your task 

Or better way is to use queques https://laravel.com/docs/8.x/queues



Answered By - Vlad Salabun
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing