gpt4 book ai didi

php csrf保护库

转载 作者:搜寻专家 更新时间:2023-10-31 21:17:48 24 4
gpt4 key购买 nike

是否有任何库可以防止 CSRF(PHP5.1/5.2) 或我需要自己创建?我使用来自 Chris 的这个片段,但没有图书馆,我在每一页上都会有很多重复。

我找到了这个 library对于 PHP5.3,但我想知道是否有任何关于 PHP5.1/5.2 的,因为我不相信所有的主机都支持 PHP5.3。

最佳答案

自从我使用 Kohana - 我刚刚扩展了它的几个核心类。它可以在任何代码中使用,但只需稍作更改:

class Form extends Kohana_Form
{
public static function open($action = NULL, array $attributes = null)
{
if (is_null($action))
{
$action = Request::current()->uri . ($_SERVER['QUERY_STRING'] ? '?' . $_SERVER['QUERY_STRING'] : '');
}

$open = parent::open($action, $attributes);
$open .= parent::hidden(self::csrf_token_field(), self::csrf_token());
return $open;
}

public static function csrf_token_field()
{
return 'csrf_token';
}

public static function csrf_token()
{
$session = Session::instance();
$token = $session->get(self::csrf_token_field());

if (!$token)
{
$session->set(self::csrf_token_field(), $token = md5(uniqid()));
}

return $token;
}
}

class Validate extends Kohana_Validate
{
public function __construct(array $array, $csrf = true)
{
parent::__construct($array);
if ($csrf)
$this->add_csrf();
}

public static function factory(array $array, $csrf = true)
{
return new Validate($array, $csrf);
}

private function add_csrf()
{
$this->rules(form::csrf_token_field(), array(
'not_empty' => array(),
'csrf' => array()
));
}

protected function csrf($token)
{
return $token == form::csrf_token();
}

}

关于php csrf保护库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4788849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com