我使用 post 方法从表单中获取数据,我想使用该值将该值传递到另一个页面,但它不起作用。将 POST 重定向到 GET

问题描述 投票:0回答:1

请帮我纠正这个代码

$salt = 'XyZzy12*_';
$stored_hash = '1a52e17fa899cf40fb04cfc42e6352f1';
//$stored_hash = 'a8609e8d62c043243c4e201cbb342862';  // Pw is meow123
$salted = md5($salt);
$failure = false;  // If we have no POST data

// Check to see if we have some POST data, if we do process it
if ( isset($_POST['who']) && isset($_POST['pass']) ) {
    if ( strlen($_POST['who']) < 1 || strlen($_POST['pass']) < 1 ) {
        $failure = "User name and password are required";
        
    } else {
        $check = hash('md5', $salted.$_POST['pass']);
        //print "$check";
        if ( $check == $stored_hash ) {
            // Redirect the browser to game.php
            header("Location: game.php?name=".urlencode($_POST['who']) );
            return ;
        } else {
            $failure = "Incorrect password";
        }
    }
}

第一个stored_hash不起作用,当我放置第二个时,出现错误,说期望POST重定向到GET,但收到了POST

我尝试仅传递页面 game.php 但它仍然无法正常工作,当我查看我的研究中的代码时,它似乎是正确的

php forms header salt-cryptography
1个回答
0
投票

$salted
变量包含盐的MD5哈希值,这似乎不正确。相反,您应该直接将盐与密码连接起来,然后对其进行哈希处理。 另外,您似乎错误地使用了 hasg,对于 md5,您可以使用
md5()
而不是
hash('md5', ...)
。 在我看来,给定的哈希值似乎不匹配

我会建议这样的事情:

$salt = 'XyZzy12*_';
$stored_hash = ''; // TODO: Input your stored hash here..
$failure = false;  // If there should be no POST data

// If there is POST data, process it
if (isset($_POST['who']) && isset($_POST['pass'])) {
    if (strlen($_POST['who']) < 1 || strlen($_POST['pass']) < 1) {
        $failure = "User name and password are required";
    } else {
        // Correctly concatenate the salt and password, then hash
        $check = md5($salt . $_POST['pass']);
        if ($check == $stored_hash) {
            // Redirect the browser to game.php
            header("Location: game.php?name=" . urlencode($_POST['who']));
            exit; // Use exit after header to prevent further code execution
        } else {
            $failure = "Incorrect password";
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.