user656925
user656925

Reputation:

Sessions are stateful, PHP user code is not

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

Upvotes: 1

Views: 1166

Answers (2)

user656925
user656925

Reputation:

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.

Upvotes: 1

mjdth
mjdth

Reputation: 6536

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

Upvotes: 0

Related Questions