我试图将php代码放入php变量,然后将其写入文件。但它不起作用。由于它包含标签,] [?空间,新线等 我试过用EOD没用。
这是我的代码:
<?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');
?>
所以基本上我用新的PHP代码刷新spams.php文件。但是我无法正确地将php代码写入文件。
如果我将第5行更改为:
$content01 = '<?php session_start(); if(!isset($_SESSION["UserData"]["Username"])){ header("location:login.php"); exit;}?>';
应用程序将写入spams.php,但重定向(header())不再起作用。
是否有任何想法将包含php代码的多行值放入变量?
答案 0 :(得分:1)
你可以尝试创建一个字符串数组,这样你就可以简单地implode数组。这样您就可以获得更好的格式化代码。我只是用你的PHP做了这个,你也可以用你的HTML做到这一点。
顺便说一句,我为我的测试更改了header("Location: ....spams.php");
的路径,只需将其更改回来并尝试以下代码:
$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
spams.php 将如下所示:
<?php
session_start();
if( !isset( $_SESSION["UserData"]["Username"] ) ) {
header("Location: login.php");
exit;
}
?>
<!DOCTYPE html><html><head><title>IrNut Spa.......
答案 1 :(得分:0)
这有用吗?
$content01 =
"<?php \n" .
"session_start(); \n" .
"if (!isset($_SESSION['UserData']['Username'])) \n" .
"{ \n" .
"header('location:login.php'); \n" .
"exit; \n" .
"}?> \n"
;