Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Do You Want To Use Php Code In Your Html Pages?, Within two minutes you will!
Jimmy
post Mar 23 2007, 10:13 AM
Post #1


Super Member
*********

Group: [HOSTED]
Posts: 484
Joined: 9-April 06
From: The UK
Member No.: 21,584



Whilst searching around for help to setup cutenews blog I came across a way to use php in html pages - lo and behold it works! so I thought I'd share it with you all (Unfortunately I can't remember the site so I wrote this up a couple of minutes after I did it:) ).

This method requires a web server with apache installed. So, luckily for us all this covers the whole of Trap17... even Qupis tongue.gif

Just to make the point, this is in no way a difficult task and it doesn't require you to change your pages at all and you don't even have to rename them .php.
(This works for web files saved as both .htm and .html)


So Step 1 - Find the .htaccess file in the "public_html" root directory on your server.

Step 2 - Now, open the .htaccess file here with notepad - you can use the cpanel editor too - and add the following code to the file:
(If the file is not there create a new, empty file in notepad or another text editor and save it as ".htaccess" - Upload this and continue smile.gif )
(If like me you have never done anything with this file it should be empty)

CODE
RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html


Step 3 - Save it and your done! Thats it biggrin.gif

Now whenever you need to use php code, simply write it into the document and it will recognise the content as php and use it as you intended!

This is a sample code of what you have to do in your page... Nothing! Simply put the code in and Bob's your uncle >:)

CODE
<html>
<head> <title>Php Test Page</title> </head>
<body>

   <div>
   <?php echo "This Really Does Work!"; ?>
   </div>

</body>
</html>


And the best thing is... Your html pages still validate! so you don't have to decide between ease of use and getting you pages to validate; you can have it all biggrin.gif

Hope this helped someone out.. It certainly helped me out
Go to the top of the page
 
+Quote Post
jlhaslip
post Mar 23 2007, 03:41 PM
Post #2


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,072
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



Good job, Jimmy...

And here are a couple of links to Trap17 Tutorials which are related to this Topic.
The following Tutorials share information about how to set up an Apache server onto your local machine which saves you from having to use the Trap or Qupis facilities to test your PHP code.

http://www.trap17.com/forums/index.php?sho...32&hl=XAMPP
http://www.trap17.com/forums/index.php?sho...17&hl=XAMPP

The XAMPP package includes Apache, MySQL, PHP4 and PHP5, Perl and several other softwares. (PHPadmin, an FTP server and a Mail Server, for instance)
In fact, you can duplicate the entire Trap17 server set-up on your Localhost. Saves quite a bit of time and hassle since you don't need to upload all of your test cases.
Go to the top of the page
 
+Quote Post
dotasif
post Mar 23 2007, 06:12 PM
Post #3


Newbie
*

Group: Members
Posts: 4
Joined: 21-March 07
Member No.: 40,465



smile.gif
That is excellent I cant believe this…..it works……as I post just 15 min before I write this ….that I cant work php code on quips now…..after I see your solution I used it ….and it works. Ohhhhoooo.. laugh.gif
This forum is really helpful as Im new here….

Thanks I really mean it…. rolleyes.gif
Go to the top of the page
 
+Quote Post
shadowx
post Mar 23 2007, 06:55 PM
Post #4


A clever man learns from his own mistakes, a WISE man learns from those of OTHERS
***********

Group: [HOSTED]
Posts: 1,028
Joined: 12-April 06
From: Essex, UK
Member No.: 21,719



im surprised this works but i believe all of you! if im right its the same code that goes into .htaccess as that which makes the dynamic signatures with the ip's and browser info etc... but i would have thought that as it is treating HTM pages as php ones that it would throw an error when you use raw HTML as a normal php file would as its not the right syntax. Anyone know why it doesnt throw an error? or maybe it does but no-one mentioned it and i havent tried it myself
Go to the top of the page
 
+Quote Post
jlhaslip
post Mar 23 2007, 07:40 PM
Post #5


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,072
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



The php parsing must be invoked by the php start and end tags, otherwise, the php parser treats all files as html, so re-directing html files through the php parser is no big deal.
Although it does add an additional step to the throughput, so the delivery of the page is *somewhat* slower.
Unless you are parsing 100k files with mega database calls, you'll not notice the change, but it is a little more demanding of the Server resources.
Go to the top of the page
 
+Quote Post
Tetraca
post Mar 23 2007, 09:47 PM
Post #6


Privileged Member
*********

Group: Members
Posts: 628
Joined: 20-May 06
Member No.: 23,968



It's not really good to do that. It's better to have a PHP file with a header and a footer of HTML included so that you can make universal changes to your site, and not have to deal with modifying multiple files just to get the template working unless you deal with JS and CSS files.
Go to the top of the page
 
+Quote Post
jlhaslip
post Mar 23 2007, 09:55 PM
Post #7


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,072
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



That's the thing, Tetra, you can do exactly what you are saying, only with an html file extension if you add the posted code into your .htaccess file. The file contents would be the same whether you use the php or html file extension.
It just conceals the fact that there is a php script running because when someone looks at your URL, it appears to be an html file.
Go to the top of the page
 
+Quote Post
Jimmy
post Mar 23 2007, 10:00 PM
Post #8


Super Member
*********

Group: [HOSTED]
Posts: 484
Joined: 9-April 06
From: The UK
Member No.: 21,584



QUOTE(Tetraca @ Mar 23 2007, 09:47 PM) *
It's not really good to do that. It's better to have a PHP file with a header and a footer of HTML included so that you can make universal changes to your site, and not have to deal with modifying multiple files just to get the template working unless you deal with JS and CSS files.

Hmmm... the thing about this was if you have a html page, and you need to insert a small bit of php, instead of forcing you to change everything to xhtml standard or re-write your site as php and put it in, you can simply do this and it will work fine. As above, you're really describing the same thing, just in a different way (where it can be done with .htm and .html files instead of php files with a different extension) !
As Jim said, It would technically make everything slightly slower, but unless your pages are > 100K and your querying huge databases, you really won't notice the difference biggrin.gif

Dotasif: Glad I could be of help ! Its really nice to know someone benefited already from this biggrin.gif

Shadowx: Nope, it gives no error (that I'm aware of!!) And simply does what it says on the tin smile.gif.

This post has been edited by Jimmy: Mar 23 2007, 10:02 PM
Go to the top of the page
 
+Quote Post
jebriggsy
post Mar 29 2007, 01:55 PM
Post #9


Newbie [Level 1]
*

Group: Members
Posts: 14
Joined: 29-March 07
Member No.: 40,791



Or you could've just renamed the file.html to file.php and added the php script rolleyes.gif As long as the output HTML is good it won't affect anything whatsoever... The only useful thing I can thing of that comes out of this is hiding the fact that you're using PHP. But I don't see the point because if you know PHP that means you're more skilled than someone who just knows HTML...

Now if you're doing that to get rid of file.php?this=that&that=this, I can see what you're getting at with SEO. But why not just use mod_rewrite instead?