php - 提交表单时如何获取从另一个页面传递的变量
问题描述
我正在处理reset
core中的密码php
。
我将code
变量从这样的页面发送reset.php
到resetPassword.php
页面:
reset.php
$code = uniqid(true);
$url = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/resetPassword.php?code=$code"
resetPassword.php
global $code;
if(!isset($_GET['code'])){
echo "There is No Code there !!!";
}
if(isset($_GET['code'])){
$code = $_GET['code'];
echo "The code is set at first , just at visit of the page ".$code;
}
// make sure there is row in the table that matches that passed code
$findEmail = mysqli_query($link,"SELECT `email` from resetpasswords WHERE `code` = '$code' ");
if(mysqli_num_rows($findEmail)==0){ // if no row found
echo "<h2 class = 'text text-center'>No Record regarding this email !</h2>";
exit();
}
// when form submits
if(isset($_POST['reset'])){
$password = md5($_POST['password']);
$confirm = md5($_POST['confirm']);
if($password!=$confirm){
echo "Password Don't matches !";
exit();
}
$row = mysqli_fetch_array($findEmail);
$email = $_POST['email'];
$updatePass = mysqli_query($link,"UPDATE `user` SET user_password = '$confirm' where email = '$email'");
if($updatePass){
$query = mysqli_query($link,"DELETE FROM resetpasswords where code = '$code' ");
exit('Password Updated, Please Login with the New Password');
}
else{
exit('Something went wrong');
}
}
在同一页面上resetPassword.php
,我有以下代码:
<form action="resetPassword.php" method="POST">
<div class="form-group">
<label for="password2">Password:</label>
<input type="password" class="form-control" name="password" id="password2" onkeyup="checkPass();">
</div>
<div class="form-group">
<label for="confirm2">Confirm Password:</label>
<input type="password" class="form-control" name="confirm" id="confirm2" onkeyup="checkPass();">
<span id="confirm-message2" class="confirm-message"></span>
</div>
<input type="hidden" value="<?php echo $code;?>">
<input type="submit" name="reset" value="Update Password" class="btn btn-success">
</form>
问题:
问题是当我提交表单时,它一直到页面顶部,并resetPassword.php
从顶部开始执行页面,因此对于resetPassowrd.php
页面,它不能get
是那个$code
变量。
因为当我提交表单时,(!isset($_GET['code']))
顶部的条件resetPassword.php
变为真,它给了我:
There is No Code there !!!
我希望$code
在提交表单时拥有它。
我尝试了什么:
我尝试使用hidden
值为 of 的字段,$code
但没有奏效。
请帮帮我谢谢
解决方案
考虑以下几点
1) 使用准备好的语句和参数化查询。
2) 使用password_hash()
和password_verify()
保护您的密码。
3) 在resetPassword.php
页面中,如果您使用action="resetPassword.php"
此提交表单,则会重定向到 resetPassword.php。所以用这个替换你的动作
$full_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
<form action="<?php echo $full_url ?>" method="POST">
推荐阅读
- yii2 - 如何在使用相同数据库的两个 yii2 应用程序中管理登录
- python - 使用自定义权重在pytorch中初始化权重?
- javascript - How to format complicated array with ._groupBy
- python - 基于高变量相关性的相关矩阵过滤与使用向量在规模上选择与目标变量相关性最小的相关性
- c++ - 计算时间和空间复杂度的问题?
- react-native - 用户注销时如何重置redux store的状态?
- google-tag-manager - 阻止从谷歌标签管理器加载 youtube 相关的 JS
- cadence-workflow - 有条件地执行活动
- amazon-web-services - AWS s3 生命周期策略
- python - 什么 channel_argument 会以与 grpc 客户端调用中的 timeout 参数等效的方式设置超时?