3

I'm creating a user class to handle my logins. As I wish to set the sessions inside the class after the username and password are validated, do I have to use session_start() at the top of the class, inside the public function where the sessions are to be set, or where the instance is created? Perhaps it could go inside function _construct()?

This is how I would like to call the class:

<php

include('user_class.php');
$user = new user;
$user->login($username,$password);


?>
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Sam
  • 51
  • 1
  • 2
  • 4
  • @Supericy Thanks for your response. Would it be possible to have the session setting in a private function which is then called IF user->login(...) returns TRUE? Or do you you think it's best to call the session setting function manually using a public function? – Sam Jan 07 '13 at 22:41

3 Answers3

7

You can just add session_start(); at the top of the file you're including the class in.

So

<?php
  session_start();
  include('user_class.php');
  $user = new user;
  $user->login($username,$password);
?>

would work.

Rob
  • 1,840
  • 2
  • 12
  • 19
  • Thanks for your reply. If I use session_start() as you suggest, can I use "SESSION['x'] = $x" as normal inside the class? Also, is there no way the session_start() could be included inside the class so I don't have to initialise it whenever the class is used? – Sam Jan 07 '13 at 22:37
4

Next to your user-class create yourself a session-class as well.

The user-class then is just storing itself into the session class and does not need to take care about calling session_start or not, that's the job of the session-class.

<php

include('session_class.php');
include('user_class.php');


$session = new session;

if ($session->hasRegisteredUser()) {
    $user = $session->getRegisteredUser();
} else {
    $user = new user;        
    $user->login($username, $password);
    $session->setRegisteredUser($user);
}

Does this answer your question or do you need now to know how to do it with the session class?

hakre
  • 193,403
  • 52
  • 435
  • 836
  • Thanks for offering your help hakre. Would it not be simplest to just call session_start() inside the user class? If not, could you explain some more about using a session class. Do you have any examples perhaps? – Sam Jan 07 '13 at 22:44
  • 2
    @Sam: No. Imagine you want more user classes at once. They all start a session? Instead you need some session you can store some stuff into and retrieve it back. It's more simple to keep these things separated, just write a little bit more code in the beginning, you will quickly see that this is better as your user object grows. - Both objects are related - but not the same. So they should become close friends, but don't bake them together in one. – hakre Jan 07 '13 at 22:46
  • And to find out if a session has been started already or not, here is a useful function: http://php.net/session_status – hakre Jan 07 '13 at 22:47
  • So, once my user class has checked that the username and password are correct, it can return an array with all the data I need inside the sessions (i.e. Username, ID, Name) into a session_class object which will set the sessions for me? This is beginning to make sense now. In theory, couldn't the session_class just be a function I include? – Sam Jan 07 '13 at 22:53
  • Even better, your user-object itself will keep it's data. And you just pass the user object to the session and register it in there. So then the session knows if / or if not it has a user regestiered with it. For example, if the session does not have a user, you can display the link to the login form, but if it has, you display the link to the logout form. - so technically the user object could know if it is logged in or not, the session object only takes care to serialize and store the user-object into the session. – hakre Jan 07 '13 at 22:56
  • How can I actually store the user data in the object and then just parse it into the session class? Can I just keep it inside a public variable? – Sam Jan 07 '13 at 23:20
  • @Sam: Yes, in a public variable, or also in a private one. Like you want. It's probably easier to start with a public one. I normally use [getters and setters](http://stackoverflow.com/a/8955492/367456). – hakre Jan 08 '13 at 02:12
4

Yes you can use sessions inside your classes because sessions are global variables in php.

Code Example(adding a new session variable):

<?php
    class sessionControle{
        ...
        ...
        public function addSession($index, $value){
            $_SESSION[$index] = $value;
            return $_SESSION[$index];
        }
    }
?>

in your main php file you can include the function $_SESSIONS are global Code in your main file:

<?php
    session_start();
    include_once("myClass.php");
    $Se = new sessionControle;
    echo $Se->addSession('User', 'Crx');

    //Double check here !!
    echo $_SESSION['User'];
?>

Output: Crx

Yassine Sedrani
  • 619
  • 5
  • 12