2012-02-11 40 views
0

目前,我在HTML端代碼如下所示:在數據庫中發佈信息的更好方法?

<form action="newstory.php" method="post"> 
<input type="hidden" name="author" value="<?php echo $loggedInUser->display_username; ?>" 
/> 
<input type="hidden" name="userid" value="<?php echo $loggedInUser->user_id ?>" /> 
Story Title: <input type="text" name="story_name" /><br> 
Story: <textarea rows="10" cols="30" name="story" /></textarea><br> 
<input type="submit" /> 
</form> 

這裏的PHP端:

include("dbconnect.php"); 

mysql_select_db("scratch", $con); 

$author  = mysql_real_escape_string($_POST['author']); 
$author_id = mysql_real_escape_string($_POST['userid']); 
$story_name = mysql_real_escape_string($_POST['story_name']); 
$story  = mysql_real_escape_string($_POST['story']); 

$sql= " 
INSERT INTO stories (author, author_id, story_name, story) 
VALUES ('$author', '$author_id','$story_name', '$story') 
"; 


if (!mysql_query($sql,$con)) 
{ 
die('Error: ' . mysql_error()); 
} 
echo "Story Submitted! Redirecting to Homepage..."; 
//User is shown this for about 3 seconds 
header('Refresh: 3; URL=index.php'); 

mysql_close($con) 

我想擺脫的

<input type="hidden" name="author" value="<?php echo $loggedInUser->display_username; ? 
>"/> 

這樣人們就可以輕鬆編輯和發佈任何用戶,但我不確定一個好方法。用戶標識也一樣。

幫助表示讚賞!

+6

如果用戶已登錄,請使用會話存儲其ID並將其插入數據庫。 – 2012-02-11 20:24:35

+2

查找PDO,而不要在你使用mysql_ *函數時使用它。 – 2012-02-11 20:37:47

+0

不要以形式發送userdata ..使用'$ _SESSION'數據進行身份驗證 – Richard 2012-02-11 20:42:40

回答

-1

檢查用戶是否已設置,並且只要用戶對象有效就顯示該表單。在使用mysql保存之前,使用用戶對象的值,而不是從POST數據中讀取值。

<?php if (isset($_POST['story_name'])) { 
// story posted.. check if user is set 
if (isset($loggedInUser->user_id)) { 
    // save into database using $loggedInUser->user_id and $loggedInUser->author_name 
} 
?> 

<?php 
// just show the form if the user object is set 
if (isset($loggedInUser->user_id)){ 
?> 
<form> <!-- and show the form over here --> </form> 
<?php } ?> 

哦,你的mysql_real_escape_string()是好的!另一個最佳做法是將變量添加到使用sprintf()查詢:

$author  = mysql_real_escape_string($loggedInUser->author_name); 
$author_id = mysql_real_escape_string($loggedInUser->user_id); 
$story_name = mysql_real_escape_string($_POST['story_name']); 
$story  = mysql_real_escape_string($_POST['story']); 

$sql= sprintf(" 
INSERT INTO stories (author, author_id, story_name, story) 
VALUES ('%s', '%s', '%s', '%s') 
", $author, $author_id, $story_name, $story); // %s accepts the value to be a string. %d accepts a decimal for example. 
+0

您不應在請求中發送userdata,因爲它可以被操縱。任何人都可以在瀏覽器中編輯隱藏字段並將其作爲其他人發佈。 – Richard 2012-02-11 20:44:06

+0

你說得對。在第二行代碼塊中,我在上面的第4行進行了編輯,但沒有進行編輯。 – user1204156 2012-02-11 21:14:17

1

通過隱藏的輸入字段的形式發送userid是一個巨大的安全威脅。任何人都可以用例如Chrome的檢查員或FireBug。當有人登錄時;您必須至少將他們的user_id存儲在會話中。您還可以在會話中存儲更多信息,以便您不必在每次請求時都查詢數據庫,以便在頁面上的某處顯示登錄用戶的用戶名。

我不知道您目前如何處理登錄,但我不知道$loggedInUser是如何填充的,但它應該是會話變量,例如, $_SESSION['user']['id']。這樣你就可以知道用戶是誰,而不必通過表單發送數據;這是一個真正的不行。

請務必在每個頁面頂部有session_start(),理想情況下,您需要使用模板,並且只需將session_start()添加到index.php的頂部。

而且

$sql= " 
INSERT INTO stories (author, author_id, story_name, story) 
VALUES ('$author', '$author_id','$story_name', '$story') 
"; 

至少應該

$sql= " 
INSERT INTO stories (author, author_id, story_name, story) 
VALUES ('". $author ."', '". $author_id ."', '". $story_name ."', '". $story ."') 
"; 

而且我個人建議:

$q = " 
INSERT INTO stories 
     SET author_id = ". $_SESSION['user']['id'] ." # This is an integer (I assume) so don't use apostrophe's 
      , story_name = '". mysql_real_escape_string($_POST['story_name']) ."' 
      , story = '". mysql_real_escape_string($_POST['story']) ."' 
"; 

從表中刪除字段author。只需使用author_id進行表引用,否則您將存儲重複數據,並且當有人更改其作者姓名時,故事中的作者姓名是過時/不正確/過時。

+0

更新了我的回答 – Richard 2012-02-11 21:32:24

+0

這樣排序是否存在問題?附:我在你的代碼中看到'mysql_close($ con)'在行尾沒有分號。在PHP關閉之前的最後一行之後的分號是可選的,但如果您決定在下面添加更多代碼,則會導致錯誤:-) – Richard 2012-02-11 22:11:41