2011-03-12 30 views
0

我有一個表單文本框,標識爲username。 當按下提交按鈕將此代碼用於:使用PHP腳本更改PHP文件的一個字

<?php 
if (isset($_POST['submit'])) { 
$myUser=$_POST['username']; 
$path="/data/www/vhosts/themacsplash.com/httpdocs/ClipBoy/userfiles/"; 
$fpath="/data/www/vhosts/themacsplash.com/httpdocs/ClipBoy/userfiles/user.php"; 
$myFolder = $path . $myUser; 
$myFile = $myUser.".php"; 
$old_umask = umask(0); 
mkdir($myFolder, 0777); 
$myFileDes = $myFolder."/".$myFile; 
copy($fpath, $myFileDes); 
umask($old_mask); 
} 
?> 

這將創建一個目錄和文件,該文件是從user.php的複製在user.php的有這個代碼,需要在提交更改:

$result = mysql_query("SELECT link, notes FROM links WHERE username='will';"); 

它需要改變的地方說:username='will'; 它需要改變意願的形式定義的用戶名。

我該怎麼做?

+0

這可以幫助:http://php.net/manual/en/language.variables.php – seriousdev 2011-03-12 17:18:35

+0

你也許可以做的fopen和緩衝的內容到PHP,然後搜索該查詢語句,做修改,然後寫入它並用編輯的內容替換以前的內容。 – kjy112 2011-03-12 17:20:04

+1

這在結構上聽起來像是一個可能不好的主意。將數據庫憑證存儲在與查詢分開的文件中可能更爲可行。 – 2011-03-12 17:20:09

回答

1

您可以從文件所在的目錄中動態獲取用戶名。使用dirname__FILE__獲得:

$username = dirname(__FILE__); 
$username = mysql_real_escape_string($username); 
$result = mysql_query("SELECT link, notes FROM links WHERE username='" . $username . "';"); 

此外,我會建議你過濾來自用戶的輸入:

$myUser = preg_replace('/[^a-zA-Z0-9_]/', '', $myUser); 

這將由空替換每個字符就是沒有字母數字字符或下劃線串。否則,用戶可以選擇"../../../foo/"作爲可能會破壞腳本的用戶名。

0

您需要先閱讀原始user.php內容,然後用您想要的值替換字符串。

$content = "";    // read Text file, read all lines into an array 
if ($arr = @file ($fpath))  // Do this if file exists 
    foreach ($arr as $line) $content .= $line.'\n'; 

$fp = fopen ($myFileDes, "w"); // create new file 
$newContent = str_replace("'will'", $myUser, $content); 
fputs ($fp, $newContent); 
fclose ($fp); 
+0

-1你可以使用'file_get_contents()'而不是'file()' – levu 2011-03-12 17:27:34