Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Customer {
- protected $customer_id;
- public function __construct() {
- $this->ci =& get_instance();
- $this->ci->load->library('session');
- $this->ci->load->helper('string');
- if ($this->ci->session->userdata('customer_id')) {
- $this->ci->db->select('*');
- $this->ci->db->from($this->ci->db->dbprefix . 'customer');
- $this->ci->db->where('customer_id', $this->ci->session->userdata('customer_id'));
- $customer_query = $this->ci->db->get();
- if ($customer_query->num_rows()) {
- $this->customer_id = $customer_query->row()->customer_id;
- }
- } else {
- $this->autologin();
- }
- }
- public function postLogin() {
- if ($this->verifyPassword() == true) {
- $this->ci->db->select('*');
- $this->ci->db->from($this->ci->db->dbprefix . 'customer');
- $this->ci->db->where('username', $this->ci->input->post('username'));
- $customer_query = $this->ci->db->get();
- if ($customer_query->num_rows() > 0) {
- $data = array(
- 'customer_id' => $customer_query->row()->customer_id,
- 'is_logged_in' => true
- );
- $this->ci->session->set_userdata($data);
- if ($this->ci->input->post('remember')) {
- $this->createAutologin($customer_query->row()->customer_id);
- }
- return true;
- } else {
- return false;
- }
- }
- }
- protected function verifyPassword() {
- $input = $this->ci->input->post('password');
- $hashed = $this->getPassword();
- if (password_verify($input, $hashed)) {
- return true;
- } else {
- return false;
- }
- }
- protected function getPassword() {
- $this->ci->db->select('*');
- $this->ci->db->from($this->ci->db->dbprefix . 'customer');
- $this->ci->db->where('username', $this->ci->input->post('username'));
- $customer_query = $this->ci->db->get();
- if ($customer_query->num_rows() > 0) {
- return $customer_query->row()->password;
- } else {
- return false;
- }
- }
- public function autologin() {
- var_dump($this->readAutologin());
- }
- protected function createAutologin($customer_id) {
- $token = random_string('alnum', 32);
- $data = array(
- 'token' => $token
- );
- $this->ci->db->where('customer_id', $customer_id);
- $customer_update = $this->ci->db->update('customer', $data);
- if ($customer_update) {
- $data = array(
- 'customer_id' => $customer_id,
- 'token' => $token,
- 'time' => time()
- );
- $customer_autologin_insert = $this->ci->db->insert('customer_autologin', $data);
- if ($customer_autologin_insert) {
- setcookie('remember', $token, $this->ci->config->item('start_cookie_time'), '/', '.localhost', false, true);
- }
- }
- }
- protected function readAutologin() {
- $this->ci->db->select('*');
- $this->ci->db->from($this->ci->db->dbprefix . 'customer_autologin');
- $this->ci->db->where('customer_id', $this->customer_id);
- $customer_autoload_query = $this->ci->db->get();
- if ($customer_autoload_query->num_rows() > 0) {
- return $customer_autoload_query->row_array();
- } else {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement