Reputation: 163
Is anyone here familiar with CodeIgniter sessions? I have a login script which is functional. On a successful login, a session is created. A user is redirected to thier home page. The new page should show thier username as it was stored in the session variables, but shows nothing at all.
The code from the login script:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
/**
* Index Page for this controller.
*/
public function index() {
$this->load->library('session');
$BB_DATA = array();
if(isset($_POST['login'])) {
// SELECT username, password FROM Bacon or bacon-like product. <-- Fuck you #nobanchancom
if(!empty($_POST['username']) && !empty($_POST['password'])) {
$q = sprintf('SELECT user_pk, username, pwhash, salt FROM tbl_user WHERE `username`=%s LIMIT 1;', $this->db->escape($_POST['username']));
$records = $this->db->query($q);
$username = $_POST['username']; // i added this
if($records->num_rows() == 1) {
$record = $records->row();
$test_hash = hash_password($record->salt . $_POST['password']);
if($record->pwhash === $test_hash) {
$new_salt = generate_salt();
$new_hash = hash_password($new_salt . $_POST['password']);
$q = sprintf('UPDATE tbl_user SET `pwhash`="%s", `salt`="%s" WHERE `user_pk`="%s" LIMIT 1;', $new_hash, $new_salt, $record->user_pk);
if($updates = $this->db->query($q)) {
$BB_DATA['messages']['information'][] = "Login successful.";
//add custom data to session
$this->session->set_userdata('username', "$username");
//$this->session->set_userdata('some_name', 'some_value');
// redirect to /users/home/
redirect(site_url() . 'users/home/'); exit();
} else {
$BB_DATA['messages']['critical'][] = "You have logged in successfully, but an error has occurred during login.";
}
} else {
$BB_DATA['messages']['critical'][] = "That username/password does not match.";
}
} else {
$BB_DATA['messages']['warning'][] = "That username/password does not exist.";
}
} else {
$BB_DATA['messages']['warning'][] = "You must enter a username/password.";
}
}
if(isset($_POST['register'])) {
// Redirect to /register/
redirect(site_url() . 'register/');
}
$this->load->view('login', $BB_DATA);
}
}
/* End of file login.php */
/* Location: ./application/controllers/login.php */
And then here is the code for the users landing page:
<?php
class Users extends CI_Controller {
public function home()
{
$this->session->userdata('username');
require_once(APPPATH . 'views/header.inc.php');//header
include(APPPATH . 'views/breadcrumbs.inc.php');//top breadcrumbs
echo ' <h1>Welcome'.$username.',</h1>';
//$username = $this->session->userdata('username');
echo $this->session->all_userdata();
//echo $username;
include(APPPATH . 'views/breadcrumbs.inc.php');//bottom breadcrumbs
require_once(APPPATH . 'views/footer.inc.php');//footer
}
}
?>
In the login script, I call codeigniters session library-
$this->load->library('session');
later, assuming everything went right, I add the $username variable to the session data. The user is redirected, and from there
$this->session->userdata('username'); should re establish the variable. It does not.
Suggestions?
NOTE: So, I used print_r(array_values($this->session->all_userdata()));
to print all the values in the array. It contained:
Array ( [0] => 4c46f32b249ed55e8439152d63365fed [1] => 75.134.164.245 [2] => Mozilla/5.0 (Windows NT; Win64; x64; rv:26.0) Gecko/20100101 Firefox/26.0 Waterfox/26.0 [3] => 1410568024 [4] => [5] => ottomatic )
So I am curious, Why did it add it as [5] rather than 'username'?
Upvotes: 0
Views: 1105
Reputation: 163
Thanks to a few tips in the right direction from @xd6_, I saw what was happening.
I had misread the documentation- you DO need to load the sessions on each page, unless it is in the /config/autoload.php array.
Once that was done, I was able to access the array element by name as
$username = $this->session->userdata('username');
Upvotes: 0