【问题标题】:CodeIgniter CSRF never works on first attemptCodeIgniter CSRF 永远不会在第一次尝试时工作
【发布时间】:2019-04-29 01:32:46
【问题描述】:

我有一个奇怪的问题,我找不到解决方案。我在 CodeIgniter 配置文件中激活了 csrf。当我尝试登录时,第一次尝试总是失败。所有接下来的尝试都有效。它适用于我的本地 MAMP,但是当我将它推送到我的网络服务器时,我遇到了这个奇怪的问题。我得到的错误: 您请求的操作不被允许。

我的配置。

$config['base_url'] = 'https://www.xy.de/test1/';
$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
$config['csrf_exclude_uris'] = array();

我的看法:

<?= form_open('login/connect' , ' class="login-form" '); ?>
        <div class="input-group mb-3">
                <input required type="text" name="fUsername" class="form-control rounded-input" placeholder="<?= $this->lang->line('username_or_email_placeholder'); ?>" aria-label="Username" aria-describedby="basic-addon1">
            </div>
            <div class="input-group mb-3">
                <input required type="password" name="fPassword" class="form-control rounded-input" placeholder="******" aria-label="password" aria-describedby="basic-addon1">
            </div>
            <div class="w-100 text-center">
                <button class="loginButton" type="submit" name="fLogin"><span><?= $this->lang->line('login'); ?></span></button>&nbsp;
                <a href="<?= base_url() ?>recover/request" id="forgotPassword"><?= $this->lang->line('forgot_password'); ?>?</a>                    
            </div>
        <?= form_close() ?>

控制器:

public function index()
    {
        if($this->LoginModel->isLoggedIn()){
            if($this->LoginModel->isUserSuperAdmin($this->session->idUser)){
                redirect("cms/dashboard");
            }else {
                redirect("cms/profile");
            }
        }else{
            $this->load->view("cms/login", array("title" => SITE_TITLE . " - " . $this->lang->line("login")));
        }
    }

public function connect()
    {
            $login = trim($this->input->post("fUsername"));
            $password = $this->input->post("fPassword");
            if ($this->LoginModel->login($login, $password)) {
                if ($this->LoginModel->isUserSuperAdmin($this->session->idUser)) {
                    redirect("cms/dashboard");
                } else {
                    redirect("cms/profile");
                }
            }

        redirect("cms");
    }

【问题讨论】:

    标签: php codeigniter csrf


    【解决方案1】:

    尝试这样的形式调用,不需要

    <input type="hidden" name="csrf_test_name" value="60bcd04d35e8e0ce9508a474e9d704da">     
    

    它会自动设置为表单 您的表单调用是:

    <form action="https://www.xy.de/test1/login/connect" class="login-form" method="post" accept-charset="utf-8">
    
    ---------------------------
    ---------------------------
    </form>
    

    你可以试试看

    <?php echo form_open('login/connect', array('class' => 'login-form')); ?>
    -----------------------------
    <?php echo form_close(); ?>
    

    希望它的工作。我使用它并工作 谢谢你

    【讨论】:

      【解决方案2】:

      哈希正在重新生成,您应该使用提供的类,而不是静态的。

      试试这个:

      <input type="hidden" name="<?=$this->security->get_csrf_token_name();?>" value="<?=$this->security->get_csrf_hash();?>" />
      

      【讨论】:

      • 我在上面发布的 HTML 代码来自 Inspect Element。我用我的视图文件中的 HTML 代码替换了它。 csrf 输入字段由表单回显生成
      • 你在配置文件中设置了全局xss过滤器
      • @GillesFeierstein 您在同一页面中还有其他表单吗?或任何帖子ajax?
      猜你喜欢
      • 2021-02-28
      • 2013-07-01
      • 2021-12-04
      • 2021-03-24
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      相关资源
      最近更新 更多