CodeIgniter questions: native PHP sessions, code flow, placement issues?

I am just starting with CodeIgniter and I am trying to hash my regular modules / functions to make them work within the framework of MVC. I have a few specific questions for those who have a strong CodeIgniter background:

MEETINGS

A CodeIgniter session stores client-side session data in a cookie that does not work for me. I know there are several replacements for it, or I could create my own library / helper; but I just don't see any benefit from using $_SESSION .

If I just use $_SESSION , will I have problems with the rest of the framework? Any other part of the structure depends on the use of the CodeIgniter session?

I am a bit strange to go beyond the scope for something so basic, but it’s pretty convenient for me to use simple PHP. I basically just want to use CodeIgniter for MVC and apply the more modular aspect to my projects.

FLOW AND CONFIGURE

I have several configuration items that need to be done before almost anything.

For example, let's say I have the APP_LIVE constant, which is set as true / false based on the name of the current server. This should happen very early, since it will install paths, error reports, CodeIgniter system and application folders, etc.

The problem is that system_folder and application_folder (which will be installed based on which server the code is running on) are installed first in the index.php file before any of the configurations are loaded.

In addition, I have functions that check things in the URL and can be redirected before the page ever loads. For example, some pages should indicate the presence of www. in the URL (for SEO), track branches, visitor sources, advertising flags, etc.

Where is the best place for things that should happen very early? I know that there is a configuration file, startup file, constant file, etc., but it is too late for some elements. Is it a good practice to just put these things at the top of the main index.php file or include them in the global configuration file? Again, I feel that I am going beyond the scope, and wondering if I am not doing this because I do not have a clear understanding yet?

AREA / FUNCER HEADER

Like most people, I have a header, navigation, footer, etc. I'm used to just adding them to the files that are included in my page template. I believe that I can do the same just by making them look and including them in my main pageview. Is this the best way to go? Some of them need some data; for example, which page they use for navigation, etc. What is the best way to handle navigation, general header / footer, etc.?

+4
source share
5 answers

The recently released CI 1.7 handles sessions in the database (if you use one).

However, CI is intended to be loosely coupled, so you should not notice any serious problems if you decide to use $ _SESSION instead.

For your header / footer / navigation, you can create (for example) headerview.php, footerview.php and contentview.php and pass the data to your views by doing something like this in the controller:

 $data['title'] = 'about us'; $data['content'] = 'hello world!'; $this->load->view('headerview', $data); $this->load->view('contentview', $data); $this->load->view('footerview'); 

Basically, you can treat these views just like include, but with the added benefit that you can change the variables inside. I would avoid evoking other views from within the views, but it can only be me.

I made additions to index.php myself once or twice to set the initial values, etc., and never had a problem with it.

Congratulations on your choice of framework; I am sure you will not be disappointed .;)

+3
source

In each controller, you can have several lines load-> view, but I personally think that this is connected. I highly recommend you take a look at the hooks in CodeIgniter, where you can define functions that will automatically run after each controller / method (a great example of AOP).

+1
source

Actually, the $ _SESSION array seems to be disabled, so you cannot use your own PHP sessions (at least 1.7). However, in the CodeIgniter wiki there is a session class that uses its own php sessions - you can use it the same way as the other, but it only stores session_id in a cookie. Here is: http://codeigniter.com/wiki/Native_session/

+1
source

@lacho I created my own auth library on $ _SESSION. and it works great on 1.7.

I believe that $ _SESSION is much safer, as CI sessions are cookies that are stored on the client side, which are classified as β€œuser-transmitted data” that cannot be trusted.

0
source

You can try using native using your own session class

http://www.moreofless.co.uk/using-native-php-sessions-with-codeigniter/

0
source

All Articles