Welcome to WebmasterWorld Guest from 54.227.231.144

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

echoing php within itself

echo and fread()

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

5+ Year Member



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

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

10+ Year Member



I think eval [us2.php.net] will help.
3:13 pm on Jul 14, 2009 (gmt 0)

5+ Year Member



thanks for your post.

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

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

5+ Year Member



here is the code


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

does anybody know why it is so slow?

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

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

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

5+ Year Member



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?

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

WebmasterWorld Senior Member 5+ Year Member



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]

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

WebmasterWorld Senior Member 5+ Year Member



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.