Php Safari正在更改会话变量
我正在验证表单输入,并确保用户不会提交表单两次。我在下面的课堂上这样做:Php Safari正在更改会话变量,php,safari,Php,Safari,我正在验证表单输入,并确保用户不会提交表单两次。我在下面的课堂上这样做: <?php //You can of course choose any name for your class or integrate it in something like a functions or base class class formKey { //Here we store the generated form key private $formKey; //
<?php
//You can of course choose any name for your class or integrate it in something like a functions or base class
class formKey
{
//Here we store the generated form key
private $formKey;
//Here we store the old form key (more info at step 4)
private $old_formKey;
//The constructor stores the form key (if one excists) in our class variable
function __construct()
{
//We need the previous key so we store it
if(isset($_SESSION['form_key']))
{
$this->old_formKey = $_SESSION['form_key'];
}
}
//Function to generate the form key
private function generateKey()
{
//Get the IP-address of the user
$ip = $_SERVER['REMOTE_ADDR'];
//We use mt_rand() instead of rand() because it is better for generating random numbers.
//We use 'true' to get a longer string.
//See http://www.php.net/mt_rand for a precise description of the function and more examples.
$uniqid = uniqid(mt_rand(), true);
//Return the hash
return md5($ip . $uniqid);
}
//Function to output the form key
public function outputKey()
{
//Generate the key and store it inside the class
$this->formKey = $this->generateKey();
//Store the form key in the session
$_SESSION['form_key'] = $this->formKey;
//Output the form key
echo "<input type='hidden' name='form_key' id='form_key' value='".$this->formKey."' />";
}
//Function that validated the form key POST data
public function validate()
{
//We use the old formKey and not the new generated version
if($_POST['form_key'] == $this->old_formKey)
{
//The key is valid, return true.
unset($_SESSION['form_key']);
return true;
}
else
{
//The key is invalid, return false.
return false;
}
}
}
?>
如果我回显
old_formKey
和postform_key
它们是完全相同的,它在除Safari之外的所有浏览器中都有效。如果我在Safari中检查这些,则旧的表单键总是不同的。为什么会这样?除非您进行了其他配置,否则PHP会话依赖于cookie。您的Safari是否设置为拒绝Cookie?@HoboSapiens它设置为从不阻止Cookie,但仍然存在相同的问题。
include STYLESHEETPATH . '/formkey.class.php';
$formKey = new formKey();
$formKey->outputKey();