1

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 :

    <?php
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 "<script>alert('Gagal login: Cek username, password!');history.go(-1);</script>";
        }
    }

}

?>

This is the model for the controller :

<?php
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

2
  • print your query by using $this->output->enable_profiler(TRUE); and try to run that query in db console. it will be easy to debug. Commented Feb 17, 2016 at 8:41
  • @Kamran Did you mean to change the return with $this->output->enable_profiler(TRUE); ? Commented Feb 18, 2016 at 1:31

3 Answers 3

2

I know that this is an old question but still the right answer is that you had to read the config.php file carefully and you would understand that you missed some points of the configuration to save sessions the right way.

/* 'sess_save_path'
|
|   The location to save sessions to, driver dependent.
|
|   For the 'files' driver, it's a path to a writable directory.
|   WARNING: Only absolute paths are supported!
|
|   For the 'database' driver, it's a table name.
|   Please read up the manual for the format with other session drivers.
|
|   IMPORTANT: You are REQUIRED to set a valid save path!
*/

Your session path was not right!

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

change from

 $query = $this->db->get_where(`pegawai`, $data);
 return $query;

to

 $this->db->where($data);
 $query=$this->db->get('pegawai');
 return $query;

8 Comments

sorry but there's another 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 'SET last_activity = 1455757641, user_data = 'a:4:{s:9:\"user_data\";s:0:\"\"' at line 1 UPDATE SET last_activity = 1455757641, user_data = 'a:4:{s:9:\"user_data\";s:0:\"\";s:9:\"logged_in\";s:12:\"Sudah Loggin\";s:3:\"nip\";s:10:\"1234567890\";s:5:\"akses\";s:5:\"admin\";}' WHERE session_id = 'f4f0eae180cfca66af04e6cd2dbb2f5a' Filename: libraries/Session.php Line Number: 306
before that, there's an error because I didn't make a key for encryption session, but after I make the key, there's an error just like that
I use backtick to call pegawai table and there's another error just like the first... It's realy I have an error in session?
delete records from ci_sessions table
how can I delete it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.