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

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.

Related

PhpStorm runs only JavaScript instead of PHP?

I have just installed fresh PhpStorm on Mac. I set my interpreter and so on. I can create .php files. But when I run it runs only JavaScript and there is no option to run php? How can I fix this?
Run 'filename.php (JavaScript...)'
Same with debug. How this can be fixed?
It seems you've configured PhpStorm to handle files with .php extension as JavaScript code rather than PHP.
Open the settings dialog (File/ Settings), head to Editor/ File Types and review the Recognized File Types section:
Remove *.php from JavaScript.
Make sure that PHP contains *.php.
Remove any other unwanted registered pattern, such as filename.php.

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.

php not loaded but the code exist in view source page

i was setup my server, unfortunately the php page not loaded in the browser. Looking at the view source pages, i see my php codes. I using centos and install cpanel.
What usually reason for the problem?
It means PHP is not running.
Have you tried reaching localhost in your browser? Generally you should see the PHP index file, which tells you if the server works or not.
A few possibilities :
Is your file in the proper root folder? Is is accessed via localhost (not file system!)
is your file in .php (ex. NOT .html)?
Are you using full <?php ?> tags? Some server configs won't recognise <? ?>

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.

Php script in html run locally with Apache

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.

Categories