I'm currently using Codeigniter 3.xx and I trying to make a multilevel login using session checking. I got some tutorial from another website, but there's an error with query.
By the way this is for the controler :
defined('BASEPATH') OR exit('No direct script access allowed');
class Auth extends CI_Controller {
public function index() {
$this->load->view('login');
}
public function cek_login() {
$data = array('nip' => $this->input->post('nip'),
'password' => $this->input->post('password')
);
$this->load->model('user_m');
$hasil = $this->user_m->cek_user($data);
if ($hasil->num_rows() == 1) {
foreach ($hasil->result() as $sess) {
$sess_data['logged_in'] = 'Sudah Loggin';
$sess_data['nip'] = $sess->nip;
$sess_data['akses'] = $sess->akses;
$this->session->set_userdata($sess_data);
}
if ($this->session->userdata('akses')=='admin') {
redirect('admin');
}
elseif ($this->session->userdata('akses')=='member') {
redirect('user');
}
}
else {
echo "alert('Gagal login: Cek username, password!');history.go(-1);";
}
}
}
?>
This is the model for the controller :
defined('BASEPATH') OR exit('No direct script access allowed');
class User_m extends CI_Model {
public function cek_user($data) {
$query = $this->db->get_where(`pegawai`, $data);
return $query;
}
}
?>
Then I got this error :
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE nip = '21212121' AND password = 'user'' at line 2
SELECT * WHERE nip = '21212121' AND password = 'user'
Filename: C:/xampp/htdocs/simpeg/application/models/User_m.php
Line Number: 7
The things that I know I don't use MariaDB Engine for this table, and it should be logged in as user if that run correctly.
Can anybody explain what was the wrong with this syntax?
By the way I'm using newest XAMPP for PHP 7 and Codeigniter in this project
UPDATE
After I tried some code, I thought that I have some problem with the session configuration.
This is my config.php for session:
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
And this is my configuration in Session.php in system folder :
class CI_Session {
var $sess_encrypt_cookie = FALSE;
var $sess_use_database = FALSE;
var $sess_table_name = '';
var $sess_expiration = 7200;
var $sess_expire_on_close = FALSE;
var $sess_match_ip = FALSE;
var $sess_match_useragent = TRUE;
var $sess_cookie_name = 'ci_session';
var $cookie_prefix = '';
var $cookie_path = '';
var $cookie_domain = '';
var $cookie_secure = FALSE;
var $sess_time_to_update = 300;
var $encryption_key = '';
var $flashdata_key = 'flash';
var $time_reference = 'time';
var $gc_probability = 5;
var $userdata = array();
var $CI;
var $now;
In the new version of Codeigniter (which one I use), whenever I tried to autoload session there always asked me to put some encryption key for the session, so after I put the encryption key there's an error in the query about session like this:
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SET last_activity = 1455761673, user_data = 'a:2:{s:9:\"user_data\";s:0:\"\"' at line 1
UPDATE SET last_activity = 1455761673, user_data = 'a:2:{s:9:\"user_data\";s:0:\"\";s:3:\"nip\";s:10:\"1234567890\";}' WHERE session_id = '316bdf2f67b16beafbe30b56bca3833d'
Filename: libraries/Session.php
Line Number: 306
解决方案
I'm sorry but, I've found the answer for my problem...
After I tried to download newest Codeigniter again, and replace it in my project, and re-config my project, in result my query was read and successful executed, but the session didn't. So I tried to debug my code for 2 days, and I found (just about 15 minutes ago) there's something strange in my controller for admin/user, there wasn't <?php defined('BASEPATH') OR exit('No direct script access allowed'); but instead <?php session_start(); so I changed it and problem solved. Oh if anyone found
libraries/Session.php
DELETE IT!
That's the first reason that I have this error
Error Number: 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'WHERE nip = '21212121' AND password = 'user'' at
line 2 SELECT * WHERE nip = '21212121' AND password = 'user' Filename:
C:/xampp/htdocs/simpeg/application/models/User_m.php Line Number: 7
Thanks for you all who helped me... :D