<?php
// Note: you can only change the $name variable
// and this class is not compatible with phplib's page and auth class.
// i just use the same interface as phplib.

class Session {

## public instance variables
var $name = "LSSID"; ## Session name

## private instance variables
var $mode ="cookie"; ## 'get' or 'cookie', default is 'cookie'
var $id; ## Unique Session ID

function Session() {
$this->start();
}

function halt($error_msg) {
print( "Session Error: " . $error_msg . "<br>");
exit;
}

function register($things) {
if ( !session_register($things) ) {
$this->halt("error in registering '$things'");
}
}

function unregister($things) {
if ( !session_unregister($things) ) {
$this->halt("error in unregistering '$things'");
}
}

function delete() {
session_destroy();
}

function url($url){
if ($this->mode == "cookie") return $url;

$sid = $this->name . "=" . $this->id;
$url = str_replace("?".$sid, "?", $url);
$url = str_replace("&".$sid, "", $url); ## cut session from url if exists
$url = ereg_replace("[&?]+$", "", $url); ## trim trailing "&?"
$url .= ( strpos($url, "?") != false ? "&" : "?" ) . $sid;
return $url;
}

function purl($url) {
print $this->url($url);
}

function self_url() {
global $PHP_SELF, $QUERY_STRING;

return $this->url($PHP_SELF.
((isset($QUERY_STRING) && ("" != $QUERY_STRING)) ? "?".$QUERY_STRING : ""));
}

function pself_url() {
print $this->self_url();
}

function add_query($qarray, $q_str = "") {
global $QUERY_STRING;

if ( $q_str == "" ) $q_str = $QUERY_STRING;
if ( ("" != $q_str)
|| ($this->mode != "cookie")) {
$sep_char = "&";
} else {
$sep_char = "?";
}

while (list($k, $v) = each($qarray)) {
$qstring = $sep_char . urlencode($k) . "=" . urlencode($v);
$sep_char = "&";
}

return $qstring;
}

function padd_query($qarray) {
print $this->add_query($qarray);
}

function get_id() {
return $this->id;
}

function start() {
global $HTTP_COOKIE_VARS, $HTTP_GET_VARS;

if ( $this->name != "" ) {
session_name($this->name);
}
else {
$this->name = session_name();
}
session_start();
$this->id = session_id();
if ( isset($HTTP_COOKIE_VARS[$this->name]) ) { // if cookie is set, set mode to cookie
$this->mode = "cookie";
}
else { // we never know it's the first time to store cookie or user client dont permit cookie, so we use get anyway
$this->mode = "get";