从ASP到PHP的Facebook身份验证脚本

时间:2009-11-24 16:43:17

标签: php authentication facebook signature

我试图将下面的代码转换为php版本,如果有人可以帮助谢谢。

private bool IsValidFacebookSignature()
    {
        //keys must remain in alphabetical order
        string[] keyArray = { "expires", "session_key", "ss", "user" };
        string signature = "";

        foreach (string key in keyArray)
            signature += string.Format("{0}={1}", key, GetFacebookCookie(key));

        signature += SecretKey; //your secret key issued by FB

        MD5 md5 = MD5.Create();
        byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(signature.Trim()));

        StringBuilder sb = new StringBuilder();
        foreach (byte hashByte in hash)
            sb.Append(hashByte.ToString("x2", CultureInfo.InvariantCulture));

        return (GetFacebookCookie("") == sb.ToString());
    }

    private string GetFacebookCookie(string cookieName)
    {
        //APIKey issued by FB
        string fullCookie = string.IsNullOrEmpty(cookieName) ? ApiKey : ApiKey + "_" + cookieName;

        return Request.Cookies[fullCookie].Value;
    }

1 个答案:

答案 0 :(得分:0)

这是未经测试的,但试试这个:

function IsValidFacebookSignature() {
    $keyArray = array( 'expires', 'session_key', 'ss', 'user' );
    $signature = '';

    foreach( $key in $keyArray ) {
        $signature .= "$key=".GetFacebookCookie($key);
    }

    $signature .= $SecretKey;
    $hash = md5(trim($signature));

    return GetFacebookCookie('') == $hash;
}

function GetFacebookCookie($cookieName) {
    $fullCookie = empty($cookie) ? $APIKey : $APIKey . '_' . $cookieName;
    return $_COOKIES[$fullCookie];
}

我不确定您希望在哪里声明$SecretKey$APIKey,但这是基本想法。