Php $\u GET[…]不工作-数据库未更新
我在这个网站上问了一些关于这段代码的问题 基本上,当我使用-$id=$GET_uu['id']时,我的数据库不会更新;(位于下面代码的顶部)。id从上一页传递到此页-该页的url为“http://www.21orange.com/CCC/changepassword.php?id=1”。我的数据库中有一个“id”字段 当我将上面的代码行更改为-$id='1'时,代码运行良好,数据库也会更新。它只有在我使用$GET_uzy['id']时才会停止工作。为什么会这样Php $\u GET[…]不工作-数据库未更新,php,get,Php,Get,我在这个网站上问了一些关于这段代码的问题 基本上,当我使用-$id=$GET_uu['id']时,我的数据库不会更新;(位于下面代码的顶部)。id从上一页传递到此页-该页的url为“http://www.21orange.com/CCC/changepassword.php?id=1”。我的数据库中有一个“id”字段 当我将上面的代码行更改为-$id='1'时,代码运行良好,数据库也会更新。它只有在我使用$GET_uzy['id']时才会停止工作。为什么会这样 // First we execu
// 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.");
}
//首先,我们执行我们的公共代码来连接数据库并启动会话
要求(“common.php”);
$id=$_GET['id'];
//此if语句检查以确定是否已提交注册表
//如果有,则运行注册码,否则显示表单
如果(!空($\u POST))
{
//确保用户输入了非空密码
if(空($_POST['password']))
{
死亡(“请输入密码”);
}
//确保用户输入了非空用户名
if(空($\u POST['confirmpassword']))
{
//请注意,die()通常是处理用户错误的糟糕方法
//像这样。用表单显示错误要好得多
//并允许用户纠正错误
//锻炼身体,让你实现自我。
死亡(“请确认您的密码”);
}
如果($\u POST['password']=$\u POST['confirmpassword'])){
//插入查询用于向数据库表中添加新行。
//同样,我们使用特殊标记(技术上称为参数)来
//防止SQL注入攻击。
$query=“updatestaff SET password=:password,salt=:salt,其中id=:id”;
//这里随机生成一个salt来再次保护暴力攻击
//和彩虹表攻击。下面的语句生成一个十六进制
//表示8字节的salt。用十六进制表示它提供
//没有额外的安全性,但让人更容易阅读。
$salt=dechex(兰特02147483647))。dechex(兰特02147483647));
//这会将密码与salt散列,以便可以安全地存储密码
//下一条语句的输出是64字节十六进制
//表示密码的32字节sha256哈希的字符串。原始
//无法从哈希中恢复密码。
$password=hash('sha256',$\u POST['password'].$salt);
//接下来我们对散列值65536进行多次散列
//防止暴力攻击。现在攻击者必须计算哈希65537
//他们根据密码进行的每次猜测的次数,而如果
//如果只进行一次哈希运算,攻击者将能够使65537不同
//猜测的时间相同,而不是只有一次。
对于($round=0;$round<65536;$round++)
{
$password=hash('sha256',$password.$salt);
}
尝试
{
//执行查询以创建用户
$stmt=$db->prepare($query);
$stmt->execute(数组)(
“密码”=>$password,
“salt”=>salt美元,
'id'=>$id));
}
捕获(PDO异常$ex)
{
//注意:在生产网站上,不应输出$ex->getMessage()。
//它可能会向攻击者提供有关代码的有用信息。
die(“无法运行查询:”..ex->getMessage());
}
//这会在用户注册后将其重定向回登录页面
标题(“位置:stafflist.php”);
//使用header函数执行重定向后调用die或exit
//是至关重要的。PHP脚本的其余部分将继续执行并
//如果您没有死亡或退出,将发送给用户。
die(“重定向到stafflist.php”);
}
死亡(“密码不匹配”);
}
我是php新手,所以请原谅我的天真。我知道我使用的方法很老套,但这只是一种测试
谢谢,
乔要避免这个错误
Undefined index: id in /home/content/47/11368447/html/CCC/changepassword.php on line 6
如果索引
存在,则进行第一次测试:
if(isset($_GET['id'])) {
$id = $_GET['id'];
} else {
// here you can set a value for the id
}
否则,您可以在if测试中添加$id
变量:
if(!empty($_POST) && $id)
{
//...
}
为了避免这个错误
Undefined index: id in /home/content/47/11368447/html/CCC/changepassword.php on line 6
如果索引
存在,则进行第一次测试:
if(isset($_GET['id'])) {
$id = $_GET['id'];
} else {
// here you can set a value for the id
}
否则,您可以在if测试中添加$id
变量:
if(!empty($_POST) && $id)
{
//...
}
看起来您正在将“id”传递给操作URL,但由于某些原因,$\u GET变量没有该id。请仔细检查:
看起来您正在将“id”传递给操作URL,但由于某些原因,$\u GET变量没有该id。请仔细检查:
首先检查$id中是否有任何值,以便通过echo打印$id您不能在一次操作中同时执行
GET
和POST
但是,您可以使用隐藏的inp