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
Related
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.
I use OS win10, xampp ( xampp manager already run and apache too) localhost and phpadmin run well. but when I write coding phpinfo.php like this coding
<?php
phpinfo();
?php
in chrome will show same like that coding.
But if I add HTML format then in chrome will blank or not show any
it happens with coding
<HTML><BODY> <?php echo "Hello World!" ?> </BODY></HTML>
I already put there C:\xampp\htdocs but still don't show any.
if I write code without html
<?php Echo " Hello World ! " ?>
then chrome show all script php code in browser.
is anyone can help me ? thanks a lot
Sounds like there is something wrong with your configuration, here's a few things you can check:
Make sure that PHP is installed and running correctly. This may sound silly, but you never know. An easy way to check is to run php -v from a command line and see if returns version information or any errors.
Make sure that the PHP module is listed and uncommented inside of your Apache's httpd.conf This should be something like LoadModule php5_module "c:/php/php5apache2_2.dll" in the file. Search for LoadModule php, and make sure that there is no comment (;) in front of it.
Make sure that the http.conf file has the PHP MIME type in it. This should be something like AddType application/x-httpd-php .php. This tells Apache to run .php files as PHP. Search for AddType, and then make sure there is an entry for PHP, and that it is uncommented.
Make sure your file has the .php extension on it, or whichever extension specified in the MIME definition in point #3, otherwise it will not be executed as PHP.
Make sure you are not using short tags in the PHP file (<?), these are deprecated not enabled on all servers by default. Use <?php instead (or enable short tags in your php.ini whith short_open_tag=On if you have code that relies on them).
Make sure you are accessing your file over your webserver using an URL like http://localhost/file.php not via local file access file://localhost/www/file.php
And lastly check the PHP manual for further setup tips.
Source: Here
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
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.
I am a new guy in web programming who still learning new stuff.
I am creating a web form that consist of one INPUT. I connected the form in the HTML file to the php file. I am using a localhost MAMP that I recently installed on my MacBook.
The Problem : When I open the HTML file on a webpage and type something on the input box and hit submit, the browser ask me to download the php file rather processing it.
I don’t think the problem is related to mysql or apache since when I run the MAMP it give me green signs next to the apache and mysql.
Do anyone know how I can fix this problem and the let the php file open and transfer inputs to the database?
You'll want to make sure that Apache has been told that .php files should be treated as PHP scripts. That means one of the following:
LoadModule php5_module modules/libphp5.so # on windows, this'd be a .dll instead
AddHandler php5-script php
and/or
AddType application/x-httpd-php php
in your httpd.conf file.
open your file from virtual web-server, not filesystem.
you have to type something like
http://localhost/form.html
in your browser's address bar
Did you checked out http://www.mamp.info/en/documentation/faq.html#q8 ?
Maybe you didn't put your site into the folder "htdocs"?
PHP won't be parsed unless it is located in that folder or a subfolder.
That would explain the fact that your browser downloads the php file instead of getting the parsed php from apache.
This error is not only about php but general. The file that the browser suggets to down load is simply the script name, php or not.