Php script in html run locally with Apache - php

I'm using Apache 2.2 on windows 7 to play a little with php5 code (locally via http://localhost/).
The php is installed as apache 2.2 module.
Problem is, that php code works great (for example, <?php phpinfo(); ?> in index.php file),
and html code also works (<html><body>Hi!</body></html> in index.html), but
php script in html doesn't work (<html><body><?php phpinfo(); ?></body></html> in index.html).
What am I missing in the cofiguration file for running php script in html pages, locally?

Add this to your apache configuration:
AddHandler application/x-httpd-php .html

You need to name files that have php code in them as .php otherwise apache won't parse the code.
For example:
<html><body><?php phpinfo(); ?></body></html>
Needs to be named
index.php
You do not need to name the files .html just because it's html in the file, but if there are php code you have to name the file .php.

If you're putting php code in it, you have to call it index.php, not index.html.

Related

php script works fine, but not inside HTML

I've installed WIndows 10 version of XAMPP with MySQL (MariaSQL) & PHP version 5.6.28
The SQL is working just fine, i.e., I can log in, create DATABASE, etc.
php -v works fine on the command line ... PHP 5.6.28 (cli)...
phpinfo.php works as expected.
It tells me display_errors is ON
php.ini = display_errors=On
As a php script: mysql_test.php output to the screen is fine.
<?php
echo "Hello World of PHP!";
echo mysql_connect ('localhost', 'joe', 'gonzo9876');
?>
When I embed it in plain vanilla HTML, i.e., http://localhost/mysql_test.html
The php code won't echo/print on the screen
- and -
when I right-click for viewing the source code, the php code is visible
- and -
the Google debugger has converted the php tags to
Your Apache, by default, will only run files with .php extension as PHP. .html will be displayed to browser as is.
You need to either:
Rename your file from mysql_test.html to mysql_test.php; or
Config your Apache to also treat .html files as PHP script
The later one is an unusual practice. I wouldn't recommend it.
Basically no hosting provider will do it. So even if you make it work in your XAMPP setup, it won't work in any normal shared hosting. So if you potentially need to move your code to a shared hosting, please don't do it.
You can't process the PHP code inside html page with .html extension(without parse). It's only for rendering html, if you wanna use embed/mix both php with html, then use .php extension instead as PHP is server-side scripting language. When talking about server-side language, you need a server either local(xampp,wampp,etc..)/production server to host and run your apps.
Reflect to Commenter's comment :
Another workarounds is by telling the Apache to treat .html as .php and with this, you can mix php code with html by using .html, but it's just kinda a HACK for me(personal perspective). Well the choice is yours.
You need to make extension .php if you want to put php code inside html tags. But if you do not want to show .php, please use .htaccess for url rewrite. You can make file as .php but with .htaccess you can show as .html so user will see it as .html.
RewriteEngine On
RewriteRule ^test.php test.html [R=301,L]
or something like this, please search for url rewrite for more detail.

Want to learn how to use php to save data in forms. How come this doesn't work in a html file?

I'm a newbie please bear with me. I just finished learning how to create forms in html. Now, I want to learn how to save and display the data using php. While following along the php exercises in w3, I tried running this file but it didn't work when saved as a .html file. Could someone explain what I'm doing wrong and why the code text won't display? Thanks.
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Your server is not setup to parse PHP in files with a .html extension. Add an .htaccess file to your web root with the following lines in it -
# allows HTML files to be interpretted as PHP files
AddType application/x-httpd-php .html
You should save it with a .php extension. I assume you're working locally and you would need to install PHP on your machine. The best way to install it locally is by using XAMPP or WAMP to emulate a server. I prefer using XAMPP https://www.apachefriends.org/index.html. Install it start the apache service, put this file in your htdocs folder i.e. 'root' and navigate to 127.0.0.1 from the browser. This is a start, even if you put it on an "actual" server it'll work fine.
First off, you need to change your file extension from .html to .php
Are you running a web server? If not, you need to either be running a server like MAMP
just done you a quick form here
Copy and paste it to where ever you run you script,make sure it has a php extension, and see if it gives you the same result as it does in the link. If not then your configuration is wrong. If you have xampp then make sure it is in the HTDOCS directory

php server doesn't seem to execute code, although server is running

I haven't worked in a very long time with php so this question may be a very noob one.
I've installed apache server 2.0.65, and although when i call the localhost, my page in htdocs gets executed, only the html code appears in the view. The php code appears in the source view as html would, thus leading me to believe that it doesn't get interpreted.
My code is the from w3school:
<!DOCTYPE html>
<html>
<body>
<?php
echo "My first PHP script!";
?>
</body>
</html>
what am i not doing ok?
You did not install PHP
You did not configure PHP to run your file (by default it will process files with a .php extensions)
You aren't actually visiting http://localhost/foo.php in your browser
Apache would just run HTML, CSS. In order to make PHP work, you must install PHP, and set environment variable on your local machine, so that all files with .php extension would be run by PHP.
Filename must have a .php file extension.
PHP must be installed and running - if you can open a terminal and type php -v or use top to see if php is running.
If you are running on your local computer try installing WAMP or something similar.

Apache: Inline PHP not working on Linux

I am having trouble getting my PHP to work for my Apache server. I am running Oracle Linux, and used yum install php + yum install httpd to get my PHP and Apache. I have scoured the internet and done a couple of things:
Firstly, I have gone into my httpd.conf file and added LoadModule php5_module modules/libphp5.so. I have chosen to use the one in my php.conf file, which is the exact same one. Furthermore, I have done AddType application/x-http-php .php, so now my .php files are loading fine.
Secondly, I have tried to run my Apache server, and it works fine. It displays my index.html file, which I set with DirectoryIndex. However, when I tried to put php code into it, it got automatically commented out, which I assume is because I haven't set it to properly execute on the server side.
Now, with all that said, I am wondering what else I may be missing. I have two set-ups, one where I have a .php file, and one where the php is inline with the html.
index.html
<!DOCTYPE html>
<body>
<?php echo "Hello everybody."; ?>
</body>
</html>
index.php
<?php phpinfo(); ?>
The index.php file loads fine, but the index.html doesn't run the php code. How do I get the inlined version of php to work?
Note: I have set-up my Mac OS to work fine with .php files, but it is also having trouble with inline php within an Html file. What am I missing?
SOLUTION: Html files themselves cannot include php. Instead, the file must be a .php extension, and within a .php file, you can have text, html, and JavaScript.
I do not think .html files are run through php at all so the <? ... ?> is treated as a normal tag which is invisible.
Change the extension to .php.
Php can contain html.
Edit:
There exists an option to configure for example Apache to parse html files as if they are php if you for some reason cannot or do not want to use php endings.
This, in my opinion, is not a good solution as it hides the fact that the page is dynamic to a future maintainer.
There is a similar question which has a similar problem:
PHP code is not being executed, instead code shows on the page
You might want to check out points 2 to 5 in the accepted answer:
LoadModule (It seems, that you have configured that properly)
Set Apache to run PHP files (this is the third point, and it seems that
you didn't configure that). Add the following line to your httpd.conf file: AddType application/x-httpd-php .php
Make sure, that you have the file ending with the .php extension (after you have configured Apache to run PHP files, see the previous point)
Change your code to use the long PHP opening tag (<?php instead of just <?)
You need to set short_open_tag = On in your php.ini file

Apache2 Web Server Isn't Executing PHP Code inside HTML Page

I can execute PHP code fine if I name the file *.php and load it in Chromium through the localhost address.
If I try to insert PHP inside a file named *.html, the PHP no longer executes.
Code:
http://pastebin.com/LgCzMs02
Output:
http://imgur.com/0CO5S
Unless you update your Apache configuration for the PHP handler to include the html extension, the extension needs to be php.
program that have includes with php file should have the extension of *.php
unless you will redirect your index.html to a *.php page during loading.

Categories