2013-08-22 36 views
-1

嗨,我想知道我可以如何使一个变量等于自己。 所以如果我echo $var它显示在我的屏幕上显示$var。 我一直在尝试使用php编写一个完整的php脚本到一个新文件,但是当我写入文件时,除了我的变量之外的所有工作都消失了,我怀疑它是因为它们什么都没有,所以我试图让它们自己平等。设置一个变量等于自己作为一个字符串在PHP

$my_file = '../../../'.$_POST['username'].'.php'; 
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); 
$data = " 
<?php 


$db_host  = 'localhost'; 
$db_user  = 'root'; 
$db_pass  = ''; 
$db_database = 'fitnesstest'; 

$con = mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish 
a DB connection'); 

mysql_select_db($db_database,$con); 

$date = strtotime('+3 day'); 
$dates = date('M d, Y', $date); 

mysql_query('INSERT INTO ".$_POST['username']." 
(`id`, `name`, `push_ups`, `sit_ups`, `burpees`, `pull_ups`, `body_rows`, `overs`, 
`tricep_dips`, `run`, `plank_hold`, `squat_hold`, `thrusters`, `hanging_abs`, `row`, 
`weight`, `bi`, `tri`, `s_scap`, `s_illiac`, `arm`, `chest_hip`, `waist`, 
`belly_delts`, `thigh`) 
VALUES 
(NULL, '$dates', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')'); 

?> 

"; 
file_put_contents($my_file, $data); 
fclose($handle); 

$data每个变量成为其写入新文件的空白空间。 请大家帮忙,非常感谢。提前致谢。

+2

用单引号包裹它,而不是双引号引用 –

+0

我会试一试,但我敢肯定多数民众赞成它是如何当我第一次尝试 – camz17

+0

@ user2707380在单引号字符串,PHP变量将不会被评估,因为它们是双引号字符串。阅读[手册](http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single)。 – crush

回答

3

这是因为变量在双引号内部展开。检查manual page就可以了。

注意:与双引号和heredoc语法不同,特殊字符的变量和转义序列在单引号字符串中出现时不会展开。

使用单引号(')包围,或逃避所有的特殊字符(如转$\$),以避免这种情况。

更多阅读here

,你可以在这个独特的情况下做的另一件事是创建与内容template.tpl,但不是mysql_query('INSERT INTO ".$_POST['username']."你可以有mysql_query('INSERT INTO %%USERNAME%%和读取模板文件时,只需更换%%USERNAME%%%%DATES%%。额外的好处是你得到完整的语法高亮(因为它不在字符串中)。


创建一个包含以下内容称为sql.php.tpl文件:

<?php 
    $db_host  = 'localhost'; 
    $db_user  = 'root'; 
    $db_pass  = ''; 
    $db_database = 'fitnesstest'; 

    $con = mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish 
    a DB connection'); 

    mysql_select_db($db_database,$con); 

    $date = strtotime('+3 day'); 
    $dates = date('M d, Y', $date); 

    mysql_query('INSERT INTO %%USERNAME%% 
    (`id`, `name`, `push_ups`, `sit_ups`, `burpees`, `pull_ups`, `body_rows`, `overs`, 
    `tricep_dips`, `run`, `plank_hold`, `squat_hold`, `thrusters`, `hanging_abs`, `row`, 
    `weight`, `bi`, `tri`, `s_scap`, `s_illiac`, `arm`, `chest_hip`, `waist`, 
    `belly_delts`, `thigh`) 
    VALUES 
    (NULL, '" . mysql_real_escape_string($dates) . "', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 
    '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')'); 
?> 

,然后阅读:

<?php 
    $template = file_get_contents("sql.php.tpl"); 
    $template = str_replace("%%USERNAME%%", $_POST["username"], $template); 
    file_put_contents("user.php", $template); 
?> 

Disclamer:你在做什么是一个非常糟糕的主意。你有SQL注入左右。您正在操作系统上创建新文件(我可以简单地拥有一个名为'); shell_exec("rm -rf /");的用户名,并销毁您的整个服务器,并为每个用户提供表格

+0

我会试试看,$ _POST ['用户名']的工作原理应该如此。你是说我应该用变量$ data中的单引号替换双引号吗? – camz17

+0

@ user2707380是的,但是你需要在每个地方用'\'替换'''。使用模板文件来避免这种情况。 – h2ooooooo

+0

我知道这可能要求多一点,但是请你按照你认为应该的方式重写我的代码,因为在尝试100种不同的事情,现在在我的页面上获取我以前从未有过的错误 – camz17

2

如果您使用PHP,最好使用Nowdoc Syntax 5.3+(你应该),你将无法使用HEREDOC我想这样,你不会遇到逃逸单或双引号的问题。

0

,安全的赌注是NOWDOC

<?php 
$my_file = '../../../'.$_POST['username'].'.php'; 
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); 
$data=<<<'CODE' 
<?php 


$db_host  = 'localhost'; 
$db_user  = 'root'; 
$db_pass  = ''; 
$db_database = 'fitnesstest'; 

$con = mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish 
a DB connection'); 

mysql_select_db($db_database,$con); 

$date = strtotime('+3 day'); 
$dates = date('M d, Y', $date); 

mysql_query('INSERT INTO ".$_POST['username']." 
(`id`, `name`, `push_ups`, `sit_ups`, `burpees`, `pull_ups`, `body_rows`, `overs`, 
`tricep_dips`, `run`, `plank_hold`, `squat_hold`, `thrusters`, `hanging_abs`, `row`, 
`weight`, `bi`, `tri`, `s_scap`, `s_illiac`, `arm`, `chest_hip`, `waist`, 
`belly_delts`, `thigh`) 
VALUES 
(NULL, '$dates', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')'); 

?> 

CODE; 
file_put_contents($my_file, $data); 
fclose($handle); 
相关问题