2014-02-28 66 views
0

我有一個PHP哈希方法,我需要從我的C#程序調用以檢查我的數據庫的密碼。這裏是PHP代碼:C#&PHP - 調用返回值的方法

<?php 
function generateHash($text, $salt = null) 
{ 
    if ($salt === null) 
    { 
     $salt = substr(md5(uniqid(rand(), true)), 0, 25); 
    } 
    else 
    { 
     $salt = substr($salt, 0, 25); 
    } 
    return $salt.sha1($salt.$text); 
} 
?> 

我想設置的變量$文本從C#的東西,並使用Web客戶端來獲取返回的值。這裏是C#代碼:

WebClient c = new WebClient(); 

string r = c.DownloadString("http://www.example.com/hash.php?text=" + pass); 
return r; 

我是新來的PHP,並不真正知道如何解決這個問題。我在網上搜索,什麼都沒找到。

感謝您提前提供任何幫助!

回答

2

在你的PHP代碼,你可以使用$_GET超全局數組訪問GET參數,就像這樣:

if (isset($_GET['text'] && !empty($text)) 
{ 
    echo generateHash($text); 
} 
else 
{ 
    echo 'Sorry, invalid request.'; 
} 

每當客戶端發送一個GET請求這個PHP腳本與text參數時,上面的代碼會生成一個散列並將其返回。返回的散列可以用於你的C#代碼(我不知道C#,所以我不打算告訴你如何)。

+0

工作很好!謝謝 :) – Zilent

0
<?php 
function generateHash($text, $salt = null) 
{ 
    if ($salt === null) 
    { 
     $salt = substr(md5(uniqid(rand(), true)), 0, 25); 
    } 
    else 
    { 
     $salt = substr($salt, 0, 25); 
    } 

    return $salt . sha1($salt . $text); 
} 

$text = $_GET["text"]; 
echo generateHash($text); 
?>