3

If PHP is stateless then even if you declare variables as static they do not retain their values across different runs. So is it pointless to try and monitor your sessions using a class such as below as sessions maintain state across runs but PHP user code does not.

class session
  {
  protected static $ses_id ="";
  public static function start()
    {
    self::$ses_id = session_start();
    }
  public static function is_start()
    {
    return self::$ses_id;
    }
  public static function finish()
    {
    self::$ses_id = 0;
    $_SESSION=array();
    if (session_id() != "" || isset($_COOKIE[session_name()]))
      {
      setcookie(session_name(), '', time()-2592000, '/');
      }
    session_destroy();
    }
  }
BenMorel
  • 34,448
  • 50
  • 182
  • 322

2 Answers2

2

Your class as you've written it doesn't add any new functionality, it simply wraps existing functionality (provided by the session_* functions). This kind of thing can be worthwhile if you need to mediate or control access to the session. Only you can judge whether it's worthwhile in your own app.

alexantd
  • 3,543
  • 3
  • 27
  • 41
2

From the documentation:

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie

This means that you are able to resume a session and $ses_id will remain the same, while the session is valid, and if it hasn't been destroyed.

Wrapping it in a class can be a nice way of encapsulating the functionality of a session and accessing it in an OOP way. You can also extend the class to make an easy way to save relevant session data that is then accessible throughout your app in a more sensible way than using the provided session_* functions.

thetaiko
  • 7,816
  • 2
  • 33
  • 49
  • True..but I can't verify wether or not the session id has changed across runs as my code does nor retain state. –  Dec 26 '11 at 18:55
  • @stack.user.0 - use a database – thetaiko Jan 02 '12 at 14:34
  • What I recently learned is that session_start, when using cookies to track the session, is sub-domain dependent, b.c. a cookie is only valid for its domain or sub-domain....I learned this the hard way...but a session set for www.hostname.com is different then a session set for hostname.com(no wwww. in front). –  Jan 02 '12 at 16:22