Welcome Guest, Not a member yet? Register   Sign In
Sessions In Views
#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.


Messages In This Thread
Sessions In Views - by El Forum - 08-30-2009, 10:54 AM
Sessions In Views - by El Forum - 08-30-2009, 11:18 AM
Sessions In Views - by El Forum - 08-30-2009, 11:21 AM
Sessions In Views - by El Forum - 08-30-2009, 11:23 AM
Sessions In Views - by El Forum - 08-30-2009, 11:39 AM
Sessions In Views - by El Forum - 08-30-2009, 04:58 PM



Theme © iAndrew 2016 - Forum software by © MyBB