View source php page

Why Don’t I See My PHP Code When I View Source?

Angela Bradley is a web designer and programming expert with over 15 years of experience. An expert in iOS software design and development, she specializes in building technical hybrid platforms.

Web developers and others who are knowledgeable about web pages know you can use a browser to view the HTML source code of a website. However, if the website contains PHP code, that code is not visible, because all the PHP code is executed on the server before the website is sent to a browser. All the browser ever receives is the result of the PHP embedded in the HTML. For this same reason, you cannot go to a .php file on the web, save it, and expect to see how it works. You are only saving the page produced by the PHP, and not the PHP itself.

PHP is a server-side programming language, meaning it is executed at the web server before the website is sent to the end-user. This is why you can’t see the PHP code when you view the source code.

Читайте также:  Php post image file

Sample PHP Script

When this script appears in the coding of a web page or .php file that is downloaded by an individual to a computer, that viewer sees:

Because the rest of the code is just instructions for the web server, it isn’t viewable. A view source or a save simply displays the results of the code—in this example, the text My PHP Page.

Server-Side Scripting vs. Client-Side Scripting

PHP isn’t the only code that involves server-side scripting, and server-side scripting isn’t limited to websites. Other server-side programming languages include C#, Python, Ruby, C++ and Java.

Client-side scripting operates with embedded scripts—JavaScript is the most common—that are sent from the web server to a user’s computer. All the client-side script processing takes place in a web browser on the end-user’s computer.

Источник

PHP show_source() Function

Using a test file («test.php») to output the file with the PHP syntax highlighted:

The browser output of the code above could be (depending on the content in your file):

The HTML output of the code above could be (View Source):

Definition and Usage

The show_source() function outputs a file with the PHP syntax highlighted. The syntax is highlighted by using HTML tags.

The colors used for highlighting can be set in the php.ini file or with the ini_set() function.

show_source() is an alias of highlight_file().

Note: When using this function, the entire file will be displayed — including passwords and any other sensitive information!

Syntax

Parameter Values

Parameter Description
filename Required. Specifies the file to display
return Optional. If set to TRUE, this function will return the highlighted code as a string, instead of printing it out. Default is FALSE

Technical Details

Return Value: If the return parameter is set to TRUE, this function returns the highlighted code as a string instead of printing it out. Otherwise, it returns TRUE on success, or FALSE on failure
PHP Version: 4+
Changelog: As of PHP 4.2.1, this function is now also affected by safe_mode and open_basedir. However, safe_mode was removed in PHP 5.4.
PHP 4.2 — The return parameter was added.

❮ PHP Misc Reference

Источник

How to display PHP source code on the web page?

Whenever you open a PHP file with your server path, it runs the PHP code. It will never display you the PHP source code. Whenever you put some PHP code inside that file, it will run the code instead of showing the source code on the web page.

But suppose you want to show the PHP source with highlighted and with the colors defined then how will you do that? How can you display the highlighted source code on web pages?

Here I am going to tell you how you can do it. I will show you how to display PHP source codes on your web page instead of running it.

Well, there are multiple ways available which can help you to do that. I am now going to tell you four simple and easy ways of displaying the highlighted version of PHP source code with the colors defined. Here are these:

  • Using PHP highlight_string() function
  • Using the show_source() PHP function
  • Using highlight_file() function
  • Use .phps file extension instead of .php

Now I am going to give you the example of doing it using each process that I have listed above.

Display highlighted PHP source code using PHP highlight_string() function

The PHP highlight_string() function outputs or returns a syntax highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP.

Below is the syntax of this function:

highlight_string(string, Return_Boolean)

The string parameter is the required parameter in the above syntax which specifies what to highlight. You have to pass the PHP code through the string.

Below is an example which shows the highlighted version of the given PHP code using the colors defined:

Show PHP source code using highlight_file() function

highlight_file() is also a built in function of PHP which we can use to get the source code of a PHP file. here we just need to give the file path and it will return the complete highlight version of the source code of that PHP file.

Suppose we want to get the source code of a PHP file filename.php, then we can get the source code of filename.php from the below code:

highlight_file('filename.php');

Display source code using the show_source() PHP function

The show_source() PHP function is an alias of: highlight_file() . In this function also you have to pass the PHP file path like that you have just seen in the case of highlight_file() function. Below is the given code which uses show_source() function to get the highlight colored version of the source code of filename.php file:

Get source code using .phps extension

If you want to get the PHP source code directly by opening the URL in your browser then it will never possible as the server will run the codes and it returns the result. But if you use the extension .phps, then it will return you the complete colored and highlighted version of the PHP source code.

I hope you have understood how to show PHP source code on a web page. All the four process I have discussed here are so simple and also understandable. If you like this post then please share it with your friends.

Источник

View source code using PHP

This tutorial will show you how to take a URL, read in the source code and then format it. The code listed will assume that you are passing the URL as an encoded variable via GET with the name u.

First off here are the CSS styles you’ll need to format the code (you should customise these to suit your needs):

body < background-color: #111111; font-family: Courier New, Courier, mono; font-size: 11px; color: #66CCFF; >span < color: #FFFFFF !important; >.linenumber < color: #FF9900; >em < color: #666666; >h3 < font-family: Arial, sans-serif; text-decoration: none; >.codelink < font-family: Arial, sans-serif; text-decoration: none; font-size: 10px; color: #EEEEEE; >.codelink:hover

Now we to read the HTML in via a HTTP file stream. This is very easy to do, we just use PHP’s file() function.

Now we’ve got the HTML stored in a variable we need to use htmlentities() to make the source code display the same way through the browser as it would look like if we were to view the source. We then do some string replacement to insert out CSS styles into the HTML so that the tags and the tag contents are different colours:

 $line) < $line = htmlentities($line); $line = str_replace("", ">", $line); $line = str_replace("", "–>", $line); echo "$line_num  : " . $line . "
\n"; > ?>

That’s all there is to it! If you saved this page as viewsource.php then you’d link to it like this:

Tim Bennett is a web designer and developer. He has a First Class Honours degree in Computing from Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.

Источник

Here is a simple Web 1.5 (static HTML with a little bit of styling and JavaScript) recipe to allow a viewer of your web page to see the PHP source-code, behind it, with a minimal amount of JavaScript and a little CSS manipulation—good for showing the work you’ve done to others. Or for embedding in your own source, in debug mode, so that teammates can see each others’ work.

The PHP and HTML

PHP has a function for this—to display a source file’s text, with the PHP code syntax colorized— hightlight_file() (or the old, more easy to remember function show_source() ) .

The wrapper is just a nice structure for allowing you to (CSS) style the content, if you, later, decide. In line 2, __FILE__ is the absolute path to the source file of the source content (not the URL path).

Show/hide the Source-code

You probably do not want to display the source-code by default, so we’ll hide it and provide a place for the user to click to show (or hide) the source-code:

First, in line 3, we use the style, display:none , to hide the source upon initial display. It’d probably be better to put this into a separate style definition, rather than hard-code it in the element.

We set onClick  with a little JavaScript to do the loading and hiding by manipulating the element’s display style. Here is a clean look at the JavaScript (which should all be included in a single line in the onClick value, above):

[js]
document.getElementById(‘php_source’).style.display =
document.getElementById(‘php_source’).style.display==’none’
? ‘block’
: ‘none’
[/js]

Which simply says, “If the display style for the element, php_source, is hidden (i.e., “none”), then display it (set it to “block”), otherwise hide it (set it back to “none”).

Notes

We want to precede the source-code by the selectable tag, so you can make this even more generic by not relying on a hard-coded id of the element as follows:

[html]

Click here to show|hide source

highlight_file(__FILE__); ?>
[/html]

The onClick code would look like:

[js]
this.nextElementSibling.style.display =
this.nextElementSibling.style.display==’none’
? ‘block’
: ‘none’
[/js]

You can put this at the bottom of your page for other techies to see what you’ve done.

Share this:

Источник

Оцените статью