Why doesn't this html code work? - php

Given the following index.html file:
<html>
<body>
<p>Welcome to <?= $_SERVER ['HTTP_HOST']; ?></p>
</body>
</html>
I expect to see "Welcome to EXAMPLE.COM", All I see is "Welcome to ".
What would cause this to happen if the code checks out?

Your file is named index.html. Unless you told your server that .html files should be treated as PHP scripts, that means the PHP code is NOT being executed - it's going out as literal text. And since PHP tags make it look like HTML, your browser is properly hiding that unknown/illegal tag.
Rename it to index.php.

It's not running because that part is not html (it's php) but you have saved it as an html file instead of as a php file.
rename it to index.php and try again?

Web servers are usually configured to run PHP only on files with the .php extension. Your index.html file will be passed as-is to the browser, which will probably ignore the unknown PHP tags. If you take a look at the source code of the web page, the tags will probably be there.
If you must have a .html extension, you can usually configure the web server to run PHP on .html files. For example, in Apache, you can use the AddType directive in an .htaccess file or in the server configuration (httpd.conf):
AddType application/x-httpd-php .html
However, this will run PHP on all .html files (in that directory), which may put an unnecessary load on the server.
A much better way is to use URLs without extensions. In Apache, you can use the DirectoryIndex directive to specify a list of index files that the web server will search for:
Options +Indexes
DirectoryIndex index.html index.php
When a browser requests a URL that ends with a slash, such as http://mydomain.example/foo/, the server will search for foo/index.html or foo/index.php in the DocumentRoot (or, failing both, generate a directory listing). You can now use whichever type of index file is appropriate for the moment, without ever having to change your URLs.

Related

HTML: Display a string from a separate PHP file

I've searched all over the internet and can't seem to find a solution to my problem. I want to be able to "call" a php file from an html file and display the string returned:
html_ONLY_file.html
...
<h1>GetHeader.php?type=main</h1>
...
GetHeader.php
if($_GET['type'] == 'main')
print 'Some header to display'; //or echo 'Some...';
exit;
I've done this for images, img src="image.php?file=file.jpg", where image.php does a header(...) and readfile(...) return but I do not know how to do this for simple text. I'm not looking for DOM or anything too involved, if I have to I will. I want to know if there is a simple solution. It's generating the html side that I'm lost on.
In case you want to know, I am doing this because I once used a <#virtual include=...> to call for a php file from my .shtml file. Well, the hosting company decided to change mod_security and now I cannot include any php files (I've already tried everything). Including html files works fine and so I am changing this part of the website around so I don't have to rename files because the site is for a small business that is now ranked highly on Google for its geographical area. If I change the file names, shtml to php, then I believe the Google ranking drops (don't comment on this part unless you are damn sure you are 100% correct).
If you can edit the .htaccess file you can add a line in your .htaccess file that will mean HTML files will be parsed as if they were PHP.
If your server is running PHP as an apache module:
AddType application/x-httpd-php .html .htm
If your server is running PHP as CGI:
AddHandler application/x-httpd-php .html .htm
Source: http://www.besthostratings.com/articles/php-in-html-files.html
Once you've added that your html_ONLY_file.html could look like:
<h1><?php print "some header"; ?></h1>
And it would function just as if it were a .php file.
Alternatively, you could convert all your files to .php and add redirects into the .htaccess file like so:
rewriteRule ^somefile.shtml$ somefile.php [R=301,L]
rewriteRule ^another-file.shtml$ another-file.php [R=301,L]
This effectively says to your server "whenever a user requests somefile.shtml, act as if they requested somefile.php instead". The R=301 is the most important part with regards to Google rankings. This tells Google (and anyone who requests the .shtml file) that it's permanently moved to the new location somefile.php. This transfers all / almost all of the ranking power from the old location to the new location.
Source: http://moz.com/learn/seo/redirection

Using PHP in a .html document

I'm working on a job at work where I need to create a web forum. In my stupidity I decided to use javascript, not knowing beforehand that javascript is a client-side language. I need a way to save the data from the javascript onto the server and then be able to read the data. I tried looking at things like node.js, however I would have reconfigure the entire web server (which isn't mine) in order to do this. The other solution is to use php. Here's the problem: There's a bunch of includes used in the html file that set up the layout of the webpage (i.e. css files, html files, and even a php include). I can't change the name of the index file to index.php because it breaks all of the includes inside of the file. So I need a way to save, say a text file, using javascript and html. If there's a way to, I would like to do something very simple, like include a php file in the html and then call a php function in my javascript code to get the contents of the file into my index.html page. I thought there was a way to call a simple command like this:
<script>
var thedata = <?php getData() ?>
</script>
where the php function getData() would return a json encoded string with all of the data in it (handled from a separate php file). Is there any way to do this? Any other suggestions for how to handle data storage on a server without changing my index.html file to index.php?
Note: I tried accessing the apache httpd.conf file and adding a handler to pre-process .html files as php files, but that doesn't seem to work (nothing as simple as echo 'test' works on the html file).
Add this to your .htaccess:
AddHandler application/x-httpd-php5 .html .htm
source
It causes Apache to treat HTML files as files that contain PHP. Be careful not to somehow accidentally use PHP syntax in a regular HTML file, though.
Remember that you'll still need to use PHP tags to enter PHP mode. This works as expected:
<p>html content ... <?php echo 'hello, world'; ?></p>
But this will output the the echo command:
<p>echo 'hello, world'</p>
If can make your webserver process your pages thru the mod_php.
if you are using apache just add this to your .htaccess
AddHandler x-httpd-php .html .htm
AddHandler php-script .php .html .htm
AddHandler php5-script .php .html .htm
AddType application/x-httpd-php .htm
AddType application/x-httpd-php .html
I hope you understand the repercussion of doing this. all your pages will be processed like that and the memory/cpu use of your pages will be way greater.
if this is to happen inside one single file, make sure to add it inside a statement.
and within your example you should add:
<script>
var thedata = <?php echo getData(); ?>
</script>
The file extension must be .php, so that the server knows to parse the file.

htaccess modifications working on some pages but not on others?

I have been trying to execute php code within a document with an .htm or .html extension. I finally got it working using:
AddType application/x-httpd-php .php .html .htm
Now, being able to execute php within .htm documents, only works if I go directly to the .htm page such as: http://www.foobar.com/layout.htm
However, it does not work if I go to the index.php page which uses that layout.htm page...
This is an example of what the index page url looks like: http://www.foobar.com/index.php
What am I doing wrong?
Assuming that you are using something like include in your php file to include the html file, your .htaccess rules will not have any effect on the included file.
The .htaccess rules only get executed on requests that are made to the apache web-server and when you include a local file in php, you are simply requesting a file on the local file system; you are not requesting it through apache.
Edit: Based on the comment below your question, it is also possible that you are using something like readfile to get the contents of the htm file. If that is the case, you need to change that to include so that the php gets executed.
Your .htaccess rule does not apply for included files in php.
Did you check this page: http://php.net/manual/en/function.include.php
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

htaccess downloading file instead of loading

I would really like my index.html to be able to have a PHP script work on it. I read that you can do this through the htaccess file. I only have access to a subdomain website directory, where I can upload my files through FTP.
The directory did not have a htaccess file, so I created one using notepad: .htaccess and added this to the file:
AddType application/x-httpd-php .html
The problem is, instead of loading the index.html page, it downloads it as a file...would I need to add something extra to the htaccess file? :S
You don't need to name the file index.html to have it served by default. You can change the default document using your with an entry in your .htaccess file like this:
DirectoryIndex index.php
Then when you navigate to http://yoursubdomain.example.com you will be served index.php instead of index.html.
If really do want PHP to interpret your .html documents then the entry you had in your question will work when PHP is running as an Apache module. If your host is running PHP as CGI, you want:
AddHandler application/x-httpd-php .html
If it still doesn't work, then this web page has some more suggestions:
http://www.velvetblues.com/web-development-blog/how-to-parse-html-files-as-php/
The directive you have sets the content-type of files with a .html file extension.
If the server has PHP installed and enabled, that content-type will cause it to be run though the PHP engine and then the output from that sent to the client.
If it doesn't have PHP installed, then the file will just be served up to the client with that content-type. Since browsers don't handle PHP scripts themselves, they will then just save the file.
You need to install and enable PHP as well as setting the content-type.
Presumably your hosting is supporting PHP?
If so, then you need to rename your file from index.html to index.php

Apache not handling files correctly (Handler Help)

I'm trying to set up my .htaccess file correctly and I'm having an issue.
The only thing my .htaccess file at the moment is:
AddType application/x-httpd-php .php .html .htm
This is included because my server is not parsing php in my html files.
However when this is included in my .htaccess file, when I open a page in my browser, the user is prompted to save or open the file locally.
I believe the answer to my issues is setting up an action to be done (run with php) however I cannot find out the path to my php files.
Any help is appreciated.
You will need to edit the configuration for enabled modules. On a Debian/Ubuntu type system this will be in /etc/apache2/mods-enabled The file you are looking for is php5.conf
So far all you have done is specify that (dot) htm, html or php files should be served -by default- as application/x-httpd-php, and to my knowledge there is not a single web browser that would attempt to interpret such content -- hence the save-as dialog.
Either you could fix your .htaccess file not to be broken (it is broken behaviour to serve html files as application/x-httpd-php), or you could manually output the correct HTTP headers using the PHP header() function.
Unfortunately, everyone seems to love abusing AddType (and then complain e.g. that MultiViews is broken). See this article, please.
This is not supposed to work in all cases. It depends on the AllowOverride directive of the web server.
You shoud specify the AddType in the serveur config file rather than in the htaccess.

Categories