PHP not working on html anymore - php

I have a website consisting mostly of html webpages. In order to allow me to use php in them, in my .htaccess file I added a bit of parsing code. A few days ago the php somehow stopped being executed and I'm still not sure why. I googled a few things and tried a few different types of parsing code but nothing seems to work. At the moment I have this line in my .htaccess file:
AddType application/x-httpd-php .html .htm
I have noticed one thing though, the php works fine at http://turkishbasics.com (my site's home page) but doesn't work at all for any other directories like http://turkishbasics.com/index.html or /contact.html
I contacted the hosting company, the guy mentioned something about Apache.?
Just how do I solve this problem?
Thanks

If you havent yet heard of Apache then this might be a bit above you. Skip out the AddType decleration and just save your files with a .php extension instead of html

To excute php code you need to write it in a php file (.php). Then execute the file in server.
You can not execute php code from html file (.html)
I guess when you execute http://turkishbasics.com it takes from http://turkishbasics.com/index.php

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.

Apache Server Downloads .php Files in Subdirectories

So I've been searching all over the internet for a solution to my problem and I've tried just about everything I could find, but still nothing is working.
My problem is that when I open a .php file in chromium by using:
localhost/filename.php
it works fine. If I put the file in a subdirectory and access it as such:
localhost/subdirectory/filename.php
it works fine as well. The problem comes when I try to access the .php file in the subdirectory from another file. For example I have an entire project located under:
var/www/test
with the index.html sitting in that folder and then the .php scripts in a folder located at:
var/www/test/php_scripts
In the HMTL file there is a form that sends the form data to one of said .php scripts and when I click the submit button it downloads the .php script instead of navigating to it and executing in the browser.
I tried moving the script into the "test" folder so it was with the index.html file and that didn't fix anything. If I place everything in:
var/www
it all works fine. But I don't want to do that because then I have to deal with a cluttered mess of files which no one likes doing...
I did search for the past couple hours all over stack exchange and other sites and couldn't find anything that worked. If this is a duplicate question then please point me in the direction of the answer, if not, hopefully one of you jedi coders can solve my headache.
Thanks,
Zak
It sounds like you need to look at the action part of your html form.
Make sure that the action starts with a /
Example
<form action="/php_scripts/script_name.php>

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

Which file to adjust to allow .php files to display (not download)

I'm having some trouble with .php files which seem to download when I open them in the browser.
I've googled around and have found this is because the server doesn't know how to parse the file, and that I need to add something to make this work.
I've read around and it seems I need to add something to a .htcaccess file, but I'm not sure where this is or where I should create it.
Below is a screenshot of the directories on my server if anyone can help me.
You mean something like
AddHandler x-httpd-php5 .php
...this should already be in place in the master Apache file (or the appropriate .conf include - in some distributions, configuration directives may be parceled between several configuration files) if PHP is properly installed.
you dont need to generate a htaccess file for this issue, if you installed a web server and php and configured them properly.

php code displaying on web page instead of working behind the scene

I have an index.html in my wampserver www directory. On this html, there is a link for a user to upload file. When I hit the link, I select files to upload but instead of the uploadmanager.php which i have tested in my eclipse debugg environment to work, it displays the some part of the code on the web page without doing anything thing. This is not what I expect. Can someone please tell me what is wrong? Thank you.
sound like you are using php-short-open-tags (<? instead of <?php) without enabling this in your php.ini. change your php.ini or use the standart open-tags to solve this.
Are you sure you enabled PHP in WAMP?
Try creating a new uploadmanager.php file directly in wamp/www (or whatever subdirectory) and paste the code from your tested uploadmanager script into the new file. Then try to run it in WAMP.
I think it is a permissions problem. I copied an index.php file into a c:/wamp/www/subdirectory and it only displayed the code. Once I created a new index.php file and pasted the contents of the old file into it, it worked perfectly.
Are you posting to the uploadmanager.php page? Are you getting an error or just seeing the code? Can you post the code from the index.html page that handled the form and the part of the php code you're seeing for us to look at?
Every now and then I have Apache serving the .php as downloadable files instead processing them on the server, but only with random requests.
Some reasons, why this might happen, are
PHP misconfiguration
PHP-files in a directory without execute rights
wrong content type sent
timeout from script execution
In my situation the last bullet is the most dangerous, but luckily it seems to show up only immediately after modifying some of the .php files. I haven't tracked the problem any deeper yet, but it seems to relate some filesystem level operations (as the disk I/O is a bottleneck) and presents itself only in testing env.

Categories