codeigniter:如何重定向后登录到电流控制器(php_self在常规php) [英] codeigniter: how to redirect after login to current controller (php_self in regular php)

查看:135
本文介绍了codeigniter:如何重定向后登录到电流控制器(php_self在常规php)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一个真正的问题,但我检查用户是否存在并记录它们并重定向到site / members_area,但我不想发送用户到特定页面,但我想重新加载当前控制器。
所以如果我在索引/ home登录,我想在index / home重定向,我应该如何进行?



要重定向到当前页的操作

 <?php echo $ _SERVER ['PHP_SELF']; > 

这是框架中的代码

  function validate_credentials()
{
$ this-> load-> model('membership_model');
$ query = $ this-> membership_model-> validate();

if($ query)//如果用户的凭证已验证...
{
$ data = array(
'username'=> $ this- > input-> post('username'),
'is_logged_in'=> true
);
$ this-> session-> set_userdata($ data);
redirect('site / members_area'); //< - 这一行应该是动态的
}
else //不正确的用户名或密码
{
$ this-> index();
}
}


解决方案

I解决这个问题自己通过一个登录表单在头中总是提交到一个登录控制器,但catch是头登录表单(显示在每一个页面)总是有一个隐藏的输入称为重定向,实际的登录控制器捕获。 ..



以下是基本设置(确保加载了url帮助程序):



标题登录表单

 < form action =/ loginmethod =post> 
< input type =hiddenname =redirectvalue =<?php echo current_url();?> />
< input type =textname =usernamevalue =/>
< input type =passwordname =passwordvalue =/>
< input type =submitname =loginvalue =登录id =submit>
< / form>

登录控制器表单

 < form id =loginaction =method =post> 
< input type =textname =usernameid =usernamevalue =/>
< input type =passwordname =passwordid =passwordvalue =/>

<?php if(isset($ _ POST ['redirect'])):?>
< input type =hiddenname =redirectvalue =<?php echo $ _POST ['redirect'];?> />
<?php endif; >

< input type =submitname =loginid =submitvalue =Login/>
< / form>

最好的部分是在失败时继续设置重定向,

 

code> function index()
{
if(!$ this-> form_validation-> run())
{
//
}
else
{
//记录用户,然后重定向
$ this-> _redirect();
}
}

function _redirect()
{
//是否有重定向要处理?
if(!isset($ _ POST ['redirect']))
{
redirect(site / members_area,location);
return;
}

//基本检查以确保我们不会重定向到登录页面
// current_url将是您的登录控制器
if($ _ POST [ 'redirect'] === current_url())
{
redirect(site / members_area,location);
return;
}

redirect($ _ POST ['redirect'],location);
}

这里发生的是以下情况:


  1. 用户在不同的页面上登录。

  2. 登录表单提交到具有隐藏输入元素的单个登录控制器,

  3. 登录控制器处理登录,然后根据输入重定向。

  4. 登录失败时,重定向会再次设置,

这只是一个基本的例子。你可以明显地根据需要调整它。


Well it's not really a problem but I check if the user exist and log them in and redirect to site/members_area, but I don't want to send the user to a specific page but i want to reload the current controller. So if I login in index/home I would like to be redirected at index/home, how should I proceed?

in regular php I would put in the action to redirect to current page

<?php echo $_SERVER['PHP_SELF']; ?>

This is the code in the framework

function validate_credentials()
    {       
        $this->load->model('membership_model');
        $query = $this->membership_model->validate();

        if($query) // if the user's credentials validated...
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
            redirect('site/members_area'); //<-- this line here should be dynamic
        }
        else // incorrect username or password
        {
            $this->index();
        }
    }

解决方案

I solved this problem myself by having a login form in the header that always submits to one login controller, but the catch is that the header login form (which appears on every page) always has a hidden input called redirect which the actual login controller captures...

Here's the basic set up (make sure the url helper is loaded):

The Header Login Form

<form action="/login" method="post">
    <input type="hidden" name="redirect" value="<?php echo current_url(); ?>" />
    <input type="text" name="username" value=""  />
    <input type="password" name="password" value=""  />
    <input type="submit" name="login" value="Login" id="submit">
</form>

The Login Controller Form

<form id="login" action="" method="post">
    <input type="text" name="username" id="username" value="" />
    <input type="password" name="password" id="password" value=""/>

    <?php if(isset($_POST['redirect'])) : ?>
    <input type="hidden" name="redirect" value="<?php echo $_POST['redirect']; ?>" />
    <?php endif; ?>

    <input type="submit" name="login" id="submit" value="Login" />  
</form>

The best part is you keep setting the redirect on failure and the redirect input only gets set if you're logging in from somewhere else.

The Controller

function index()
{
    if( ! $this->form_validation->run())
    {
        // do your error handling thing
    }
    else
    {
        // log the user in, then redirect accordingly
        $this->_redirect();
    }   
}

function _redirect()
{
    // Is there a redirect to handle?
    if( ! isset($_POST['redirect']))
    {
        redirect("site/members_area", "location");
        return;
    }

    // Basic check to make sure we aren't redirecting to the login page
    // current_url would be your login controller
    if($_POST['redirect'] === current_url())
    {
        redirect("site/members_area", "location");
        return;
    }

    redirect($_POST['redirect'], "location");
}

What's happening here is this:

  1. User logins on a different page.
  2. The login form submits to a single login controller with a hidden input element stating where they are logging in from.
  3. The login controller processes the login, then redirects based on the input.
  4. On failed login the redirect keeps getting set again, so no matter what, the user will return to the original page.

That's just a basic example. You can obviously tweak it as needed.

这篇关于codeigniter:如何重定向后登录到电流控制器(php_self在常规php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆