Ask a question »

How to easily display PHP/HTML codes in your webpages

April 2015

Source codes at the end of the webpages can be of vital importance because it enables others to create new webpages and it allows the alternation of the page's display. This code can be displayed easily in by HTML files.To do this from the View option in the Taskbar, the Source option has to be selected. This procedure works for Internet Explorer web browser users. In PHP, source codes can be displayed too. At the end of the PHP files, the codes can be added. It is important to remember that displaying the code correctly is crucial.

How to easily display PHP/HTML codes in your webpages




Issue


If users want to view the source code for your website, they often have only to right click on a page and click View Source.

However, if you want to be sure they can view the source - perhaps to help them learn to create their own website based on your own - you use certain PHP codes.

What code to use?


If you want to display the code of your index file (index.php), all you have to do is to insert
highlight_file(index.php);
in the body of the document.

One other way to do it is to use the constant __FILE__. You should however use the exit function to avoid displaying the rest of the script. This is very helpful to avoid displaying a protected document including a username and password.

Here is a full code that you can use to display the code correctly. You can insert it after your body tag:

display_code_php function ()     
   (     
     if (isset ($ _GET [ 'source']))     
       (     
         echo '<a <p> href="',$_SERVER['PHP_SELF'],'"> Back </ a> </ p>';     
         echo '<p> This is the code php file: </ p>';     
         $ page = highlight_file ($ _SERVER [ 'SCRIPT_FILENAME'], TRUE);     
         $ page = str_replace (     
           array ( '<code>', '/ code>', '','</ are >','< font color ="'),     
           array ( '<pre style="padding:1em;border:2px solid black;overflow:scroll">', '/ pre>', '','</ span >','< span style = "color:' ), $ page);     
         echo $ page;     
         echo '<a <p> href="',$_SERVER['PHP_SELF'],'"> Back </ a> </ p>';     
         echo '</ body> </ html>';     
         exit;     
       )     
   )
For unlimited offline reading, you can download this article for free in PDF format:
How-to-easily-display-php-html-codes-in-your-webpages.pdf

See also

In the same category

Published by aakai1056. - Latest update by Paul Berentzen
This document entitled « How to easily display PHP/HTML codes in your webpages » from Kioskea (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.