0

a bit new to CI, googled and overflowed alot and still got no answer

  1. User enters site.
  2. After succesful auth got redirected to main page
  3. Link on the url stays the same with class/method
  4. If u refresh page on a main - u always got question about repopulate form (chrome/firefox 100%)

the solution may be: after success redirect to another class or method

but i don't know how to do it, documentation seems more like reference to me

code is here: http://paste.ubuntu.com/696751/ line 28 - how to do redirect to another class or method with a redirection to another view too?

Umren
  • 392
  • 4
  • 12
  • possible duplicate of [Seeking simple Post-Redirect-Get code example](http://stackoverflow.com/questions/4142809/seeking-simple-post-redirect-get-code-example) – Gordon Sep 25 '11 at 16:40
  • *(reference)* https://secure.wikimedia.org/wikipedia/en/wiki/Post/Redirect/Get – Gordon Sep 25 '11 at 16:40
  • *(related)* http://codeigniter.com/forums/viewthread/140096/#691586 – Gordon Sep 25 '11 at 16:43
  • on second link to CI forums, there's a solution with an optional success page, but if i don't wanna have one? how to solve this without extra pages? – Umren Sep 25 '11 at 16:47
  • Simply redirect to wherever you want the user to be redirected after a successful login. – Gordon Sep 25 '11 at 16:48
  • Why not just redirect home then, if you don't want another page, or redirect to the page they were viewing before – Jess Sep 25 '11 at 16:50

2 Answers2

1

Well an example in CodeIgniter may be:

class login extends CI_Controller
{
    function index ()
    {
        $this->load->library('form_validation');
        $this->load->helper('url');
        //Set form validation rules here: http://codeigniter.com/user_guide/libraries/form_validation.html
        if ($this->form_validation->run() == TRUE)
        {
            //login user here



            redirect('login/sucLogin'); // or just redirect to '/' if you want to send them to your home page
        }
        else
            $this->load->view('loginForm'); //make form
    }
    function sucLogin ()
    {
        echo 'Successfully logged in';
        echo anchor('/', 'Go Home');
    }
}
Jess
  • 8,628
  • 6
  • 49
  • 67
  • somehow redirect('something'); just don't working, only a direct load of view ;/ http://paste.ubuntu.com/696840/ – Umren Sep 25 '11 at 20:11
  • with redirect there's blank page only – Umren Sep 25 '11 at 20:12
  • Did you write the rules for the form validation? – Jess Sep 25 '11 at 20:17
  • No, input now is working just against db, and it's really working, telling you - if i do $this->load->view('main'); im going on main page if my auth is correct, but i got problem with refresh/repopulate, and somehow redirect just don't working for me, and i want to do it CI-way ofcourse – Umren Sep 25 '11 at 20:18
  • Well the way I posted above is the most CI correct way, using the form validation library, and the url helper. The validation library is amazing after you learn it, I recommend you look at their examples of how to use it. And it will help you out a lot in the future. – Jess Sep 25 '11 at 22:07
1
  1. Check to see if the user submitted the form
  2. Validate the login credentials
  3. Redirect on success

    public function login() { if ($_POST) { $login = $this->input->post('login'); $password = md5($this->input->post('password'));

        $q = $this->db
            ->where('login', $login)
            ->where('password', $password)
            ->limit(1)
            ->get('userbase');
    
        if ($q->num_rows > 0 )
        {
            redirect('enter/main');
        }
    }
    
    $returnlogin['login'] = $login;
    $this->load->helpers('form');
    $this->load->view('login_form',$returnlogin);
    

    }

    public function main() { $this->load->view('main'); }

Chris Schmitz
  • 8,097
  • 6
  • 31
  • 41
  • Not sure why my code isn't being highlighted correctly... Here it is on pastebin: http://pastebin.com/QdnE0vHT – Chris Schmitz Sep 25 '11 at 16:54
  • donnow why, but this is don't working! if i load $this->load->view('main') directly into if - it's working, but redirect don't! http://paste.ubuntu.com/696844/ – Umren Sep 25 '11 at 20:08
  • with redirect there's blank page only – Umren Sep 25 '11 at 20:13
  • It could be because `redirect()` is a method in the URL helper, so you would need to load that (`$this->load->helper('url')`) before you use the method. – Chris Schmitz Sep 25 '11 at 20:38