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

#1
[eluser]Uto[/eluser]
Hi,

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
Code:
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?

#2
[eluser]drewbee[/eluser]
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)

#3
[eluser]xzela[/eluser]
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:
Code:
function __construct() {
    parent::Controller();
    $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.

#4
[eluser]Uto[/eluser]
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

#5
[eluser]drewbee[/eluser]
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

and

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.

#6
[eluser]Uto[/eluser]
Working! Smile

The documentation is wrong though:

It shows:
Code:
class MY_Input extends CI_Input {

    function My_Input()
    {
        parent::CI_Input();
    }
}

while it should be:

Code:
class MY_Input extends Input {

    function My_Input()
    {
        parent::Input();
    }
}

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

#7
[eluser]drewbee[/eluser]
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.

#8
[eluser]Uto[/eluser]
Oh, ok Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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