1

After validating user input and storing login credentials I start a session by calling session::start and then reload the index.php file by calling general::reload. The I use session::is_start() to determine which page to load from the index file.

I don't think this is working correctly as I always get the same page loaded - b1e.htm.

My concern is that my static class session does not maintain its value between the AJAX/PHP call and the reload index.php call.

Similar posting - here

index.php

  include 'b2.php'; 

  if(session::is_start())
    {
    include 'b2e.htm';  // user is logged in
    }
  else
    {
    include 'b1e.htm'; // user is not logged it
    }

Snippet - session:start() and session::is_start();

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();
    }
  }

Snippet - general::reload()

class general
  {
  public static function reload()
    {
    $uri = 'http://';
    $uri .= $_SERVER['HTTP_HOST'];
    header('Location: '.$uri.'/host_name');
    }
Community
  • 1
  • 1
  • Unless you have not called `session::start()`, `session::is_start()` will always return an empty string. Is that what you want? – hakre Jul 14 '11 at 18:54
  • And what is your problem then? As long as you don't start the session it won't be started. Please elaborate. – hakre Jul 14 '11 at 19:16
  • PHP is stateless. If one run is over, the next run knows nothing about the previous run. You can use sessions to overcome this however, then you need to start the session at the beginning of each run. I guess you want to set a flag in the session on run1 you can check for on run2 then. But that's not checking if a session exists. You understand the difference? – hakre Jul 14 '11 at 20:01
  • Because it's stateless, we use a session. The session keeps data between requests, is a tool/method to add a sort of state in the stateless requests. See **[Introduction to Sessions (PHP Manual)](http://www.php.net/manual/en/intro.session.php)** - and sure you can encapsulate the functionality into a class of it's own. I think you just need to add some code to your class to get it working. – hakre Jul 14 '11 at 20:26

2 Answers2

1

You can encapsulate and consolidate session functionality, but you can not fully monitor sessions with a class as php user code is stateless (even when using static keyword)...i.e. it will depend upon SESSION to retain state.

0

You need to call your session_start(); to actually start the session on each page.

mjdth
  • 6,536
  • 6
  • 37
  • 44
  • You can still use a class, but your class needs to put things into the _SESSION variable or some other persistent store. – davidtbernal Jul 14 '11 at 21:12