[PHP] CodeIgniter Help

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by Shadow121, Mar 16, 2009.

  1. Shadow121

    Shadow121 Member

    Messages:
    901
    Likes Received:
    0
    Trophy Points:
    16
    I'm making a page render class for a thing i'm making but when I go to do $this->session->userdata('username'); it says I can't access it.

    the PageRender class is extending the controller so it can load view files for the template_header and template_footer. I also get the error above when I try to access the template parser class hence why i'm having it extend the Controller class instead. could someone help?

    PageRender:
    Code:
    <?php
    if (!defined('BASEPATH')){
        exit('No direct script access allowed'); 
    }
    class PageRender extends Controller{
        function PageRender(){
            parent::Controller();
        }
        function generateHeader($loggedUser){
            //$user = ($this->session->userdata('username')) ? $this->session->userdata('username') : 'Guest';
            $headerData = array(
                'pageHeading' => 'Nevux Ability Boards (1.4.6 Devel)',
                'cssFiles' => array("style.css"),
                'jsFiles' => array("jquery"),
                'metaData' => array(),
                'User' => $loggedUser,
                'RenderInstance' => $this
            );
            $this->load->view('templates/layout_header', $headerData);
        }
        function incorporateTemplate($templateData){
            
        }
        function generateFooter(){
            
        }
        function loadItems($type = null, $fileName = null, $metaKey = null, $metaData = null){
            
        }
    }
    ?>
     
  2. misson

    misson Community Paragon Community Support

    Messages:
    2,572
    Likes Received:
    72
    Trophy Points:
    48
    What's "it"? What's the actual error you're getting?

    As it says in the CodeIgniter user guide, make sure you load the session library, either via auto-loading or in the PageRender constructor:
    PHP:
        function PageRender(){
            
    parent::Controller();
            
    $this->load->library('session');
        }
     
  3. Shadow121

    Shadow121 Member

    Messages:
    901
    Likes Received:
    0
    Trophy Points:
    16
    Yes, it being the error.

    And the library is auto-loaded because I can access it in the boards controller. but instead of putting it in EVERY controller I want to declare the $User one time which is when it goes to generate the header area.
     
  4. misson

    misson Community Paragon Community Support

    Messages:
    2,572
    Likes Received:
    72
    Trophy Points:
    48
    That doesn't actually answer my questions. Again, what is the exact error you're getting? What is "it" that can't be accessed? For instance, if "it" is $this->session, then the session library isn't being loaded. If "it" is $this->session->userdata, then the CodeIgniter installation is probably messed up.

    When asking a question, please describe the problem concisely, including the software environment and precise error messages (you posted a minimal test case, which is very helpful). It's extremely hard to solve an issue if we don't know exactly what is going wrong.

    The best thing you can do for yourself is install XDebug on a test server and use Eclipse PDT to debug the application. Then you can examine variables &c. at the exact point the PHP script is failing.
     
  5. mephis

    mephis New Member

    Messages:
    39
    Likes Received:
    0
    Trophy Points:
    0
    Assuming the session library is loaded and you're only having issues accessing the session variable "username": do you have cookies enabled on your test browser? CodeIgniter's developers have decided to use cookies to store CI's session variables (don't ask me why!). I personally prefer to replace their original session library with one that uses native PHP sessions (you can find it on their forums/wiki)
     

Share This Page