LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-21-2012, 06:26 PM   #1
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Rep: Reputation: 40
stopping execution of php code within a file in a broswer


Hi group,

I am having a problem displaying a php file in a browser. If I just download the file, the browser executes it. So I have writting a php script to view the code but it execute the file in the browser and does not show all of the file. My script looks like this:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/plain; charset=UTF-8">
<?php
header('Content-Disposition: attachment; filename='.basename($file));
$prog = $_GET[ 'prog' ];
echo "<title>$prog</title>\n</head><body ><pre>";
echo "$prog\n";
$Home = $_SERVER['DOCUMENT_ROOT'];
$path = "$Home" . "$prog";
readfile( "$path" );
?>
</pre> </Body> </html>
I am using "<pre>" but the php code in the "prog" is still executed.

Any ides on how to stop executing the php code in the downloaded file??

Thanks for your time.
 
Old 10-22-2012, 06:51 AM   #2
r0b0
Member
 
Registered: Aug 2004
Location: Europe
Posts: 608

Rep: Reputation: 50
I am sorry, I have a very hard time understanding your problem. You want to be able to view PHP files without them being executed as PHP by the server?
If your web server is configured to execute .php files, it will execute them. You can disable the PHP module for the whole web server or a part of it. Or you can rename the php files to some other extension so that they are not executed.
If I'm misunderstanding you, please re-phrase your question.
 
Old 10-22-2012, 07:35 AM   #3
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
readfile() does not execute your code provided that your $_SERVER['DOCUMENT_ROOT'] does not contain "something://". Can you paste example file and contents of received file?
 
Old 10-23-2012, 01:34 PM   #4
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
Hi guys,

My problem is not with the server, it is doing what it is supposed to. The problem is on the browser side. When the browser sees the "<?php" code in the data, it assumes that it should process that code.

If you go to: http://64.124.13.3/Fritzing/PerfBoard.htmlScroll down to "The Script" header and click on "View perf.php". When the page is loaded, there is code missing in the browser window but if you view the page source, all of the code is there. I have tried both Seamonkey ( Mozilla ) and Firefox. I am uncertain why the browser is executing any code, I thought that happened only on the server side?

All I want is to view the php code in a browser's window. I though by defining
Quote:
content="text/plain;
that would tell the browser "this is just plain text"?

Does that make my problem clearer? Sorry for the confusion.
 
Old 10-23-2012, 02:38 PM   #5
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
OK, guys. I have a fix, it is not pretty but here it is:
Change
Code:
readfile("$path");"
to:
Code:
$Lines = file( "$path" );
foreach ($Lines as $Line) {
    echo htmlspecialchars($Line);
}
 
Old 10-23-2012, 03:31 PM   #6
Noway2
Senior Member
 
Registered: Jul 2007
Distribution: Gentoo
Posts: 2,125

Rep: Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781
Quote:
When the browser sees the "<?php" code in the data, it assumes that it should process that code.
As an important point of clarification, the browser is not PHP aware and does not interpret or process your code. This is purely a server side function. The PHP interpreter on your server converts the code into HTML or rather generates the HTML from your PHP code. The reason your code is being executed is that any code between the <?php and ?> tags will be interpreted before it is passed through to the browser.

Your fix, reads the raw text file into strings, which you are then echoing, while the htmlspecialchars function converts things like quotes and ampersands which are part of the code, into entity references that will display in HTML (what is being echoed).
 
Old 10-23-2012, 03:50 PM   #7
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Just as a matter of interest, what are you trying to achieve that 'View Source' (or 'Source') in the browser doesn't do?
 
Old 10-23-2012, 05:56 PM   #8
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by salasi View Post
Just as a matter of interest, what are you trying to achieve that 'View Source' (or 'Source') in the browser doesn't do?
I am trying to give the user the option of viewing the php code in a browser window before downloading it. Many people are very paranoid about downloads.
 
Old 10-23-2012, 06:04 PM   #9
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by Noway2 View Post
As an important point of clarification, the browser is not PHP aware and does not interpret or process your code. This is purely a server side function. The PHP interpreter on your server converts the code into HTML or rather generates the HTML from your PHP code. The reason your code is being executed is that any code between the <?php and ?> tags will be interpreted before it is passed through to the browser.

Your fix, reads the raw text file into strings, which you are then echoing, while the htmlspecialchars function converts things like quotes and ampersands which are part of the code, into entity references that will display in HTML (what is being echoed).
Before this experience I would have agreed with you. Now I don't. The browser does do something with php code in a web page, as well as CSS and Java. Further testing is needed for me.
 
Old 10-24-2012, 03:42 AM   #10
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
Quote:
The browser does do something with php code in a web page...
Yes, it do something, but does not execute this code, bucause it just does not understand PHP, like Noway2 wrote ealier. CSS, Java (if special plugin is enabled), HTML, JS (if not blocked), but not PHP. What you saw on browser was an attempt to interpret this <?php ..... ?> as HTML tags. So, you didn't saw what was between this tags (because this is not html and was hidden), and saw the rest.

If you want to display unknown text in the browser you need to use htmlspecialchars() functions. All these special chars, like <, >, &, etc. are interpreted by browser and you did not see them like you want. Also maybe you been interested about a function highlight_file().

As you are beginner in PHP advise you to read about security, like filtering user data. For example
Code:
$prog = $_GET[ 'prog' ];
here a malicious user can read any file from your serwer.

Last edited by eSelix; 10-24-2012 at 03:55 AM.
 
Old 10-24-2012, 06:57 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Like eSelix and Noway2 said, php is purely a server side language.
If you try to actually deliver the php code to the browser it may try(!) to run it, but it won't know how.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] problems with PHP, showing code trying to "run" a php file? michaelinux Slackware 1 12-29-2010 10:21 PM
Postfixadmin PHP setup file - only displays PHP code davidmbecker Linux - Software 3 04-17-2008 10:33 AM
PHP: Stopping binary file uploads vxc69 Programming 2 10-02-2007 10:59 AM
HELP:Stopping execution of a program allomeen Programming 4 03-10-2006 06:42 PM
Stopping before the Execution of the startup files !! sachin77 Linux - Newbie 0 04-25-2001 03:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration