1

I am trying to put a php code into a php variable and then write it to a file. But it does not work. Since it contains tabs, ][? space, new line etc. I tried to use EOD did not work.

Here is my code:

<?php
ini_set('display_errors',1);  
error_reporting(E_ALL);
$content02 = "\n";
$content01 = <<<EOD
<?php session_start();

if(!isset($_SESSION["UserData"]["Username"])){
   header("location:login.php");
   exit;
   }
?>
EOD;
$content03='<!DOCTYPE html><html><head><title>Spambox</title></head><body><html dir="rtl"></html><form action="flushsink.php" method="get"><input type="submit" value="Flush!"></form>';
$content4 = $content01.$content02.$content03;
function writeUTF8File($filename,$content) {
    $f=fopen($filename,"w");
    fwrite($f,$content);
    fclose($f);
}
writeUTF8File("spams.php",$content4);
header('Location: http://targeturl.com/int/spams.php');
?>

So basically I am flushing a spams.php file with new php code. But I can not write the php code to the file properly.

If I change the line 5 to :

$content01 = '<?php session_start(); if(!isset($_SESSION["UserData"]["Username"])){ header("location:login.php");   exit;}?>';

The application will write to the spams.php but the redirection (header()) does not work any more.

Any idea to put multiple line values containing php code to a variable?

2 Answers 2

1

You could try to create an array of strings, so you can simply implode the array. This way you can achive a better formatted code. I just did this for example with your PHP, you can also do this with your HTML.

By the way, I changed the path of your header("Location: ....spams.php"); for my test, just change it back and try the code below:

$content = array();
$content[] = '<?php';
$content[] = 'session_start();';
$content[] = 'if( !isset( $_SESSION["UserData"]["Username"] ) ) {';
$content[] = '  header("Location: login.php");';
$content[] = '  exit;';
$content[] = '}';
$content[] = '?>';
// for better readability you can also split the html below, like I did with the PHP above
$content[] = '<!DOCTYPE html><html><head><title>IrNut Spambox</title></head><body><html dir="rtl"></html><form action="flushsink.php" method="get"><input type="submit" value="Flush!"></form>';

// implode array to string, glue will be a \n
$content = implode( "\n", $content );

function writeUTF8File($filename,$content) {
    $f=fopen($filename,"w");
    fwrite($f,$content);
    fclose($f);
}
writeUTF8File( "spams.php", $content) ;
header('Location: spams.php');// just changed the path to fit my test

The spams.php will look like this:

<?php
session_start();
if( !isset( $_SESSION["UserData"]["Username"] ) ) {
    header("Location: login.php");
    exit;
}
?>
<!DOCTYPE html><html><head><title>IrNut Spa.......
Sign up to request clarification or add additional context in comments.

1 Comment

Glad that I could help :)
0

Does this work?

$content01 =
    "<?php \n" .
    "session_start(); \n" .
    "if (!isset($_SESSION['UserData']['Username'])) \n" .
    "{ \n" .
    "header('location:login.php'); \n" .
    "exit; \n" .
    "}?> \n"
    ;

2 Comments

Nope. PHP Syntax Check: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in your code on line 8 "if (!isset($_SESSION['UserData']['Username'])) \n" . PHP Syntax Check: Errors parsing your code
If you re-type the apostrophes, does it work? (Copying-and-pasting the apostrophes may be an issue.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.