homepage Welcome to WebmasterWorld Guest from 54.205.52.110
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
echoing php within itself
echo and fread()
jharker1987




msg:3952241
 1:43 pm on Jul 14, 2009 (gmt 0)

Hi all

I am building a very simple articles based website and I want to create a single page that displays the associated article.

ill let the code do the talking as its difficult to explain.


if(isset($_GET['id']))
{
$articleID = $_GET['id'];
$query = "SELECT * FROM ps_articles WHERE ID='$articleID'";
$result = mysql_query($query);
$num = mysql_num_rows($result);

if ($num == 0)
{
echo('<p>The selected article was not found. <a href="index.php">Back to open source</a></p>');
}
else
{
GetArticleSource($articleID, $result);
}
}
else
{
header("Location:index.php");
}

function GetArticleSource($id)
{
$file=fopen("articles/" . $id . "/p1.php","r") or exit("<p>Unable to get the selected article.</p>");
while(!feof($file))
{
echo fgets($file);
}
fclose($file);
}

Ok here goes.
The php file (articles/1/p1.php for example) itself contains php. but when its echo'd, its echo'd as HTML and not as php so it just appears as a load of random text on the page. what I want is for the page to be displayed as is and php code to be parsed as-is. How can I go about doing this?

thanks

 

Mike12345




msg:3952248
 2:02 pm on Jul 14, 2009 (gmt 0)

I think eval [us2.php.net] will help.

jharker1987




msg:3952272
 3:13 pm on Jul 14, 2009 (gmt 0)

thanks for your post.

i have implemented this but found it to be painfully slow! i think ill try a different route

jharker1987




msg:3952278
 3:22 pm on Jul 14, 2009 (gmt 0)

here is the code


while(!feof($file))
{
eval("" . fgets($file) . ";");
}
fclose($file);

does anybody know why it is so slow?

coopster




msg:3952282
 3:30 pm on Jul 14, 2009 (gmt 0)

Welcome to WebmasterWorld, jharker1987.

Use include instead of fopen in your function. Check to see if the file exists and if so, include it. The included code will be parsed.

jharker1987




msg:3952327
 4:14 pm on Jul 14, 2009 (gmt 0)

ok this is beginning to work now.

here is what ive got


$file=fopen("articles/" . $id . "/p" . $id . "_php.php","r") or exit("<p>Unable to get the selected article.</p>");

$str = "";
while(!feof($file))
{
$str .= fgets($file) . ";";
}
fclose($file);
eval($str);


$file=fopen("articles/" . $id . "/p" . $id . ".php","r") or exit("<p>Unable to get the selected article.</p>");
while(!feof($file))
{
$line = "". fgets($file);

if (strlen(strstr($line,"<?php"))>0)
{
eval ($line . ";");
}
else
{
echo("" . $line);
}

}
fclose($file);

the first loop appears to work correctly.

the second loop is for the actual body of the html code. within, there are little php snippets, here is an example:


<div class="curlycontainer">
<div class="innerdiv">
<?php GetCode3();?>
</div>
</div>

so the second loop looks for the php code, if found, does eval otherwise does echo. although it seems to work kinda (there is no php code in the source of the document) just blank spaces appear where the result should be.

where am i going wrong?

andrewsmd




msg:3952479
 7:37 pm on Jul 14, 2009 (gmt 0)

Try this I actually echoed the contents of another php file.

$file = file("yourFileHere.extension");

foreach($file as $i){

echo(htmlentities($i)."<br>");

}//foreach

[edited by: andrewsmd at 7:42 pm (utc) on July 14, 2009]

andrewsmd




msg:3952481
 7:41 pm on Jul 14, 2009 (gmt 0)

I just noticed in your original code if you put

function GetArticleSource($id)
{
$file=fopen("articles/" . $id . "/p1.php","r") or exit("<p>Unable to get the selected article.</p>");
while(!feof($file))
{
echo htmlentities(fgets($file));
}
fclose($file);
}
I think that should also work. Unless it wasn't working for some other reason. But reading the way in my previous code is a lot easier as long as you can base your output on carriage returns within the file. Thats how the file() function works. It creates an array with each line as a separate value. Let me know if you need anymore help, I had to work alot with most of the file functions at my previous job.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved