Welcome Guest, Not a member yet? Register   Sign In
Session Library troubled with database
#1

[eluser]Unknown[/eluser]
Hallo!

I don't know if this is really a bug, but I hope I will help somebody with this.

My job runs on LAMP with PHP 5.2.6 and MySql 5.0.51b-community-nt.

Setting on application/config/config.php
Code:
$config['sess_use_database']    = TRUE;

when I try my site i have only this:

Code:
A Database Error Occurred

Error Number: 1364

Field 'user_data' doesn't have a default value

INSERT INTO `sessdata` (`session_id`, `ip_address`, `user_agent`, `last_activity`) VALUES ('f9b292089de94d6883c638705765892c', '127.0.0.1', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.', 1270279689)

To solve this I modified the method sess_create in library Session.php

Before:
Code:
function sess_create()
    {
        $sessid = '';
        while (strlen($sessid) < 32)
        {
            $sessid .= mt_rand(0, mt_getrandmax());
        }

        // To make the session ID even more secure we'll combine it with the user's IP
        $sessid .= $this->CI->input->ip_address();

        $this->userdata = array(
                            'session_id'     => md5(uniqid($sessid, TRUE)),
                            'ip_address'     => $this->CI->input->ip_address(),
                            'user_agent'     => substr($this->CI->input->user_agent(), 0, 50),
                            'last_activity'    => $this->now
                            );


        // Save the data to the DB if needed
        if ($this->sess_use_database === TRUE)
        {
            $this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata));
        }

        // Write the cookie
        $this->_set_cookie();
    }

After:
Code:
function sess_create()
    {
        $sessid = '';
        while (strlen($sessid) < 32)
        {
            $sessid .= mt_rand(0, mt_getrandmax());
        }

        // To make the session ID even more secure we'll combine it with the user's IP
        $sessid .= $this->CI->input->ip_address();

        $this->userdata = array(
                            'session_id'     => md5(uniqid($sessid, TRUE)),
                            'ip_address'     => $this->CI->input->ip_address(),
                            'user_agent'     => substr($this->CI->input->user_agent(), 0, 50),
                            'last_activity'    => $this->now,
                            'user_data'    => ''
                            );


        // Save the data to the DB if needed
        if ($this->sess_use_database === TRUE)
        {
            $this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata));
        }

        // Write the cookie
        $this->_set_cookie();
    }


I don't like to modify the CI original code, but this works.

Does anybody have any opinion about this?


P.S.: Sorry for my english... :-(
#2

[eluser]Wuushu[/eluser]
Or you could change the database schema to set the user_data field default value NULL
#3

[eluser]Unknown[/eluser]
[quote author="Wuushu" date="1270299690"]Or you could change the database schema to set the user_data field default value NULL[/quote]

Absolutely right! :red:

But the User Guide ( http://ellislab.com/codeigniter/user-gui...sions.html ) says this:

Code:
CREATE TABLE IF NOT EXISTS  `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(16) DEFAULT '0' NOT NULL,
user_agent varchar(50) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id)
);
:down:




Theme © iAndrew 2016 - Forum software by © MyBB