Skip to content
Advertisement

Control repeated requests in Phalcon

I want to intercept the same request, so i write a flag in session if one request come, such like this:

in Security.php/beforeExecuteRoute

    public function isActed($actkey) {
            $log = $this->getDI()->get('log');
            $actq = $this->session->get($actkey);
            $log->debug("magic show" . $actq);

            if (!empty($actq)) {
                $log->debug("isActed can not send agin!");
                return false;
            } else {
                $log->debug("isActed clean,it can go!");
                $this->session->set($actkey,true);
                return true;
            }
        }

in ControllerBase.php/afterExecuteRoute

    public function afterExecuteRoute() {
        $controllername = strtolower($this->dispatcher->getControllerName());
        $actionanme = strtolower($this->dispatcher->getActionName());
        $cakey = $controllername . $actionanme;
        $this->session->remove($cakey);
    }

But the session of write need real time, if the request send quickly, it will not work fine.For example,at the same time, only to allow a person to enter, when a person enters,the door will close very quickly ,then people go, then open… But it may come two or more people in same time, before closing, another one already came in.

I hope someone gives some advice, or other ways to control repeated requests. Thank you!

Advertisement

Answer

There are few layers of how you can implement this. First one starts with JavaScript on view layer. You can disable button on click event.

Afer you have this done right, I recommend to implement a CSRF mechanism that Phalcon supports. Once you display form you add a generated unique token for it. When receiving form from user received token should match one you have saved in your session, and than one saved in session should be regenerated. This way same HTML cannot send data second time, because token does not match.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement