How to Convert C to PHP?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KeredDrahcir
    Contributor
    • Nov 2009
    • 426

    How to Convert C to PHP?

    I worked in C a long time ago and touched on C++ however I now use PHP. I have some free C source code for a program but I'd like to covert it to PHP.

    Is there any software that would do this or any person who would do it.

    Failing either of these, is there anyway for running the C program on the internet?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    is there anyway for running the C program on the internet?
    sure, make the web server execute the C application and return its output (in the end PHP does exactly that, it’s a C(++) application that is registered in the web server (e.g. via module or FastCGI) and its output is sent back to the server which sends it back to the client.)

    Comment

    • KeredDrahcir
      Contributor
      • Nov 2009
      • 426

      #3
      How do I make the web server run the C program and return its output?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        simple answer, by calling it from the server.

        longer answer, you register your C application in the server configuration that when a certain event happens (e.g. a HTTP request) the server calls the C app through the OS and passes it the necessary information.

        take for example PHP. PHP is an application written in C, when a HTTP request (for a file with the .php extension) occurs, the web server calls the PHP executable and passes it some data, then PHP executes based on the given data (the PHP script file mentioned in the request being one of them) and returns the result to the web server, which passes that back to the requesting User agent.

        Comment

        • KeredDrahcir
          Contributor
          • Nov 2009
          • 426

          #5
          Do I need to compile the C file because C complies to an executable file, or do I call the executable file?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Well that depends on how you want the interaction to happen.

            1.) If you want to call the C program from PHP then yes you compile and create the binary as you would do for a C application release and install it on the OS. Then you call it from your PHP using the system (http://php.net/manual/en/function.system.php) command. You have to be careful how you call the command here because if you are dependent on internet input it can be manipulated to execute harmful commands on your system so only call commands you have defined and make proper checks before calling.

            2.) You could write an Extension as explained in the tutorials starting here :http://devzone.zend.com/303/extensio...-php-and-zend/

            3.) If you don't use PHP at all and just call the C program from the webserver then you need to have the C application properly installed as required for 1.) above as well.

            Comment

            • KeredDrahcir
              Contributor
              • Nov 2009
              • 426

              #7
              I had a look at system and exec. The C program may be required to take hundreds of user inputs possibly close to 500 and output the result for each before it completes. Is there a way to execute it through HTML or JavaScript since php will wait until the program is finished before displaying it which doesn't help.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                It would be dangerous to allow Javascript direct access to your system shell. Couldn't you make AJAX requests to a PHP script that does the validation and call for each request?
                Also 500 separate requests for a user sounds like a lot. You'd certainly struggle to display all 500 results to the user at the same time unless if they are being aggregated somehow.

                Comment

                • KeredDrahcir
                  Contributor
                  • Nov 2009
                  • 426

                  #9
                  I'm afraid I know know anything about AJAX. In terms to the 500 seperate requests, it would ask them a question and then based on the result of that question, ask them another. I know that my traget audeince would be quite happy to answer all these questions. They can even save past way through and continue later.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Then you don't need AJAX and you should let PHP wait for each response from the C program because you need to use the result in your next question to the user anyway.

                    Comment

                    • KeredDrahcir
                      Contributor
                      • Nov 2009
                      • 426

                      #11
                      I thought php would only show what the program is going to display after it has finished. If the user has to answer several questions how do they know what they will be?
                      After each question, they will get different questions depending upon their answer to the previous question.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Erm, it's you who decides what you want PHP to display to the user and when so it's all up to your requirements.
                        Remember that I have no idea what your C program does or how often it should be called for each user question. You just asked for how to call C from PHP. All I can say is that it's you who decides when to let PHP call the C program and that ii up to your requirements.

                        Comment

                        • KeredDrahcir
                          Contributor
                          • Nov 2009
                          • 426

                          #13
                          All I want to do is call the C program once to display in the browser so the user can go through it until it had finished.

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Do you mean that your C program is interactive and you want the user interaction part to happen via the browser? Then you need to be able to provide input to the program and read its output streams without shutting it down. The C forum will help you with that.

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              a C programme cannot be displayed in a browser, that’s not what browsers are supposed to do. they have to display HTML and execute JavaScript.
                              if you want the user to use the C programme directly, make the user install it on his computer.

                              Comment

                              Working...