[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">
<?php if($this->session->userdata('active') == TRUE) {
echo $this->session->userdata('email');
} else {
echo $this->session->userdata('active');
?>
<form name="login" method="post" action="index.php/login/do_login">
<input type="text" name="email" value="Email" class="login_input" /> <input type="password" name="password" value="Password" class="login_input" /> <input type="submit" name="submit" value="Login" class="login_submit" />
</form>
<?php
}
?>
</div>
## CONTROLLER ##
Code:
<?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');
}
}
}
}
?>
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.