【问题标题】:redirect not working with 'REQUEST_METHOD'重定向不适用于“REQUEST_METHOD”
【发布时间】:2015-07-02 19:05:03
【问题描述】:

在我的登录控制器上,我正在尝试 ($this->input->server('REQUEST_METHOD') == 'POST') 提交表单而不是 codeigniter form_validation

但是当我使用 redirect() 时,它不会重定向。

问题:为什么在使用($this->input->server('REQUEST_METHOD') 提交表单时重定向不起作用,让它起作用的最佳解决方案是什么。

更新:似乎没有提交表单,因为我使用$config['uri_protocol'] = 'QUERY_STRING'; 而不是$config['uri_protocol'] = 'REQUEST_URI';

但需要使用$config['uri_protocol'] = 'QUERY_STRING';

Routes.php

$route['route=common/login'] = 'admin/common/login/index';
$route['route=common/dashboard&token=(:any)'] = 'admin/common/dashboard/index';
$route['route=common/logout&token=(:any)'] = 'admin/common/logout/index';

Config.php

$config['base_url'] = 'http://localhost/project-cms/admin/';
$config['index_page'] = '';
$config['uri_protocol'] = 'QUERY_STRING';

登录控制器

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends MX_Controller {

    public function index()
    {

        if ($this->input->server('REQUEST_METHOD') == 'POST') {

            $token = md5(mt_rand());

            $url_token = '&token=' . $token;

            echo "working";

            //redirect('index.php?route=common/dashboard' . $url_token);

            //$this->response->redirect($this->url->link('common/dashboard', $url_token, 'SSL'));
        }

        $data['action'] = $this->url->link('common/login', '', 'SSL');

        $this->load->view('template/common/login', $data);
    }
}

登录查看

<form enctype="multipart/form-data" method="post" action="<?php echo $action;?>">

<input type="text" name="username" />

<input type="submit" value="Submit" />

</form>

版本: Codeigniter 3 & XAMPP 与 Windows 7

【问题讨论】:

标签: codeigniter


【解决方案1】:

REQUEST_METHOD 与您的重定向问题无关。
您使用了错误的方法来定义路由。

$route['common/login'] = 'admin/common/login/index';
$route['common/dashboard/(:any)'] = 'admin/common/dashboard/index/$1';
$route['common/logout/(:any)'] = 'admin/common/logout/index/S1';

你会得到你的令牌(在你的控制器中),

public function index($token)
{
    echo $token;
}

并将您的登录控制器更改为,

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends MX_Controller {
    public function index()
    {
        if ($this->input->server('REQUEST_METHOD') == 'POST') {
            $token = md5(mt_rand());
            redirect('common/dashboard/' . $url_token);
        }
        $data['action'] = $this->url->link('common/login', '', 'SSL');
        $this->load->view('template/common/login', $data);
    }
}

【讨论】:

    猜你喜欢
    • 2017-08-28
    • 2012-08-20
    • 1970-01-01
    • 2010-12-08
    • 2021-09-25
    • 2012-03-27
    • 2013-10-31
    • 2014-12-02
    • 2018-08-24
    相关资源
    最近更新 更多