• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Site-wide variables not available in controller/model files

#1
My website uses CI just for the admin part, so structure will look similar to:

-root
    sitewideinitfile.php
    various other files
     
    admin (folder)
        CI Application:
        index.php

All my non-CI website files include_once sitewideinitfile.php to have access to global settings like
Code:
$siteVars['debug_mode'] = true;
$siteVars['abspath']= 'absolute/path'
 and so on.

I would like to have these variables available in CI as well, however, I am not sure how to do that.
I understand I need to pass the data in views, but still not sure what is the best way to go to have them available in controllers/models.
The admin part of the website cannot be accessed directly (so a user HAS to login in the non-CI part of the website).

What I did/tried so far:
added to index.php: 

PHP Code:
session_start();
include_once(
'../sitewideinitfile.php');
$_SESSION['siteVars'] = $siteVars

in each controller:

Code:
function __construct() {
   $this->load->library('php_session');
   $this->php_session->start();
}

$data['siteVars'] = $this->php_session->get('siteVars');
$this->load->view('viewname', $data);

Questions:
1) Is it good practice to pass data around using $_SESSION? I don't really like it, I would rather have them loaded from file like it happens in the non-admin (non-CI) area of the website
2) Is it good practice to add those lines to 'index.php' file?
3) Is it good practice to start the session this way (and in that location, again index.php)?
4) Is the controller code above the right way to go?

So far, this approach seems to get the job done, but I didn't test a lot yet.

Thank you!
Reply

#2
(03-16-2016, 11:13 AM)bg17aw Wrote: 1) Is it good practice to pass data around using $_SESSION? I don't really like it, I would rather have them loaded from file like it happens in the non-admin (non-CI) area of the website

No.

(03-16-2016, 11:13 AM)bg17aw Wrote: 2) Is it good practice to add those lines to 'index.php' file?

No, and that wouldn't work anyway - they'd be outside of the controller's scope.

(03-16-2016, 11:13 AM)bg17aw Wrote: 3) Is it good practice to start the session this way (and in that location, again index.php)?

No.

(03-16-2016, 11:13 AM)bg17aw Wrote: 4) Is the controller code above the right way to go?

No, and you don't need that 'php_session' thing at all.

-----------------------------------

Easiest way is to use constants, because they aren't affected by scope visibility.
Reply

#3
(03-16-2016, 12:10 PM)Narf Wrote:
(03-16-2016, 11:13 AM)bg17aw Wrote: 1) Is it good practice to pass data around using $_SESSION? I don't really like it, I would rather have them loaded from file like it happens in the non-admin (non-CI) area of the website

No.

(03-16-2016, 11:13 AM)bg17aw Wrote: 2) Is it good practice to add those lines to 'index.php' file?

No, and that wouldn't work anyway - they'd be outside of the controller's scope.

(03-16-2016, 11:13 AM)bg17aw Wrote: 3) Is it good practice to start the session this way (and in that location, again index.php)?

No.

(03-16-2016, 11:13 AM)bg17aw Wrote: 4) Is the controller code above the right way to go?

No, and you don't need that 'php_session' thing at all.

-----------------------------------

Easiest way is to use constants, because they aren't affected by scope visibility.

Thanks for your answer, but I have to say I was hoping for more info on how to achieve the desired effect, and why it is not good practice, not just that it is not.

session_start() in index.php works, from what I can see, and all the code I pasted above works.
Why do you say it won't work? 

Why don't I need 'php_session' ? How can I can the session variables without using php_session?

About defining constants: there would be a lot of them and not sure that would be good practice wither, I never seen this approach in any code I inspected. 

If you can elaborate a bit on your answers, why I should not put session_start in index.php, where should I put it (I definetely need the session started at some point, as the login took place already so the user data is in the session), how can I get session data without php_session etc I would really appreaciate it. 
I tried searching and googleing on this issue and some were recommending the Session approach, which you say it is not a good idea. Confused now... However, so far, the code above is working fine in production.
Reply

#4
For sessions, use CI's session library: https://codeigniter.com/user_guide/libra...sions.html

Sessions are primarily intended for storing temporary, user-specific data which needs to be preserved between pages and/or visits to the site. In general, you should store as little information as possible in the session, and site-wide data should never be stored in the session.

Most of this data could be stored in a config file: https://codeigniter.com/user_guide/libra...onfig.html

You could then load the config file in your base controller (MY_Controller): https://codeigniter.com/user_guide/gener...core-class

Once you've loaded the config file, you can assign the data to properties of your base controller. Public properties would then be available in your controllers and models, while protected properties would only be available in your controllers (assuming your controllers extend MY_Controller, or some other controller which extends MY_Controller). Other code (e.g. libraries) could access the public properties through get_instance(), or you could pass the necessary values.
Reply

#5
(03-18-2016, 03:45 AM)bg17aw Wrote: Thanks for your answer, but I have to say I was hoping for more info on how to achieve the desired effect, and why it is not good practice, not just that it is not.

session_start() in index.php works, from what I can see, and all the code I pasted above works.
Why do you say it won't work? 

Why don't I need 'php_session' ? How can I can the session variables without using php_session?

About defining constants: there would be a lot of them and not sure that would be good practice wither, I never seen this approach in any code I inspected. 

If you can elaborate a bit on your answers, why I should not put session_start in index.php, where should I put it (I definetely need the session started at some point, as the login took place already so the user data is in the session), how can I get session data without php_session etc I would really appreaciate it. 
I tried searching and googleing on this issue and some were recommending the Session approach, which you say it is not a good idea. Confused now... However, so far, the code above is working fine in production.

If you put this into your index.php file:

(03-16-2016, 11:13 AM)bg17aw Wrote:
Code:
$siteVars['debug_mode'] = true;
$siteVars['abspath']= 'absolute/path'

I can assure you, you can't just access $siteVars from inside a model, controller, etc. That's what I meant when I said it wouldn't work.

These are not session variables and nothing in your original post suggests that you need to use session variables - you're obviously thinking that using a session is a solution to the problem, but the problem itself is not "how to read session vars?" ...

For "debug_mode" and "abspath", constants are surely the easiest solution.

As for why you don't need that 'php_session' thing ... well, why the hell did you think you do need it in the first place?
You don't need a library to use PHP sessions. And if you're manually calling session_start(), you surely aren't starting the session via an external library in the first place, so ... just access $_SESSION directly.
Reply

#6
besides the principle debate - if you want to have your variables in your CI Environment - a possible idea would be:

create a site-vars.php in your config folder
something like (you've to adjust the include path)

PHP Code:
include_once("../sitewideinitfile.php");
$config $siteVars

after that just simply call in one of your Controller functions and print it out - you'll see this works

PHP Code:
$this->config->load("site-vars"true);
echo 
$this->config->item("debug_mode","site-vars"); 
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.