[eluser]Unknown[/eluser]
Hi All,
I have only been using CI for two days and I am struggling to see any real advantages apart from a better code structure. Today I have developed a basic user login system and it took a lot longer than it would just to do it in PHP.
Another thing is, I normally use $_SESSION to store details of the logged in user however I can't seem to do this anymore? (Also is a bad from a security point of view to store these details there? if so, what way is better?)
Below I have also provided some of the code I have developed today for the registration part of the user login system. Any feedback on this would be great.
Register (Controller)
Code:
<?php
class Register extends CI_Controller {
function index() {
// Load Register Model
$this->load->model('Register_model');
// Load Form Validation Helper
$this->load->library('form_validation');
// Validate Form Fields
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('forename', 'First Name', 'trim|required');
$this->form_validation->set_rules('surname', 'Last Name', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('register');
}
else
{
// Check Username is Available
if ($this->Register_model->check_username($this->input->post('username')))
{
// User Details Array (for passing to register function)
$User['username'] = $this->input->post('username');
$User['password'] = $this->input->post('password');
$User['email'] = $this->input->post('email');
$User['forename'] = $this->input->post('forename');
$User['surname'] = $this->input->post('surname');
// Register the User
if ($this->Register_model->register_user($User['username'], $User['password'], $User['email'], $User['forename'], $User['surname']))
{
$this->load->view('register_success');
}
else
{
echo "Registration Failed, Please Try Again.";
}
}
else
{
echo "Username in use.";
}
}
}
}
Register (Model)
Code:
<?php
class Register_model extends CI_Model {
function check_username($username) {
$this->load->database();
$check_username = $this->db->query("SELECT * FROM Users WHERE Username = '".$username."' LIMIT 1");
if ($check_username->num_rows() == 1)
{
return FALSE;
}
else
{
return TRUE;
}
}
function register_user($username, $password, $email, $forename, $surname) {
$this->load->database();
$sql = "INSERT INTO Users (Username, Password, Email, Forename, Surname)
Values('".$username."', '".$password."', '".$email."', '".$forename."', '".$surname."')";
$register_user = $this->db->query($sql);
if ($register_user)
{
return TRUE;
}
else
{
return FALSE;
}
}
}
Register (View)
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title></title>
</head>
<body>
<?php echo validation_errors(); ?>
<form name="username" action="register" method="post">
<label for="username">Username</label></br><input type="text" name="username" /></br>
<label for="password">Password</label></br><input type="password" name="password" /></br>
<label for="passconf">Password Confirmation</label></br><input type="password" name="passconf" /></br>
<label for="email">Email</label></br><input type="email" name="email" /></br>
<label for="forename">First Name</label></br><input type="text" name="forename" /></br>
<label for="surname">Last Name</label></br><input type="text" name="surname" /></br>
<input type="submit" value="Register" />
</form>
</body>
</html>