1

我在这个网站上问了一些关于这段代码的问题。

基本上,当我使用 - $id = $GET_['id']; 时,我的数据库没有更新 (在下面代码的顶部)。id 从上一页传递到此页面 - 页面的 url 是“http://www.21orange.com/CCC/changepassword.php?id=1”。我的数据库中有一个“id”字段。

当我将上面的代码行更改为 - $id = '1' - 代码运行完美并且数据库被更新。只有当我使用 $GET_['id'] 时它才会停止工作。为什么是这样?

// First we execute our common code to connection to the database and start the session 
require("common.php"); 

  $id = $_GET['id'];

// This if statement checks to determine whether the registration form has been submitted 
// If it has, then the registration code is run, otherwise the form is displayed 
if(!empty($_POST)) 
{  
    // Ensure that the user has entered a non-empty password 
    if(empty($_POST['password'])) 
    { 
        die("Please enter a password."); 
    } 

    // Ensure that the user has entered a non-empty username 
    if(empty($_POST['confirmpassword'])) 
    { 
        // Note that die() is generally a terrible way of handling user errors 
        // like this.  It is much better to display the error with the form 
        // and allow the user to correct their mistake.  However, that is an 
        // exercise for you to implement yourself. 
        die("Please confirm your password."); 
    } 

     if ($_POST['password'] == $_POST['confirmpassword']) {

    // An INSERT query is used to add new rows to a database table. 
    // Again, we are using special tokens (technically called parameters) to 
    // protect against SQL injection attacks. 
    $query = "UPDATE Staff SET password=:password, salt=:salt WHERE id=:id"; 

    // A salt is randomly generated here to protect again brute force attacks 
    // and rainbow table attacks.  The following statement generates a hex 
    // representation of an 8 byte salt.  Representing this in hex provides 
    // no additional security, but makes it easier for humans to read. 
    $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647)); 

    // This hashes the password with the salt so that it can be stored securely 
    // in your database.  The output of this next statement is a 64 byte hex 
    // string representing the 32 byte sha256 hash of the password.  The original 
    // password cannot be recovered from the hash. 
    $password = hash('sha256', $_POST['password'] . $salt); 

    // Next we hash the hash value 65536 more times.  The purpose of this is to 
    // protect against brute force attacks.  Now an attacker must compute the hash 65537 
    // times for each guess they make against a password, whereas if the password 
    // were hashed only once the attacker would have been able to make 65537 different  
    // guesses in the same amount of time instead of only one. 
    for($round = 0; $round < 65536; $round++) 
    { 
        $password = hash('sha256', $password . $salt); 
    }  

    try 
    { 
        // Execute the query to create the user 
        $stmt = $db->prepare($query); 
        $stmt->execute(array(
        'password' => $password,
        'salt' => $salt,
        'id' => $id));
    } 
    catch(PDOException $ex) 
    { 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    // This redirects the user back to the login page after they register 
    header("Location: stafflist.php");

     // Calling die or exit after performing a redirect using the header function 
    // is critical.  The rest of your PHP script will continue to execute and 
    // will be sent to the user if you do not die or exit. 
    die("Redirecting to stafflist.php"); 

}

die("Passwords do not match.");  
}

我是 php 新手,所以请原谅我的天真。Ps 我知道我使用的方法是相当老派的,但这只是一个测试。

谢谢,乔

4

4 回答 4

1

您不能同时GETPOST单个HTTP Request中进行。

但是,您可以使用隐藏的输入字段来绕过此限制:

在您的 HTML 标记中,您可以添加以下内容:

<input type="hidden" name="id"
           value="<?php echo htmlspecialchars($_GET['id'], ENT_QUOTES); ?>" />

$_GET['id']应该工作得很好。

于 2013-09-24T14:43:21.590 回答
0

为了避免这个错误

Undefined index: id in /home/content/47/11368447/html/CCC/changepassword.php on line 6

首先测试是否index存在:

if(isset($_GET['id'])) {
    $id = $_GET['id'];
} else {
    // here you can set a value for the id
}

否则,您可以$id在 if 测试中添加您的 var:

if(!empty($_POST) && $id) 
{
    //...
}
于 2013-09-24T13:46:18.490 回答
0

看起来您正在将“id”传递给操作 URL,但由于某种原因 $_GET 变量没有它。请仔细检查:

  1. 你真的将“id”传递给 URL 吗?请确定。

  2. 请检查 common.php 中的代码,看看其中是否修改了 $_GET 变量。

  3. 该脚本是否在重写设置后面(例如在.htaccess 中)?如果是,$_GET 参数可能会由于不适当的重写设置而消失。您可以通过放置 print_r($_GET); 来进一步测试它。一开始并直接访问该脚本( GET 而不是 POST )

于 2013-09-24T13:52:32.613 回答
0

$id = $_GET['id']; 首先检查 $id 中是否有任何值以通过 echo 打印 $id

于 2013-09-24T13:54:02.133 回答