【问题标题】:Form is not submitted and Invalid Token in PHP表单未提交且 PHP 中的令牌无效
【发布时间】:2016-12-02 01:52:30
【问题描述】:

当您点击Login提交按钮时,它只显示“Invalid Token”,因为系统无法识别token。但是,如果我点击 Register 提交按钮,表单就会被提交并处理。

表格代码:

<form method="post">
  <div class="field">
    <label for="username">Username: </label>
    <input type="text" name="username" id="username" autocomplete="off" />
  </div>

  <div class="field">
    <label for="Password">Password: </label>
    <input type="password" name="password" id="password" autocomplete="off" />
  </div>

  <div class="field">
    <label for="remember">
      <input type="checkbox" name="remember" id="remember" value="on"/> Remember Me
    </label>
  </div>

  <input type="hidden" name="login_token" value="<?php echo Token::generate(); ?>" />
  <input name="login" type="submit" value="Login" />
</form>
<hr>
<br>
<form action="" method="post">
      <div class="field">
        <label for="username">Username</label>
        <input type="text" name="username" id="username" value="<?php echo sanitize(Input::get('username')); ?>" autocomplete="off" />
      </div>

      <div class="field">
        <label for="password">Choose a Password</label>
        <input type="password" name="password" id="password" />
      </div>

      <div class="field">
        <label for="password_again">Enter your Password Again</label>
        <input type="password" name="password_again" id="password_again" />
      </div>

      <div class="field">
        <label for="name">Name</label>
        <input type="text" name="name" id="name" value="<?php echo sanitize(Input::get('name')); ?>"/>
      </div>
      <input type="hidden" name="rgstr_tkn" value="<?php echo Token::generate(); ?>" />
      <input type="submit" value="Register" name="register"/>
</form>

表单提交时要处理的PHP代码:

if (isset($_POST["login"])){
        if(Token::check(Input::get('login_token'))) {
            echo "Login!";
            echo Input::get('login_token');
        } else {
            echo 'invalid token';
        }
}

if (isset($_POST["register"])) {
        if(Token::check(Input::get('rgstr_tkn'))) {
            echo "Register!";
            echo Input::get('rgstr_tkn');
        }
}

Token班级:

class Token {

  # Generate a token, and put it into the session/token_name
  public static function generate() {
      return Session::put(Config::get('session/token_name'), md5(uniqid()));
  }

  # Check if the token exists
  public static function check($token) {
      $tokenName = Config::get('session/token_name');

      if(Session::exists($tokenName) && $token === Session::get($tokenName)) {
          Session::delete($tokenName);
          return true;
      }

      return false;
  }

}   

Input类:

class Input {

    # Check if the POST or GET request is submitted
    public static function exists($type = 'post') {
        switch($type) {
            case 'post':
                return (!empty($_POST)) ? true : false;
                break;
            case 'get':
                return (!empty($_GET)) ? true : false;
                break;
            default:
                return false;
                break;
        }
    }

    # Get an item from the posted or get field
    public static function get($item) {
        if(isset($_POST[$item])) {
            return $_POST[$item];
        } else if(isset($_GET[$item])) {
            return $_GET[$item];
        }

        return '';
    }

}

【问题讨论】:

  • 能否包含您的配置
  • 你能不能也展示一下你的 Session 课程
  • 对我来说似乎没问题,检查你是否session_start(); 可能你还没有start 会话中的会话
  • 你能print_r($_SESSION);看看结果,如果你的会话确实存在于你的数组中,也试试echo Input::get('login_token);看看结果
  • 我的意思是你点击登录,Array() 是什么样的?

标签: php forms token


【解决方案1】:

您的问题是,当您再次返回该页面时,它会再次生成一个新令牌 这就是为什么它返回“无效令牌”来解决这个问题

在你的

public static function generate() {

}

在创建之前先检查是否已经生成了指定的会话令牌

public static function generate() {
    $tokenName = Config::get('session/token_name');
    // if session is already generate then just return it instead of generating new one
    if (Session::exists($tokenName)) {    
        return Session::get($tokenName);
    }
    // else create this session_token
    return Session::put($tokenName, md5(uniqid()));
}

希望对你有帮助

【讨论】:

  • public static function generate() { $tokenName = Config::get('session/token_name'); if (Session::exists($tokenName)) { return Session::get($tokenName); } return Session::put($tokenName), md5(uniqid())); }
  • @astronomicalXoom 我看到错误将其更改为return Session::put($tokenName, md5(uniqid())); 这只是因为)$tokenName 之后
  • 好吧,谢谢你的回答 :) 好吧,很抱歉打扰你 :( 我才 13 岁,开始学习 OOP 和课程哈哈哈哈哈哈
  • 我想要,但我做不到 :( 我只有 3 个代表 :(
  • @astronomicalXoom 祝你的编程课程好运 :)
猜你喜欢
  • 2016-02-24
  • 2014-06-20
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
  • 2018-01-15
  • 1970-01-01
  • 2017-08-01
相关资源
最近更新 更多