My web host supports PHP. I have successfully run info.php and get pages of stuff to prove it in my browser.
When I try to execute a tiny piece of PHP in a tiny piece of HTML, as in the following hallowed script, I see nothing at all in my browser. Why? Is there something I am supposed to do?
Note: I use RapidWeaver as my principal site generation tool.
<html>
<head>
<title> PHP Test Script </title>
</head>
<body>
<?php
print "Hello World!";
?>
</body>
</html>
Make sure that your file extension is indeed .php, so that the web-server knows how to handle the file. Also, check your error-logs, there might be something in there if you get no output at all. You can do that in your terminal by running php --info | grep error. On your file-system, the error-log is stored in either /var/log/httpd/error_log or /var/log/apache2/error_log (if you're on Linux).
Related
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 3 years ago.
I just started to learn php. I am using lubuntu and that's why I don't want to install xampp, wamp, or similar programs because I have very limited resources.
I installed Apache server and php 7, which seems to be working fine because I can execute and visualize html code via localhost. The problem is when I run php code the web page shows blank. How can I fix this issue?
I have only one file on /var/www/html which is called index.php and can be visualize on localhost/index.php
php -v
php 7.2.19-0ubuntu0.18.10.1
apache2 -v
Server version: Apache/2.4.34 (Ubuntu)
The code I have on index.php is this:
<!DOCTYPE html>
<html>
<body>
something
<?php
echo "hello world";
?>
</body>
</html>
On localhost/index.php it only shows "something", which means apache server is running correctly and runs html code but I don't know why php doesn't run. If Instead I just run a file with php code like
<? php
phpinfo();
?>
it only shows a blank page.
If you see a blank page then normally something else is blowing up behind the scene. I suggest you check both Apache and PHP error logs. Normally you would find both in /var/log/*
It could very well be permission related or a module missing.
I recently installed an Apache2 web server, and PHP5 on my Raspberry Pi, and hooked it to my home router. It all works fine in terms of displaying HTML, even when the index document is a .php file. However, as soon as I try to enter any PHP code, and run it through my browser, it's completely removed from the document.
<html>
<head>
<?php
$baseUser = fopen("GCusernames.txt", "a");
$newUser = $_POST['user'];
fwrite($baseUser, $newUser);
fclose($baseUser);
?>
</head>
<body>
<h1>Writing to userbase</h1>
</body>
</html>
PHP document inspected in my browser
I've tried changing it back and forth to a HTML file and scouring the internet for clues. But everything I found was seriously unclear and vague.
I'd appreciate any help at all. Thanks.
UPDATE: So after I realized how idiotic I was (thank you everyone who answered,) I now understand that the PHP code shouldn't show up in the browser anyway. Sorry about that.
However, it still doesn't explain why the code doesn't execute at all. It just goes straight to the PHP page, rather than executing it.
PHP is interpreted by the server, it is not send to the user.
Thats what PHP is for.
For Example:
You want to make a password login. You write the passwordfunction in php because the user cannot see it.
More Information on PHP
PHP is run by the server that your '.php' file lives on, in this case, your Raspberry Pi. When you request a page from your PI your PHP code will be executed before any HTML is returned to your browser.
You will never see your PHP code in your browser's inspector because your browser never even knows it existed.
If you added something like:
<?php echo "This sentence was written by PHP"; ?>
to your page, you would see the text This sentence was written by PHP in your HTML but you wouldn't see the word echo or the <?php ? tags.
There is a great piece in the PHP docs which explain the three main abilities of PHP. They also have a good guide on how to get started with PHP which may be worth checking out if you are not that familiar with it.
Further Reading:
PHP enabled page
Many people try out codecademy
I'm running lighttpd 1.4.35 on Debian 8.2. I have a very simple html file with php code (php5) which calls a bash script and prints the output:
<html>
<body>
<?php
$res = shell_exec("/var/www/html/run.sh");
echo $res . " is the result";
?>
</body>
</html>
If that html file is called on firefox, the output is
is the result
If I directly run php with that file (php index.php), the output is
<html>
<body>
13.00
is the result</body>
</html>
So, where does the result get lost?
edit:
The webpage source code from firefox is
<html>
<body>
is the result</body>
</html>
edit: solved. bash script uses '~' which expands to wrong directory when the script is run from webserver.
The exec functions "only" return the contents of stdout, which is why you might miss an error message.
But you can redirect stderr to stdout, e.g. via
$res = shell_exec("/var/www/html/run.sh 2>&1");
shell_exec does not run if you're in safe mode, that could be the issue
With the following code below,
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
I want only the Hello World statement to be printed, but it also prints out ;?>.
After I inspected the element, it came out like this.
<body>
<!--?php echo '<p-->
Hello World
<p></p>
"; ?>"
</body>
Your website is treated as plain HTML. Ensure that you have a webserver running:
If you have accesst to the command line on a Gnu/Linux derivate you can start PHP's internal server by:
php -S 127.0.0.1:1337
This command has to be started in your web root and you should have an index.php living there. Then your code runs fine.
In the long run, you want to set up a nginx or an apache2. Another aspect to look into is using vagrant to setup a virtual Linux machine that creates your development enviroment.
You are trying to open the file directly in your web browser, but unlike HTML, your browser is incapable of understanding PHP.
PHP needs to be run server-side behind Apache, Nginx or any viable web server, your web browser will then request pages from that webserver, which will request PHP to serve your browser the fully generated page.
Practically, if you're trying PHP for the first time, you want to install a development LAMP stack such as WampServer (for Windows only), XAMPP (for Linux, Windows, OS X) or MAMP (for OS X, Windows). Installing and running this will easily set up a web server on your computer.
I will not write a full tutorial on how to use a LAMP stack but I can recommend you to read that article from Kasia Mikoluk on Udemy. It should teach you the basics.
This is my php script
<HTML>
<head>
<h1>Hello</h1>
</head>
<body>
<?php
$command=escapeshellcmd('~/PycharmProjects/Test_1/wiki_update.py 2 2>&1');
$output=shell_exec($command);
echo $output;
?>
</body>
</HTML>
I get no output when I run it through the browser.
When I change the
$command=escapeshellcmd('whoami');
it outputs as NOBODY,
I granted permission to nobody for that python script still no output.
I am using xampp for linux and php script in htdocs/xampp.
Are you sure you don't need to change your command to run python?
$command=escapeshellcmd('python ~/PycharmProjects/Test_1/wiki_update.py 2 2>&1');
You also might try specifying absolute path - the ~ alias changes based on current user (which is the web server's user, not yours).