Jump to content

Proper Usage Of Session


longki

Recommended Posts

Hello, I just used Codeigniter here. And i'm just a beginner. In this case, i want to make a homepage. If guests open the homepage, the main menu will be :

-Home

-Gallery

-About

-Contact Us

 

But, if admin open the homepage (After log in), the main menu will be :

-Home

-Gallery

-About

-Contact Us

 

So, I tried to use this code FOR EACH CONTROLLER :

public function index()
    {
        $this->load->helper('html');
        $this->load->helper('url');
        
        if($this->session->userdata('logged_in'))
        {
             $session_data = $this->session->userdata('logged_in');
             $data['title'] = 'Home - Rubalang';
             $data['username'] = $session_data['username'];
             $data['id'] = $session_data['id'];
             $data['name'] = $session_data['name'];
             $data['position'] = $session_data['position'];
             $this->load->view('templates/header', $data);
             $this->load->view('templates/menu1');
             $this->load->view('home/home1');
             $this->load->view('templates/footer', $data);
        }
        
        else{
            $data['title'] = 'Home - Rubalang';
            $this->load->view('templates/header', $data);
            $this->load->view('templates/menu1');
            $this->load->view('templates/menuadmin');
            $this->load->view('home/home1');
            $this->load->view('templates/footer', $data);
        }
    }

We can see, I've made 3 sections of menu. menu1 and menuadmin. I do this because if i combine thoose two menu, i can't separate which menu can be seen by Admin, and which menu can be seen by user/guests.

 

But Actually it's tedious and troublesome. I have to write that code on every controller (Because that is main menu)

Is there any proper way to implement that ?

 

Link to comment
Share on other sites

Personally, I would create a Template model that has a view function. Then, call the template model with the main template you want to load (home/home1), and let the Template object handle figuring out if the user is logged in or not, and showing the appropriate views. One slightly more complicated way of implementing this would be something like this: http://jeromejaglale.com/doc/php/codeigniter_template

Link to comment
Share on other sites

Sorry, "Template" here is a helper ?

 

as what i've understnad, in the template we have some functions that call menu1 (Menu that can be seen by user : Home, About, Contact Us, Gallery), and the other function is function that call menu2 (Menu that can be seen by admin : Home, About, Contact Us, Gallery, Settings).

Edited by longki
Link to comment
Share on other sites

  • 4 weeks later...

I'm sorry Ben, I still do not understand enough to construct my own Library. I have used some instruction in your link, and get nothing. Here is my code. I think it's so tedious and troublesome to use this method.

 

 

 

Here is my core controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_HeaderController extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		
		if(!($this->uri->segment(1)))
		{
			$this->data['tithead'] = 'Home - Paluadv';
			
		}else if($this->uri->segment(3)){
			if($this->uri->segment(2) == "blog"){
				$this->load->model("paneldata");
				
				$data = $this->paneldata->blogview($this->uri->segment(3));
				if($data){
					$this->data['tithead'] = $data[0]->title;
				}
			}else{
				$this->data['tithead'] = ucfirst($this->uri->segment(3)) . ' - Paluadv';
			}
		}else if($this->uri->segment(2)){
			
			$this->data['tithead'] = ucfirst($this->uri->segment(2)) . ' - Paluadv';
			
		}else if($this->uri->segment(1)){
			
			$this->data['tithead'] = ucfirst($this->uri->segment(1)) . ' - Paluadv';
			
		}
		
		
	}
}

Here is my actual Home Controller inherited from MY_HeaderController (my own controller)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends MY_HeaderController {

	public $data;

	public function __construct()
	{
		parent::__construct();
		//$this->load->library('Template');
	}
	
	public function index()
	{			
	
        if ( ! $this->session->userdata('admin'))
        { 
	    $this->load->view('template/header/home.php',$this->data); //Repetition
	    $this->load->view('containt/home');                        //Second Repetition
            $this->load->view('template/footer/home.php'); //If not admin, call this view
        }else
	{
            /*Because in administration.php (additional view for admin) there is additional css, i use this
            code. */
	    $this->data['css'] = 'footer_acl';
	    $data['tithead']=$this->data['tithead'];
	    $data['css']=array($this->data['css']);
	    $this->load->view('template/header/home.php',$data);  //Repetition
	    $this->load->view('containt/home');                   //Second Repetition
	    $this->load->view('template/footer/administration.php'); //If admin, call this view
	}
		
	}
	
	function logout() {
         //remove all session data
         $this->session->unset_userdata('admin');
         $this->session->sess_destroy();
         redirect(base_url(), 'refresh');
     }
}

Here is my header view.

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $tithead  ?></title>

<?php echo link_tag('public/css/mine/style.css'); ?>
<?php echo link_tag('public/css/kickstart.css'); ?>
<?php echo link_tag('public/css/mine/adminstyle.css'); ?>
<?php
	if(isset($css)){ 
		foreach($css as $c): 
			echo link_tag('public/css/mine/' . $c. '.css');
		endforeach; 
	}
?>
<?php echo '<script src="' .  base_url() . 'public/js/jquery.min.js"></script>'?>
<?php echo '<script src="' .  base_url() . 'public/js/kickstart.js"></script>'?>


</head>

<body>
	//Some HTML STUFF

To Identify whether the user is admin or not, I just use simple validation. But, as you see, there are some repetition code, and that is not efficient. As you tell me to use my own library, and i got trouble in that.

 

Can you give me clue how to make library about this case ?

 

 

Btw, here is my footer for admin

<footer>
        	<div class="tengah">
            	<div class="col_6">
                	<p>Site powered by Kickstart. Is created and developed by Andrew Skiolsa</p>
                    <p></p>
                </div>
                
                <div class="col_6">
                	<p class="right">Copyright ® Andrew Skiolsa 2014 New York. All right Reserved</p>
                </div>
                
                <div class="acl col_6">
                	<?php echo anchor(base_url() . 'panel',"Administration Control Panel"); ?> | <?php echo anchor(base_url() . 'home/logout',"Log Out"); ?>
                </div>
            </div>
        </footer>
    </div>
    
</body>
</html>

and for usual user

        <footer>
        	<div class="tengah">
            	<div class="col_6">
                	<p>Site powered by Kickstart. Is created and developed by Andrew Skiolsa</p>
                    <p></p>
                </div>
                
                <div class="col_6">
                	<p class="right">Copyright ® Andrew Skiolsa 2014 New York. All right Reserved</p>
                </div>
            </div>
        </footer>
    </div>
    
</body>
</html>

Edited by longki
Link to comment
Share on other sites

This link has all the info on creating a library, including the basic template for a library file, loading the library (you can either use load->library(), or you probably want to consider autoloading it), and calling a method within the library: https://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

 

Basically, you just need to create a "load" method that will accept the view parameter. For example:

 

public function load($view) {   }

 

Inside that method, you determine whether or not the user is an admin user, and then load the appropriate views. So you'd call it something like this:

 

$this->Template->load('containt/home');

 

and then within the method, it would check if you are an admin user, and load all the appropriate views for either situation. This way, you reduce repetition and have one central location that controls what header/footer views are displayed. In your controllers, you'd just need one Template->load line, rather than multiple $this->load->view.

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...