How to easily display PHP/HTML codes in your webpages

August 2017

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.


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;     
       )     
   )

Related


Published by aakai1056. Latest update on February 16, 2012 at 09:56 PM by Paul Berentzen.
This document, titled "How to easily display PHP/HTML codes in your webpages," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).