Asked  7 Months ago    Answers:  5   Viewed   38 times

I'm developing a website with CodeIgniter and have created a User and a session:

$user->first_name = 'Gerep';
$user->gender = 'M';
$user->age = '26';


$this->session->set_userdata('user', $user);

But when I try to access the session object:

echo $this->session->userdata('user')->first_name;

It returns me a error: Object of class __PHP_Incomplete_Class could not be converted to string

I have always worked like that and never had that problem.

Thanks!

 Answers

91

The class definition had not been loaded, when PHP tried to deserialize the object in the session.

You can solve your problem by employing Autoloading.

Wednesday, March 31, 2021
 
DMTintner
answered 7 Months ago
59

on linux ubuntu

For PHP5: sudo apt-get install php5-cgi

For PHP7: sudo apt-get install php7.0-cgi

Saturday, May 29, 2021
 
lewiguez
answered 5 Months ago
33

To get all session data you can use $this->session->all_userdata();

To print all your session variable use this line anywhere in your code :

echo '<pre>'; print_r($this->session->all_userdata());exit;
Saturday, May 29, 2021
 
daniel__
answered 5 Months ago
62

You can use the parse_url function, here is an example:

$uri = parse_url( $_SERVER['REQUEST_URI']);
$protocol = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';
$url = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . '?' . ( isset( $uri['query']) ?  $uri['query'] : '');

I did not see in your code where you get the script's filename, so I used $_SERVER['SCRIPT_NAME'].

Edit: My mistake, I did not see that you need to manipulate / remove the last $_GET parameter. Here is an example on how to do that using a method similar to the above in conjunction with parse_str. Note that this method will work regardless of the location of the lang parameter, it does not have to be the last one in the query string.

$protocol = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';

$params = array();
if( isset( $_SERVER['QUERY_STRING']) && !empty( $_SERVER['QUERY_STRING']))
{
    parse_str( $_SERVER['QUERY_STRING'], $params);
    $params['lang'] = 'anything';
    // unset( $params['lang']); // This will clear it from the parameters
}

// Now rebuild the new URL
$url = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . ( !empty( $params) ? ( '?' . http_build_query( $params)) : '');

Thanks to @Yzmir Ramirez for an improvement in the second version that eliminates the extraneous call to parse_url.

Saturday, May 29, 2021
 
Octopus
answered 5 Months ago
40

I found that the issue is with some earlier version of Codeigniter 3 and this is a bug already reported in their website.The underlying session bug has been fixed on:

  • 3.1.2
  • 3.1.3
  • 3.1.4

So in the latest version of Codeigniter 3 this issue doesn't happen.

Solutions:

If you are already in a faulty version codeigniter consider replacing system folder with latest version's one. Version 3.1.6 at the time writing this.

Monday, July 5, 2021
 
treeface
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :