-1

This is in a controller

class LandingPage extends CI_Controller {
    public function startEncryptedSession(){
    $this->load->library('session'); 
    $this->load->library('encrypt');    

    $this->load->view('index', array('session', $this->session));
}


}

How do I load this in the view index

view code

<?php echo $head; ?>

<body>
    <?php echo $guts; ?>
</body>

<?php echo $foot; ?>
<?php echo $session->userdata('session_key'); ?> 

I'm using code igniter MVC

When I load the view, it gives me:

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined variable: session</p>
<p>Filename: views/index.php</p>
<p>Line Number: 8</p>

</div><br />
<b>Fatal error</b>:  Call to a member function userdata() on a non-object 
tereško
  • 58,060
  • 25
  • 98
  • 150
CodeTalk
  • 3,571
  • 16
  • 57
  • 92
  • what do you mean by load that in the view index? That code should load the session and encrypt libraries from CodeIgniter and then load the index view. You should have an `index.php` file in your `views/` folder. – El Barto Mar 11 '12 at 02:22
  • and it should be incorporated with that view when that view is loaded? How do I check to make sure that function is running on load? – CodeTalk Mar 11 '12 at 02:24
  • Please see updated question. Any thoughts? – CodeTalk Mar 11 '12 at 02:32
  • possible duplicate of [Call to a member function on a non-object](http://stackoverflow.com/questions/254291/call-to-a-member-function-on-a-non-object) ... also: http://stackoverflow.com/a/12769983/727208 – tereško Feb 12 '13 at 21:47

1 Answers1

0

You need to pass the variables you want to use to your view. Something like this:

One way:

In your controller:

public function startEncryptedSession(){
    $this->load->library('session'); 
    $this->load->library('encrypt');
    $data = array('session_key' => $this->session->userdata('session_key'));
    $this->load->view('index', $data);
}

In your view:

<?php echo $session_key; ?> 

Another way:

In your controller:

public function startEncryptedSession(){
    $this->load->library('session'); 
    $this->load->library('encrypt');    
    $data = array('session' => $this->session->all_userdata());
    $this->load->view('index', $data);
}

In your view:

<?php echo $session['session_key']; ?> 

A word of advice: CodeIgniter's session class kinda sucks, since it stores data on a cookie and has a very low limit of data size (or at least that was how it worked last time I checked)... so you might want to handle it using PHP's default session functions or creating a class of your own. But I'll leave that for you to check, since it isn't what you were asking.

El Barto
  • 919
  • 1
  • 5
  • 18
  • I updated my answer, check this way. There might be a problem calling the userdata() method from the view. – El Barto Mar 11 '12 at 03:12
  • Yeah, both give the same error. I wonder what is wrong with the userdata() method.. – CodeTalk Mar 11 '12 at 03:16
  • Are you sure you tested with my last edit? It shouldn't give you the same error. Or if it does, it should be in the controller rather than the view, because the view isn't calling the userdata() method. – El Barto Mar 11 '12 at 03:25
  • Either way it says Undefined variable:session_key or Undefined variable:session based on which set of code I use.. – CodeTalk Mar 11 '12 at 03:31
  • I realized I was building the array incorrectly. I also assigned the array to a variable just in case. Try with my new edit, please. – El Barto Mar 11 '12 at 03:36
  • This is weird, because I'm following CI's [documentation](http://codeigniter.com/user_guide/general/views.html). You could try doing: `print_r(get_defined_vars())` in your view. – El Barto Mar 11 '12 at 03:42