C++ Serve PHP documents? - php

I am writing a small web server, nothing fancy, I basically just want to be able to show some files. I would like to use PHP though, and im wondering if just putting the php code inside of the html will be fine, or if I need to actually use some type of PHP library?
http://www.adp-gmbh.ch/win/misc/webserver.html
I just downloaded that and I am going to use that to work off of. Basically I am writing a serverside game plugin that will allow game server owners to access a web control panel for their server. Some features would be possible with PHP so this is my goal. Any help would be appreciated, thanks!

The PHP won't serve itself. What happens in a web server like Apache is before the PHP is served to the user it is passed through a PHP parser. That PHP parser reads, understands and executes anything between (or even ) tags depending on configuration. The resultant output, usually still HTML, is served by the web server.
There are a number of ways to achieve this. Modules to process PHP have been written by Apache but you do not have to use these. PHP.exe on windows, installed from windows.php.net, will do this for you. Given a PHP file as an argument it will parse the PHP and spit the result back out on the standard output.
So, one option for you is to start PHP.exe from within your web server with a re-directed standard output to your program, and serve the result.
How to create a child process with re-directed IO: http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx however, you won't be writing the child process, that'll be PHP.exe
Caveat: I am not sure from a security / in production use perspective if this is the most secure approach, but it would work.

PHP needs to be processed by the PHP runtime. I'm assuming the case you're talking about is that you have a C++ server answering HTTP queries, and you want to write PHP code out with the HTML when you respond to clients.
I'm not aware of any general-purpose PHP library. The most straightforward solution is probably to use PHP as a CGI program.
Here's a link that might be useful for that: http://osdir.com/ml/php-general/2009-06/msg00473.html
This method is nice because you don't need to write the HTML+PHP out to a file first; you can stream it to PHP.

You need execute the PHP page to serve the page it generates.
The easiest thing for you to do would be to add CGI support to your webserver in some basic form. This is non-trivial, but not too difficult. Basically you need to pass PHP an environment and input, and retrieve the output.
Once you have CGI support you can just use any executable, including PHP, to generate webpages.

Related

Why do I need a server for practicing PHP?

I took a PHP course on a site called Codecademy.com, and I later learned that you needed a server to practice PHP independently. I don't want to host a server, because all I want to do is practice PHP. Why is this so?
Your need for a "server" is really just a need for some kind of PHP runtime. Since version 5.4, PHP includes an integrated webserver for you to do just this kind of thing, so as long as you install a recent version of PHP you have everything you need.
What You need to understand is that PHP is server-side.
That means if You open a php file without a server,
you will see the code, and not the result of php,
because PHP must be processed by PHP parser.
That's why You have to use a server, but You can open one locally, using a program such as:
XAMPP

Setting up a Server-side interpreter

OK. So, let's take PHP as an example.
I'm entering a URL in the browser, requesting a php script. (e.g. http://www.somedomain.com/index.php)
The browser sends that request to the appropriate server.
The server recognizes - by its extension (.php) - that's it's a PHP
file.
The PHP interpreter processes the file and outputs the results.
The server sends back the output.
Now, let's say we are dealing with a... home-made interpreter XYZ (with its binary,etc), dealing e.g. with .xyz files.
How should I go about that, so all the above things are valid for my XYZ interpreter?
How should I set this whole thing up, so that it works properly?
Hint : I'm not requesting help on how to write an interpreter, but on how to make it function the above way...
Your options are to either use an existing, standard interface and write your software to conform to it, or to write a new module for the server.
For the former, CGI is simple but inefficient while FastCGI is more modern but relatively complicated.
If you want to write a new module, then the specifics will depend on the server you are dealing with.
This totally depends on what web server you are using. However, if that is Apache, you should look into apxs for building and installing your own apache modules. Google will help you find online tutorials on apxs.
For adding a handler for a specific file ending, you should do something like this in your apache configuration (after your iterpreter/apache module is built):
AddHandler my_handler .end

Using php as a desktop application [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP as a Desktop Programming Language
I have developed a sales application with php (codeigniter framework). i am using xampp to run this application in local PC's browser. now i want 2 things:
is there any way to run this application as a desktop application? something like an icon will open the app and run without any browser. also without xampp to be set up.
also i want to protect the code from unauthorized using. someone can easily copy the code and run it on other computer. i want to prevent this.
please help me about these issues. thanks in advance.
PHP is not really suitable for either of these things. You're going to need a browser either way, but you could if you really wanted to use some kind of custom browser (e.g., you can use Java or .NET to create a window that has a basic browser page with only whatever controls you want to add on it rather than a full browser).
You won't be able to prevent people copying it, but you could try googling for a PHP obfuscator to make the code hard to read and you could add whatever checks you may wish to determine the PC is one you've approved (perhaps some kind of license file and you could activate it against a MAC address or something).
You can use PHP/GTK+ to create a PHP Desktop Application, but the code protection is very difficult, you can try ofuscating the code, or you can use a php compiler like this: http://www.phpcompiler.org/
I would not use php to develop a desktop application as you like, try using another language, dont use scripting, use compiled codes.
Luck with you project.
PHP is a recursive acronym which stands for "PHP: Hypertext Preprocessor." At its core, PHP is designed to process information and output it as HTML ("Hypertext"). If you wish to output information primarily as something other than HTML, PHP is probably the wrong language.
PHP is also a scripting language. That means that it is not compiled (converted from source code to machine code). As such, the scripts are human-readable. Tools such as phc exist, but if you want a compiled application, PHP is probably the wrong language.
Can you use PHP as a desktop application? Yes. Can you compile PHP? Yes. Should you? Probably not, because you are circumventing the purpose and features of the language.
If you are interested in writing client-side applications with PHP, you have to use PHP-GTK. I don't know how flexible is that. In any case, you should try it to see what it can do for you ;)

How to access variable values of a php script from a python script?

How can I access my php script from a Python script?
I need my Python script to be able to access the variables within the php script. (By the way, I'm new to php and Python.)
Thanks in advance.
If I understand it correctly, you have a service in PHP, and want to communicate with another one in Python.
Now, this is not really related to PHP or Python: this is quite a classic issue of integration and there are several ways to accomplish it; without more details about your problem, it may be very difficult to be specific about a solution and what kind of approach could be the better for you, but below you can find some ideas.
You could for instance save the status from PHP service in an ad-hoc table in the database, and then query it from the Python service.
Another way could be to use a RESTful approach: the information is available as a resource, accessible via a GET query; in PHP you would have a small handler that would just return a small JSON (or XML, if you like that kind of stuff), and in Python you would have instead the client. Of course, there are security issues to consider, but I think you got the idea.
For more information, I recommend you having a look at an interesting series written some time ago by Paul Stovell about integration. It is very accessible, and shows several approaches - although not all of them apply to your current issue.
Elaborate. Is the PHP file local? On a webserver? Where's the python file?
If the php file is on a server with the python file, use an exec statement.
If the python file is local and the php file is on a server, then you need to use urllib.
If both are local, write an interpreter...

Best design for PHP/ASP application that makes calls to remote servers

We run multiple Windows/IIS/.Net sites (up to 30+ sites per server). Each site is customized for the individual customer via a configuration file that contains the settings.
I am tasked with writing a small tool that will 'grep' all of the config files on a certain server for a particular config setting (or settings) and return the values for a nice tabled web page display. It will save many groups lots of time, especially since most groups don't have access to production servers, but they need to know how a customer is currently configured.
I have working code that finds all .config files from a starting path, I can easily extend this to do my grep'ing. Here are the challenges:
I want to aggregate this data from MULTIPLE servers. That means, the tool will be hosted on its own server -- and will make calls to a list of servers.
I'm limited to using .NET/ASP on the actual servers (they won't install PHP on IIS), but I'm writing the tool in PHP.
PROPOSED DESIGN: From my vantage point, I'm thinking the best way to accomplish this is to write my PHP tool and have it make AJAX or CURL requests to ASP scripts that live on each server in the list. Each ASP script could do the recursive directory parsing to find the config files and individually grep the files for the data, and return it in the RESPONSE.
Is that the best way to accomplish this? Should the ASP or PHP side do the 'heavy lifting'? Is their a recommended data format I should be using to pass the data.
Any ideas or samples would be great. If you need more info, I can provide!
Thanks!
Update: Here's an example of a config. Its a basic ASP file that gets included in other scripts.
custConfig1 = " 8,9,6:5:5 "
custConfig2 = " On "
I think you're bang on using PHP for the "receiving" script, and pretty sure you have that in hand.
Based on the format of your example config file, you could use ExecuteGlobal in classic ASP to load each file as you loop through them in your recursive directory lookup. Then you can use the custConfig1 et al. names in your script. e.g. (pseudo)
for each file
output("custConfig1") = custConfig1
next
Return what you need as JSON using a handy library and then do all the "hard" work of collating it and outputting it in PHP.
Yes, "grep" (if by that you mean importing a text file and using reg expressions to navigate it) isn't the best solution, in my humble opinion, use either JSON or XML as the format, and use PHP's built in XML or JSON tools.
JSON: http://php.net/manual/en/book.json.php
XML: http://php.net/manual/en/book.simplexml.php
You could use the DOM to navigate XML alternatively to SimpleXML, but SimpleXML is easier to learn (again, in my opinion) and will work for your needs.

Categories