so i have xampp installed on windows the address is localhost and i have a page test.php to preview my code
a couple days ago everything worked fine when echoing

<?php
echo '<'.htmlentities('?php if ($_SESSION[\'Username\']==\'nume\') { echo \'right name\'; } else { echo \'wrong name\'; } ?').'>';
?>

and when i tried to test it yesterday the page showing is blank why?

Recommended Answers

All 8 Replies

Write the code out properly:

<?php

if ($_SESSION["Username"] == "name") {
    echo "Right Name";
} else {
    echo "Wrong name";
}

?>

I swapped 'nume' to 'name' I don't know if it was meant to be like that?

Mat

if (htmlentities($_SESSION["username"]) == "name"

If you want to use htmlentities.

Member Avatar for diafol
echo '<'.htmlentities('?php if ($_SESSION[\'Username\']==\'nume\') { echo \'right name\'; } else { echo \'wrong name\'; } ?').'>';

This will echo an xml-type tag to the page <...>
So of course, it won't be displayed, but if you look in the browser's "view source", you should see it.

Not sure what output you need or want with this. Are you looking to display php code to the screen?

If so, here's something I knocked up that you could modify...

function codify($str)
{
    //newline -> <br />, \t, tabs and 4 spaces to 4 non-breaking spaces 
    return '<code>' . str_replace(array("\t",'   ','    '),'&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(htmlentities($str))) . '</code>';
}

This lets you work with nowdoc and heredoc syntaxes...

NOWDOC
$str = <<<'CODE'
<?php
if($_SESSION['Username']=='nume')
{
    echo 'right name';
}else{
    echo 'wrong name';
}
?>
CODE;
HEREDOC
$str2 = <<<CODE2
<?php
if(\$_SESSION['Username']=='nume')
{
    echo 'right name';
}else{
\techo 'wrong name';
}
?>
CODE2;

You can test them like this...

echo codify($str);
echo '<br />';
echo codify($str2);

the point is that i want to echo it out as php code because i put the string into the "fwrite" function to write an entire php page

Member Avatar for diafol

I think I'm missing something here. You want to write the formatted/converted output to a file? For what purpose? I don't understand why you need to do this.

for writing an entire page with the code

Member Avatar for diafol

So do you want the content of the file to be 'runnable' as php code or should it be 'htmlentitied', so it actually looks like this...

&lt;?php
    ...
?&gt;

i will leave this like this right now because i made a script to auto write the code for testing thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.