Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function login() {
- $data = $this->data;
- $email = $this->input->post('email');
- $password = md5($this->input->post('password'));
- $result = $this->user_model->login($email, $password);
- if (count($result) !== 0) {
- $this->session->set_userdata('user_id', $email);
- $seid = $this->session->userdata('user_id');
- if ($seid == '') {
- redirect(site_url());
- } else {
- redirect('home/view');
- }
- } else {
- redirect('home/index');
- }
- }
- function login($email, $password) {
- $this->db->where("email", $email);
- $this->db->where("password", $password);
- $query = $this->db->get("user");
- return $query->result_array();
- }
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Simple Login with CodeIgniter</title>
- </head>
- <body>
- <h1>Simple Login with CodeIgniter</h1>
- <?php echo validation_errors(); ?>
- <?php echo form_open('verifylogin'); ?>
- <label for="username">Username:</label>
- <input type="text" size="20" id="username" name="username"/>
- <br/>
- <label for="password">Password:</label>
- <input type="password" size="20" id="passowrd" name="password"/>
- <br/>
- <input type="submit" value="Login"/>
- </form>
- </body>
- </html>
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class VerifyLogin extends CI_Controller {
- function __construct()
- {
- parent::__construct();
- $this->load->model('user','',TRUE);
- }
- function index()
- {
- //This method will have the credentials validation
- $this->load->library('form_validation');
- $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
- $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
- if($this->form_validation->run() == FALSE)
- {
- //Field validation failed. User redirected to login page
- $this->load->view('login_view');
- }
- else
- {
- //Go to private area
- redirect('home', 'refresh');
- }
- }
- function check_database($password)
- {
- //Field validation succeeded. Validate against database
- $username = $this->input->post('username');
- //query the database
- $result = $this->user->login($username, $password);
- if($result)
- {
- $sess_array = array();
- foreach($result as $row)
- {
- $sess_array = array(
- 'id' => $row->id,
- 'username' => $row->username
- );
- $this->session->set_userdata('logged_in', $sess_array);
- }
- return TRUE;
- }
- else
- {
- $this->form_validation->set_message('check_database', 'Invalid username or password');
- return false;
- }
- }
- }
- ?>
- <?php
- Class User extends CI_Model
- {
- function login($username, $password)
- {
- $this -> db -> select('id, username, password');
- $this -> db -> from('users');
- $this -> db -> where('username', $username);
- $this -> db -> where('password', MD5($password));
- $this -> db -> limit(1);
- $query = $this -> db -> get();
- if($query -> num_rows() == 1)
- {
- return $query->result();
- }
- else
- {
- return false;
- }
- }
- }
- ?>
- public function login(){
- $email=$this->input->post('email');
- //query the database
- $result = $this->mod_user->login($email, $password);
- if($result){
- $sess_array = array();
- foreach($result as $row){
- $sess_array = array(
- 'user_id' => $row->user_id,
- 'email' => $row->email
- );
- $this->session->set_userdata('sess', $sess_array);
- }
- return TRUE;
- }
- else
- {
- $this->form_validation->set_message('login', 'Invalid username or password');
- return false;
- }
- }
- function login($userName, $pass){
- $this -> db -> select('user_id, email, password');
- $this -> db -> from('tbl_users');
- $this -> db -> where('email', $email);
- $this -> db -> where('password', MD5($password));
- $this -> db -> limit(1);
- $query = $this -> db -> get();
- if($query -> num_rows() == 1)
- {
- return $query->result();
- }
- else
- {
- return false;
- }
- }
- public function login() {
- $this->load->helper('form');
- $this->load->library('form_validation');
- $this->form_validation->set_rules('email', 'Email', 'required');
- $this->form_validation->set_rules('password', 'Password', 'required');
- // This condition check whether the request is post with valid data or not if it's not a post request than form validation return FALSE
- if ($this->form_validation->run() == FALSE) {
- // This condition check if session id is set or not if session id is set it will redirect you to homepage.
- if (isset($this->session->userdata['user_id'])) {
- redirect('home/view');
- } else {
- //else it will redirect you to login page.
- $this->load->view('login_view');
- }
- } else {
- //if it's a post request with valid data it will validate data in database.
- $email = $this->input->post('email');
- $password = md5($this->input->post('password'));
- $result = $this->user_model->login($email, $password);
- //This condition redirect you to homepage if you entered valid credentials
- if (count($result) !== 0) {
- $this->session->set_userdata('user_id', $email);
- redirect('home/view');
- } else {
- //This will redirect you to login page with error.
- $this->session->set_flashdata('message', 'Login Fail!!<br>Invalid username or password!!');
- redirect('login');
- }
- }
- }
- $this->db->select('*');
- $this->db->where($info);
- $this->db->from($table);
- $query = $this->db->get();
- if($query->num_rows() > 0){
- $row = $query->row_array();
- return $row;
- }
- }
- if(isset($_POST['login'])){
- $log['email'] = $_POST['email'];
- $log['password'] = $_POST['password'];
- $details = $this->User_model->fetchrowlogin($log,'candidate');
- if(count($details)){
- $ids = $details['id'];
- $email = $details['email'];
- $fname = $details['fname'];
- $this->session->set_userdata(array(
- 'custid' => $ids,
- 'emailid'=> $email,
- 'fname'=> $fname,
- ));
- redirect('http://localhost/test27/index.php/welcome/dashboard');
- }else{
- redirect(base_url().'front1');
- }
- }
- $this->load->view('front/login');
- }
Add Comment
Please, Sign In to add comment