• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Two issues: Checking login status and mail CR/LF


I have found two problems while starting to use CI, well, the first one is not actually a problem but a doubt, cause I'm not sure how to do it better:

I have a site requiring login, so default controller is login.php and it takes care of everything. When the login is correct the method _loginok() is called, and that method starts a session, and adds the "userid" to that session.

The rest of the controllers require the "userid" on the session, or they cannot work (shoudl be redirected to login page if someone tries to load those pages).

I have thought about doing a "check" function on every controller, so I can check if the session var exists and if not, do something like
header('Location: /index.php')
or similar. But of course that means that I have to remember to do it at the beginning of every method (and that's dangerous as I can forget it).

Also I have thought about doint it on the _remap() method, but I cannot find how telling the remap method to call the default method except if I find there is no session.

Maybe there is another better way o do this, but I cannot find something suiting on docs or FAQ.

The second problem is about CR/LF on mails, I have tried everything, even I've created the email.php on the config folder, and tried all the combinations, but I still can find no way for it to work, I allways get "rn" wherever I write "\r\n", "r" when I write "\r", etc.
Any tips?

For your first issue, extend the main controller. Create a function in the construct() method that does this check for the user id. Just remember to put an exception in it for the login page.

MY_Controller extends Controller


As for the config file, is the first letter captial or lowercase? I had an issue with the configuration not working as it wasn't being read in. I can't remember which way it is now, but if it is currently lowercase, change it to Email.php or visa-versa (email.php)

drewbee is correct. You should roll some sort of custom made library and insert it into all of your constructors. I do exactly what he has suggested.

Here's what I've done for all of my controllers:
function __construct() {
    $this->load->library('authorize'); //load the authorize lib
    $this->authorize->isLoggedIn(); //check to see if the user is logged in.

in fact, you could probably autoload the library itself. That way you don't have to load it in each constructor and save a line per controller.

just a suggestion.

Nice fast answers, many thanks.

Of course, the constructor, how could I pass that over... Smile

So I can either extend the Controller class and then make all my controllers but the login one be of the extended class, or I can add the custom constructor to all controllers.

About the config, I'll try to rename to Email.php, as currently is whole lowercase.

Thanks again Smile

Yup. Exactly. I actually have extended my main controller with two methods:

require_login() - Requires user to be logged in to use the current view


require_logout() - requires user to be logged out to use the current view

I then call as required in either the main constructor (if required on all methods), or each indidivual method as needed.

Working! Smile

The documentation is wrong though:

It shows:
class MY_Input extends CI_Input {

    function My_Input()

while it should be:

class MY_Input extends Input {

    function My_Input()

At least with the Controller class it works the second way.

Right. This is OK. The controller class isn't labeled quite the same as the libraries, helpers etc. In all other cases, you would expect to see MY_*** extends CI_***

With the actual controllers and models, the class is simply Controller, or Model.

Oh, ok Smile

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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