2013-08-22 39 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); 
相關問題