I created a basic web server in Java. It can also run php code.
I execute:
php - f <file>
to get the result and send it back to the client. It works great, however when I want to use the phpinfo(); function, it returns plain text as it is written in the manual too. How can I get the result in HTML format like Apache does?
Thank you!
I'm afraid that's hardcoded in PHP's binary. It detects if it's running an Apache module or the CLI binary. Since you're not invoking the Apache module, it uses the CLI one. From the manual page:
phpinfo() outputs plain text instead of HTML when using the CLI mode.
You might want to try running the file through the php-cgi interface instead (should be an executable named php-cgi or similar).. this is one of the ways you can run php under apache.. (other choice being mod php or fcgi)
Raidence, why would you make a http server with dependencies to apache, you dont get the redundancy in that?
Related
I am calling a perl script from one php page. This perl script calls some other scripts inturn and a C binary file. The C binary uses mbstowcs function inside. This is not getting executed correctly.
But the same thing If I call from terminal(the perl script) everything works fine.
I have given proper permissions to all the files before executing(Even gave 777 permission).
Is apache uses some other terminal session/ something else to run the scripts.
I am using ubuntu 14.04 and apache webserver.
Without given any further information, I suspect you may have run across the following situation:
Use of non standard mbstowcs feature
This information is dated I realize but it's all I can offer at this time unless you can provide some more information like an error message from your Apache log file.
Sorry I could not be of more help.
How can i setup my powershell to run php scripts in like a commandcall like this
php test.php
I'm able to do this on a server at work which I connect to by putty, but would be nice if I was able to execute those scripts directly from my own without having some server running.
I also know about Xaml, which I don't like since its require you to refresh some browser.
You can do all the stuff suggested above or ...
Go to php.net and download the php file stack for windows.
Copy the file stack into say c:\php or if you want multiple versions, say c:\php5 or c:\php7 etc.
Open powershell and type c:\php\php.exe -h, you will get the php help output. Yay you are up and running, whoot.
(Note: you may need to rename php.ini.development -> php.ini
Advanced instructions:-
Type env into os search (cortana) and select environmental variables.
Add your php location to path (c:\php) and create a variable php (or php5 etc) pointing to c:\php\php.exe
Now you can run php in powershell with php (php -h to test).
Note: while not the question, this also works in the git bash shell.
I'm assuming windows since you said powershell. You can just install php on windows but that means also installing apache or enabling IIS.
Or there's apparently a built-in webserver for command-line functionality that might minimize the amount of headache involved in configuring that stuff.
This might help get you going also:
http://php.net/manual/en/install.windows.legacy.index.php#install.windows.legacy.commandline
I am using php version 5.3 on media temple's grid server, however when I call a file using exec() the page is executing in PHP version: 4.4.9
The reason I am using exec() is to process the file in the background.
This is probably a simple question, but how do I manually set the PHP version to 5.3 for this file without using .htaccess?
Thanks.
The PHP interpreter you invoke via exec() is often a CGI version installed on the server as /usr/bin/php. You need to find out if a more contemporary version is available and then call the interpreter explicitly:
exec("/usr/bin/php-5.3 your-script.php &");
# or just adapt your scripts shebang #!/usr/bin/php5
(Just an example, the filename will be different. Also you can usually leave out the path. It's mostly just security relevant for setuid binaries.)
You might find out about other versions via print_r(glob("/usr/bin/php*")). But asking your hoster might be a better idea.
I have a (possibly dumb) question.
I have a script made in php, constructed for cli usage. Works fine when I run it from the command line, no problem there. The problem is that the site I'm working on has ssh restrictions on the hosting server and I cannot ssh there to run it. Hence my question: how can I run the script from another php that is web-accessible? Already tried with exec(), system(), etc.
The main problem is that I need he $_SERVER['SHELL'] variable set, and when the call is comming from a web browser of course php doesn't set it.
Any ideeas will be greatly apreciated, thanx.
There are many possibilities why exec() and related function calls are not working for you.
Your webhost does not have PHP-CLI installed. Just a webserver module
You need to use the full path to the php binary for lack of a decent shell environment. E.g. /usr/bin/php <script> instead of php <script>.
Your webhost has installed PHP-CLI on a non-standard path (e.g. /usr/local/bin/php, or /opt/php5/php)
The webserver user does not have rights to access the php binary
Et cetera..
maybe update the php script to be both an include and a cli script.
use
__FILE__
to check if it's a file, then read the params. otherwise do nothing.
and as an include just call the function you want directly.
From inside a PHP program I want to know the location of the binary executing it. Perl has $^X for this purpose. Is there an equivalent in PHP?
This is so it can execute a child PHP process using itself (rather than hard code a path or assume "php" is correct).
UPDATE
I'm using lighttpd + FastCGI, not Apache + mod_php. So yes, there is a PHP binary.
eval/include is not a solution because I'm spawning a server which has to live on beyond the request.
Things I've tried and don't work:
$_SERVER['_'] looks like what I want from the command line but its actually from an environment variable set by the shell of the last executed program. When run from a web server this is the web server binary.
which php will not work because the PHP binary is not guaranteed to be the same one as is in the web server's PATH.
Thanks in advance.
The PHP_BINDIR constant gives you the directory where the php binary is
Yeah, $_SERVER['_'] is what you're talking about, or as near as exists. The reason you're getting a Web server binary when it's run from the web is that /usr/bin/php has nothing to do with the Web server's execution; what it's running is a separate SAPI. There's nothing from the web PHP instance to point to /usr/bin/php because there's no reason for there to be.
The PHP_BINDIR constant is probably the easiest thing to use; the next best thing I could come up with is basically re-creating that bindir path from the extension_dir configuration setting:
$phpbin = preg_replace("#/lib(64)?/.*$#", "/bin/php", ini_get("extension_dir"));
It has a regex in it, so it feels more like your native perl(!) but otherwise is not especially optimal.
In PHP5.4 you can use the PHP_BINARY constant, it won't work via mod_php or similar but will via CGI etc.
For earlier versions of PHP readlink('/proc/self/exe'); will probably be fine, again it won't work via mod_php.
Depending on the way php is installed you CANT find the php executable.
if php is running as a module for the webserver like apache module, then there is no binary you can call.
you can take a look into php_info() it lists everything.
may also the path to php. within that path you can assume a php binary.
but why do you want to call a extra process?
you can execute other php files by include command or eval.
there is no reason to spawn a new process.
what about:
<?php
exec("which php");
?>
But, it's unix/linux only:D
I've been looking for the php7 executable on my mac (OSX El Capitan) in order to configure and install xdebug (needed to find the right version of phpize to run). None of the solutions I found worked for me, so I just ended out searching for it:
find / -name php -print
I knew (from phpinfo()) that I was running php7, so I was able to infer the correct directory from the options presented by find.