简体   繁体   中英

How to run php function after redirecting user to another page?

I am building a form which the user fills and then I save it into the db. Then I retrieve users who match the criteria and for each one of them I store into another table and also send them an email.

   $userModel = new User();
   $currentUser = $userModel->findUserById($user->id);
   $requestModel = new Requests();
   $success = $requestModel->saveRequest($currentUser->usr_id, $tagId, $title, $task, $fixed, $price, $hour, $quality, $multiple, $datetime, $postal, $cityId, $travel);
                if($success){
                    $request = $requestModel->getUserLatestRequest($currentUser->usr_id);
                    if($request){
                        $user = new User();
                        $alluserids= $user->getAllSkillCityUserIds($cityId, $tagId);
                        $targetId = (array_column($alluserids, 'usr_id'));
                        //error_log("<pre>targetId".print_r($targetId,true)."</pre>"); 
                        foreach($targetId as $target) {
                            if($target == $currentUser->usr_id){
                                continue;
                            }
                            $lead = new RequestsLead();
                            $lead->addRequest($request->req_id, $request->req_userid, $target);

                            $contractor = $userModel->findUserbyId($target);
                            $nemail = new NotificationsEmail();
                            $nemail->sendGotRequest($contractor->usr_email, $contractor->usr_firstname);
                        }
                    }
                    $this->flash->success('<div data-toggle="notify" data-onload data-message="Thanks for using our service!." data-options="{"status":"success"}" class="hidden-xs"></div>');
                    $this->response->redirect($this->url->get(""));
                }else{
                    $this->flash->error('<div data-toggle="notify" data-onload data-message="Sorry! Please try again." data-options="{"status":"danger"}" class="hidden-xs"></div>');
                    $this->response->redirect($this->url->get("request"));
                }   

The problem comes when there are alot of users and this function will need to finish running before the user is redirected back to the page with the flash message. How can I modify this so I redirect the user back to the page with the flash message first then run the php foreach functions storing into the db and sending emails after?

I tried switching the order of the functions but once the user is redirected with flash message the php functions stopped proceeding.

PHP is a server side scripting language . It's essentially (but not completely) stateless.

What this means is that when a PHP Page is loaded, it executes it's required PHP code on the server, and then sends the response to the browser. There is no way to re-xecute PHP code after the page has been sent to the client without a new call to the server .

In your case, you're redirceting clients to a new PHP page. That's great, but the new PHP page is a new script being run on the server, it has no concept of what the previous page was doing.

In order to execute the PHP Code AFTER the page has loaded, you will need to use AJAX to send out a request to the PHP Server to execute this request in the background if you want to do it without redirecting the user again, or redirect the user again after displaying the 'flash' message.

Note that with AJAX you can also use the original page - without any redirection - to execute this request AND to display the flash message (at the same time!).

For more information about sharing data between pages:

For more information about AJAX requests and PHP:

What you mention would require some way for the script to keep running beyond the response being sent back to the user. PHP does not work this way - you can start writing content to the output buffer, but the browser will still wait until the entire response has been returned.

Instead, you should think of some process to temporarily store the information you need, and process these asynchronously. eg store them to a database and run a cron script or deamon to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM