Swoole\Form::secret PHP Method

secret() static public method

设置Form Secret防止,非当前页面提交数据
static public secret ( $page_name = '', $length = 32, $return = false ) : string
$page_name
$return
return string
    static function secret($page_name = '', $length = 32, $return = false)
    {
        $secret = uniqid(RandomKey::string($length));
        if ($return) {
            return $secret;
        } else {
            $k = 'form_' . $page_name;
            setcookie($k, $secret, 0, '/');
            if (!isset($_SESSION)) {
                session();
            }
            $_SESSION[$k] = $secret;
        }
    }

Usage Example

Example #1
0
 /**
  * 自动生成表单
  *
  * @param $set_id
  *
  * @return unknown_type
  */
 function getForm($set_id = 0)
 {
     $this->_form_();
     //传入ID,修改表单
     if ($set_id) {
         $data = $this->get((int) $set_id)->get();
         foreach ($this->_form as $k => &$f) {
             $f['value'] = $data[$k];
         }
         if (method_exists($this, "_set_")) {
             $this->_set_();
         }
         if ($this->_form_secret) {
             Form::secret(get_class($this) . '_set');
         }
     } elseif (method_exists($this, "_add_")) {
         $this->_add_();
         if ($this->_form_secret) {
             Form::secret(get_class($this) . '_add');
         }
     }
     return Form::autoform($this->_form);
 }