Welcome Guest, Not a member yet? Register   Sign In
Sessions In Views
#1

[eluser]macmonkey[/eluser]
I've created a simple form allowing a user to login. In my controller the login is occuring but I want to load my view and display "Welcome $username" instead of the login form once they have successfully logged in.

For some reason my session variables aren't being carried over to my view.

## VIEW SNIPPIT ##
Code:
<div id="header_right">
                &lt;?php if($this->session->userdata('active') == TRUE) {
                
                echo $this->session->userdata('email');
                    
                } else {
                    echo $this->session->userdata('active');
                ?&gt;
                    &lt;form name="login" method="post" action="index.php/login/do_login"&gt;
                        &lt;input type="text" name="email" value="Email" class="login_input" /&gt; &lt;input type="password" name="password" value="Password" class="login_input" /&gt; &lt;input type="submit" name="submit" value="Login" class="login_submit" /&gt;
                    &lt;/form&gt;
                &lt;?php    
                }
                ?&gt;
            </div>

## CONTROLLER ##
Code:
&lt;?php


    class Login extends Controller {

        function index()
        {
            parent::Controller();
        }

        function do_login()
        {
            
            $this->load->helper('url');
            $this->load->library('validation');
            
            // IF THEY ARE ALREADY LOGGED IN THEN LETS REDIRECT THEM
            if($this->session->userdata('active')) {
            //    redirect("index.php");
            }
            
            
            // LETS VALIDATE THEIR INPUT
            $rules['email'] = "required|min_length[4]|max_length[32]|alpha_dash";
            $rules['password'] = "required|min_length[4]|max_length[32]|alpha_dash";
            $this->validation->set_rules($rules);
            
            if($this->validation->run() == false) {
                echo "All fields are required.";
            } else {
                // SEE IF THEY ARE IN THE DATABASE
                $this->load->database();
                $data = array(
                    'email' => $this->input->xss_clean($this->input->post('email')),
                    'password' => $this->input->xss_clean($this->input->post('password'))
                    );
                $query = $this->db->query("SELECT * FROM users WHERE email = '$data[email]' AND password = '$data[password]' LIMIT 1") or die(mysql_error());
                if($query->num_rows() == 1) {
                    
                    $this->session->set_userdata(array('active' => 'TRUE', 'email' => $this->input->post('email')));
                
                    redirect('', 'location');
                }
                
            }
        
        }
    
    }
    ?&gt;

It's my very first attempt at Code Igniter (or any MVC pattern). I'm just really confused.

Thanks

jw
#2

[eluser]jedd[/eluser]
[quote author="macmonkey" date="1251669251"]
For some reason my session variables aren't being carried over to my view.
[/quote]

In the code you've posted, you don't actually appear to be loading the view.

In the unseen bit where you do load the view, are you transferring any variables (just not session variables) - or are you not transferring anything at all?
#3

[eluser]macmonkey[/eluser]
Jedd - thanks for your question. I am not loading a view rather I am redirecting back to my default controller/view. Does the session data not persist? I'm guessing what I need to do is load the view within the login controller and pass the data to the view?

Thanks
#4

[eluser]überfuzz[/eluser]
[quote author="macmonkey" date="1251669251"]I've created a simple form allowing a user to login. In my controller the login is occuring but I want to load my view and display "Welcome $username" instead of the login form once they have successfully logged in.

For some reason my session variables aren't being carried over to my view.

## VIEW SNIPPIT ##
Code:
<div id="header_right">
                &lt;?php if($this->session->userdata('active') == TRUE) {
                
                echo $this->session->userdata('email');
                    
                } else {
                    echo $this->session->userdata('active');
                ?&gt;
                    &lt;form name="login" method="post" action="index.php/login/do_login"&gt;
                        &lt;input type="text" name="email" value="Email" class="login_input" /&gt; &lt;input type="password" name="password" value="Password" class="login_input" /&gt; &lt;input type="submit" name="submit" value="Login" class="login_submit" /&gt;
                    &lt;/form&gt;
                &lt;?php    
                }
                ?&gt;
            </div>

## CONTROLLER ##
Code:
&lt;?php


    class Login extends Controller {

        function index()
        {
            parent::Controller();
        }

        function do_login()
        {
            
            $this->load->helper('url');
            $this->load->library('validation');
            
            // IF THEY ARE ALREADY LOGGED IN THEN LETS REDIRECT THEM
            if($this->session->userdata('active')) {
            //    redirect("index.php");
            }
            
            
            // LETS VALIDATE THEIR INPUT
            $rules['email'] = "required|min_length[4]|max_length[32]|alpha_dash";
            $rules['password'] = "required|min_length[4]|max_length[32]|alpha_dash";
            $this->validation->set_rules($rules);
            
            if($this->validation->run() == false) {
                echo "All fields are required.";
            } else {
                // SEE IF THEY ARE IN THE DATABASE
                $this->load->database();
                $data = array(
                    'email' => $this->input->xss_clean($this->input->post('email')),
                    'password' => $this->input->xss_clean($this->input->post('password'))
                    );
                $query = $this->db->query("SELECT * FROM users WHERE email = '$data[email]' AND password = '$data[password]' LIMIT 1") or die(mysql_error());
                if($query->num_rows() == 1) {
                    
                    $this->session->set_userdata(array('active' => 'TRUE', 'email' => $this->input->post('email')));
                
                    redirect('', 'location');
                }
                
            }
        
        }
    
    }
    ?&gt;

It's my very first attempt at Code Igniter (or any MVC pattern). I'm just really confused.

Thanks

jw[/quote]
You could read up a bit on MVC-structure. If you're planing to use it.
View: display info, no logic is necessary.
Controll: Handle information, all the logic.
Model: Get info, like sql-statments, configuration etc.
#5

[eluser]macmonkey[/eluser]
Certainly - to be fair I have read about it. I've been touching on MVC for at least a year but since I have not had any practical experience I'm still pretty green with it. I've been developing a procedural approach for 4 years so this is still very new.

Thanks
#6

[eluser]jedd[/eluser]
[quote author="macmonkey" date="1251670906"]Jedd - thanks for your question. I am not loading a view rather I am redirecting back to my default controller/view.[/quote]

You might be less confused if you think of redirecting to a different controller/method. Views are (arguably, I concede) subjugated by controllers.

Quote: Does the session data not persist?

Session data persists, yay verily.

Quote:I'm guessing what I need to do is load the view within the login controller and pass the data to the view?

Correct.




Theme © iAndrew 2016 - Forum software by © MyBB