Welcome Guest, Not a member yet? Register   Sign In
username is not displaying, SESSION?
#1

[eluser]solid9[/eluser]
hi guys

I'm trying to store and display the username through variable session.

<b>storing 'username'session variable:</b>
Code:
if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
   { //if the login is successful
     //store username into session
     //redirect them back to the home page
    
    $this->session->set_userdata('username', $this->input->post('identity'));
    $this->session->set_flashdata('message', $this->ion_auth->messages());
    redirect($this->config->item('base_url'), 'refresh');
   }


<b>displaying 'username' session variable:</b>
Code:
function login()
{
     //$data_user['username'] = $this->input->post('identity');
  $this->data['username'] = $this->session->userdata('username');
  echo 'user: '. $this->data['username'];
     $this->data['header'] = $this->load->view('header', $this->data, TRUE);
     $this->data['midnav'] = $this->load->view('body_m_frontpage', null, TRUE);  
     $this->load->view('front_page', $this->data);
}

But it won't display the username.
It's not displaying the currently login username?
What is wrong with my codes?

Thanks in advanced.


#2

[eluser]Ayeyermaw[/eluser]
Can't see anything wrong with the code so the question is this:

Does the "if" statement in your first block of code run true and redirect back to the home page?
If it does then all should be well.

This also could be the problem I had a while back where the ci_session table was generating a new session on each page. That would cause this exact behaviour. I think it was because the user_agent field was too small
Make sure you have the latest version of the ci_session table that codeigniter needs (current version has the size of the user_agent field set to 120)

#3

[eluser]solid9[/eluser]
Anyone would like to help?
Why the session 'username' is not stored?

Thanks again in advanced.
#4

[eluser]Kyle Johnson[/eluser]
Are any sessions being stored?

If yes, verify that each page load isn't getting a new session ID.

If they are getting a new session ID, verify that your database (assuming you are using database storage for sessions) has user_agent as at least varchar(120). (this was my problem a week ago)

Also, I'm not used to storing single bits of session data, so I'm thinking you might not be storing anything in the 'username' session field.

Check out my code for storing some session data after they successfully authenticate:
Code:
$session_data = array(
                'acl_user_id' => $result->userid,
                'acl_username' => $result->username,
                'acl_customer_id' => $result->customerid
            );
            $this->session->set_userdata($session_data);
#5

[eluser]solid9[/eluser]
Hi guys

How to get the latest version of the ci_session table?
By the way How do I check the ci_session version I'm using?

Thanks in advanced.

Sorry noob.
#6

[eluser]Kyle Johnson[/eluser]
Actually that's a really good question. The easiest way is to turn on profiling for a controller.

At the bottom (again assuming you're using database sessions) you will see one or two queries. Refresh the page a few times and see if the session_id changes.

Code:
SELECT *
FROM ci_sessions
WHERE session_id =  '8f2a6ed99a6eb151a083adaa887d7ce3'
AND user_agent =  'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'

Refresh and compare.
#7

[eluser]CroNiX[/eluser]
[quote author="solid9" date="1329619444"]Hi guys

How to get the latest version of the ci_session table?
By the way How do I check the ci_session version I'm using?

Thanks in advanced.

Sorry noob.
[/quote]
Just compare the schema of the session table you are using in your database to the one in the user guide for sessions. If you are using a different version of CI, compare it to the one in that user guide (the one that comes with the version you downloaded)
#8

[eluser]InsiteFX[/eluser]
Code:
// CI Version
echo CI_VERSION;

// Session Table New CI 2.1.0
-- ------------------------------------------------------------------------

--
-- Table structure for CodeIgniter `ci_sessions`.
--
DROP TABLE IF EXISTS `ci_sessions`;

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(120)                      NOT NULL,
  `last_activity` int(10)      unsigned DEFAULT 0   NOT NULL,
  `user_data`     text,
  PRIMARY KEY (`session_id`),
  KEY `last_activity_idx` (`last_activity`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;

-- ------------------------------------------------------------------------
--  `user_data` text,       COMMENT - maximum length of 65535 characters.
--  `user_data` mediumtext, COMMENT - maximum length of 16777215 characters.
--  `user_data` longtext,   COMMENT - maximum length of 4294967295 characters.
-- ------------------------------------------------------------------------




Theme © iAndrew 2016 - Forum software by © MyBB