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
Related
A non-technical owner of an authority site with 1000s of asp/aspx pages decided to rebuild the site using WordPress on a new cPanel server. The original site was on a windows server and built with ASP.net. The problem is that they now had lots of dead external links going to *.asp and *.aspx pages that no longer existed.
I can get the static, mostly html content from the old site and wrap a php template derived from the WordPress template around it
I've added the following lines to .htaccess file so that asp and aspx files should be able to use php code, but this is not working.
AddHandler application/x-httpd-php .asp
AddHandler application/x-httpd-php .aspx
Any suggestions about getting php code to execute with files ending in .asp/.aspx on a linux host?
You can also set mime type handler using a RewriteRule
Try :
RewriteEngine on
RewriteRule ^.+\.aspx?$ - [H=application/x-httpd-php5]
x is optional in the pattern above ,so it matches both .asp or .aspx .
I am designing a website that requires me to show the outputs of a mysql database on a webpage written in html. However my browser doesn't seem to recognize the php scripts within the html file and returns blank values where their should be details. The scripts worked fine in the .php files but have no effect when included in the .html files even after creating a .htaccess file as instructed in previously asked questions. Is there anything else that can be done to solve this issue?
There are two methods (that I've encountered) of changing the environment on an Apache server via .htaccess to allow PHP scripts to be processed in .html files, depending on the configuation. Whichever of these you've attempted, try the other.
AddHandler application/x-httpd-php5 .php .html .htm
Or
AddHandler cgi-script .html
SetEnv PHP_EXTENSION .html
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.
First of all, I am not trying to run php within a js script, there is a similar question on here that refers to a user trying to run php from inside a js script.
I have added many combinations of
AddType application/x-httpd-php .php .html
AddHandler x-httpd-php .html
to the .htaccess file in a higher level directory containing the .html file I want to run php in. This has not worked. (I am open to trying new combinations)
The is either not read at all or commented out when viewing the source in broswer.
My question is how to get to be run inside of an html file OR is there a better way to include php functionality in an html document without having the code in the same document.
Additionally my host uses cpanel if this helps anything.
I can elaborate on anything I need to, thanks in advance.
You can try to use mod_rewrite for that task:
RewriteEngine On
RewriteRule ^(.*).html index.php [QSA]
I have made a subdomain and uploaded php script on that. but php code is shown there included file are also not shown on page.
In description, i have made a sub domain jobs.example.com. All project done in php on locally when is uploaded it to the srever (in the root flder of jobs.example.com). only html css and jquery is running php code is shown there as it is. How may i handle this plese help me.
My guess is: may be you might need to set up your subdomain folder to run php files.
You might need to setup mime type for .php
# Add this line inside the <IfModule mod_mime.c> conditional brace
AddType application/x-httpd-php .php
Referenced from : http://www.php.net/manual/en/install.windows.apache1.php