PHP-CPP : How to stop current request without using exit(0)

141 Views Asked by At

I am creating a simple PHP module with PHP-CPP library.

I make this to limit process per IP address per php script file. I have found a solution for everything I needed, except 1 thing : I cant stop the script cleanly.

If you look in the script it have exit(0); .. If I use for example Php::echo(); for writing some headers and HTML, that result doesn't get returned to the web browser (nor telnet).

Do you know a method to echo some headers and body then kill the script without executing the final script ?

Thank you very much !

#include <phpcpp.h>
#include <iostream>
#include <fstream>
#include <stdio.h>  


extern "C" {


    PHPCPP_EXPORT void *get_module()     {


        static Php::Extension extension("dosguard", "0.9");


        extension.onShutdown([]() {
            system("rm -r /dev/shm/dosguard-*");
        });

        extension.onRequest([]() {

            std::string inode = Php::call("fileinode", Php::SERVER["SCRIPT_FILENAME"]);
            std::string ip = Php::SERVER["REMOTE_ADDR"];
            int time = Php::call("time");

            std::string shm = "/dev/shm/dosguard-"+inode+"@"+ip+".time";

            int saved = Php::call("file_get_contents", shm);

            if(saved >= time)   {

                //////////////////////////////////////////
                exit(0);
                //////////////////////////////////////////

            }   else    {

                if(saved == NULL)   {
                    saved = time;
                }
                else    {
                    saved = time + 1;
                }

                Php::call("file_put_contents", shm, saved);

            }

        });


        return extension;
    }
}
0

There are 0 best solutions below