2017-08-23 132 views
-1

阻止其他網站發送數據到我的網站在PHP中的最佳方法是什麼?如何防止其他網站發送數據到我的網站在PHP

我把它搜索了一遍,發現我需要使用哈希。但是有不同的哈希,哪一個可以使用?比方說,我挑sha1

現在,我究竟能避免其他網站後的數據發送到我的網站sha1

我有點糊塗了,可有人告訴我一個小演示代碼。

這是代碼,我想,但它並非無懈可擊..

Index.php page:

$password = sha1("toby123"); 

<form method="post" action="insert.php" /> 
<input type="text" name="username"/> 
<input type="password" name="password"/> 
<input type="hidden" name="hiddenpass" value=" ".$password." "/> 
</form> 

插入到數據庫中的PHP頁面:

$hiddenpass = "toby123" 

if ( $_POST["hiddenpass"] == "sha1($hiddenpass)" ) 

{ 
// INSERT THE DATA 
} 

但這裏的問題是,表格中的代碼將對每個人都是可見的。如果有人破解它會怎麼樣?

我的意思是,通過使用命中&試驗方法???

我的方法是否100%安全?

編輯:

這是在尋找答案的一個後,我的新代碼,但如果條件永遠是假的。

index.php頁面

<?php 
// Start the session 
session_start(); 


    $token = bin2hex(random_bytes(32)); 

    if (!isset($_SESSION['token'])) { 
     $_SESSION['token'] = $token; 
    } 

?> 

Insert.php頁:

session_start(); 

echo $_SESSION['token']; 
echo '<br/>'; 
echo $_POST['token']; 

if ( (isset($_SESSION['token'])) && ($_POST['token'] == $_SESSION['token'])) 

{ 

// Insert Data 

} 
else 
echo "<br/>Something went wrong"; 


unset($_SESSION['token']); 

輸出:

055442be59701631db6ed88dc341027ebf2238507bb9a72f1caefd6d3b126a4b

055442be59701631db6ed88dc341027ebf2238507bb9a72f1caefd6d3b126a4b

出了錯

+5

您不能阻止他們以這種方式發送POST數據(到PHP運行時請求已發送),但您應該查找的術語是CSRF令牌,它們丟棄一個令牌。 –

+0

@JonStirling此評論應該是一個答案 – Philipp

+0

不確定。這不是一個答案,更多的是「你應該去研究這個術語」,這不是嚴格的答案。 –

回答

1

您應該通過添加使用CSRF令牌來保護您的表單。 CSRF令牌應該始終是隨機的。 CSRF代表(跨站請求Frogery)


這裏是一個很好的和安全的方法:

<?php 

function random_token() 
{ 

    $token = bin2hex(random_bytes(32)); 

    return $token; 
} 


function gentoken() 
{ 
    if (!isset($_SESSION['token'])) { 
     $_SESSION['token'] = random_token(); 
    } 
} 


function checktoken($token) 
{ 
    if (isset($_SESSION['token']) && $token === $_SESSION['token']) { 
     deletetoken(); 
     return true; 
    } 
    return false; // default 
} 


function deletetoken() 
{ 
    unset($_SESSION['token']); 
} 

?> 

而這裏應該是你的形式

<form method="post" action="insert.php"/> 
<input type="text" name="username"/> 
<input type="password" name="password"/> 
<input type = "hidden" name="token" value="' . $_SESSION['token'] . '"> 
<input type = "submit" name="submit" value="Insert"/> 
</form> 

當頁面開始時,寫下:

gentoken(); 

**And to check for the token do this** 



if (isset($_POST['submit'])) { 

    $token = $_POST['token']; 
    if (checktoken($token)) { 

    } else { 
     echo "Something wrong happened"; //When the token is changed or doesn't match! 
    } 

} 

注:

random_bytes()生成安全密碼學字節,並且不需要被散列!

我希望這有助於。祝你好運!

+0

我從來沒有在PHP中使用函數。但是我認爲這段代碼可能沒有功能,對吧?那麼爲什麼使用函數?爲什麼複雜的代碼。 – Toby

+1

@Toby我通常創建一個名爲functions.php的文件,其中使用了多於一頁的代碼,並且每當我需要使用它時,我只是將函數文件包含在另一頁中,而不是一遍又一遍地寫每次! –

+0

嗨idk爲什麼,但這個條件'if(isset($ _ SESSION ['token'])&& $ token === $ _SESSION ['token']){'總是false ... – Toby

0

答案很簡單,你不能。如果一個頁面可以在公共互聯網上查看,那麼數據可以發佈給它。

實施合理的數據衛生和驗證以保護應用程序的完整性由您決定。首先,不要在表單中放置散列密碼,即使隱藏字段也是如此!存儲哈希密碼並散列客戶端發送的任何值。這樣,您可以比較密碼哈希值而不暴露它們。

-1

而不是使用「toby123」使用很多cmplex和長隨機生成的字符串。 爲了更安全,您可以使用兩個哈希值。

session_start(); 
// if there is data submitted 
if(isset($_POST['submit'])){ 
$password1 = $_SESSION['password1']; 
$password2 = $_SESSION['password2']; 
//check these passwords 
if($_POST['password1']==$password1 && $_POST['password2']==$password2){ 
// code to pe processed 

}else{ 
echo '<h1>FORBIDDEN</h1>'; 
} 
}else{ 
$rand1 = md5(uniqid().mt_rand()); 
$rand1 = md5(uniqid().mt_rand().uniqid()); 
$password1 = sha1($rand1); 
$password2 = sha1($rand2); 
$_SESSION['password1'] = $password1; 
$_SESSION['password2'] = $password2; 
} 
+0

不安全:mt_rand不再安全,它甚至不會生成安全密碼字節,並且能夠通過強力破解和預測! –

+0

另外,md5不是散列的好方法! –

相關問題