Jump to content

escape <?php ?> tags


dsaba

Recommended Posts

hey I am using this line of code to write to a file, I'm trying to create a php file

I need to escape the php tags, how do I do that?

 

I tried doing this:

 

<?php

$startphp = "<?php";
$closephp = "?>";
$htmlforfile = <<<HTMLFORFILE
$startphp
session_start();
if ($_SESSION['translation'] != "jamba-translation") {
die("not allowed");
}
$closephp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body>$text</body></html>
HTMLFORFILE;

?>

 

 

 

as you can see i'm using heredoc syntax because i'm using this one variable like this:

 

fwrite($handle, $htmlforfile) or die("could not write to the file");

 

$htmlforfile is the heredoc variable i created

 

 

you see the general idea is that I want to write to a file and let it be a php file

How do I do this?

-thanks

Link to comment
Share on other sites

I dont want to strip tags, i just want to escape the tags so I can write them in a php script without the php script acting like I want to use them

 

for example if I write ?> it will end the php script

 

 

if i escape_tags(?>)  then it will escape the tags get it?

 

 

its like escaping quotes, doesn't strip them just lets u use them

 

the problem I DONT KNOW OF ANY ESCAPE TAGS function

 

maybe there is another method to do this if there is no such function?

 

-thanks

Link to comment
Share on other sites

this code gives me a parse error:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

 

 

 

 

$htmlforfile = <<<HTMLFORFILE
<?php
session_start();
if ($_SESSION['translation'] != "jamba-translation") {
die("not allowed");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body>$text</body></html>'
HTMLFORFILE;

 

 

if there is nothing wrong with this code, then that means that the HEREDOC did not escape the php tags

its also obvious because in my dreamweaver highlighting its all wrong, as if ITS NOT ESCAPING TAGS

Link to comment
Share on other sites

Oh.. I see.

 

It's not the php tags that are your problem.  The heredoc syntax is not escaping php variables, that is what is causing your problem.  Try this:

 

<?php
$htmlforfile = <<<HEREDOC
<?php
session_start();
if (\$_SESSION['translation'] != "jamba-translation") {
die("not allowed");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body>\$text</body></html>'
HEREDOC;
print $htmlforfile;
?>

 

The difference is that I have escaped the "$" signs.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.