<?
	/*
	** File: GENERATE_SID
	** Description: create an sid and create row in session table
	** Version: 1.0
	** Created: 03/15/99
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	*/

	//include module for getting a User ID based on login/password
	include("$Application_Root/modules/include/getUserID");

	
	$charpool = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
			
	$Query = "SELECT seed FROM seed";
	$DatabaseResult = mysql_query($Query, $DatabaseLink);
	list($seed)  = mysql_fetch_row($DatabaseResult);

	srand($seed);

	$seed = Rand() + intval(substr(microtime(),2,4));

	$Query = "UPDATE seed SET seed = $seed";
	mysql_query($Query, $DatabaseLink);

	$count=0;
	$sid = "";
	while($count < 16)
	{
		$sid = $sid . substr($charpool, 
			rand()%(strlen($charpool)),1);
		$count++;	
	}

	$tUserID = 0;

	//if using autologin, maybe we can log the user
	//in based on login/password stored in cookies
	if(USE_AUTOLOGIN)
	{
		//check for username/password from cookie
		$tUserID = getUserID($cookie_al, $cookie_ap, TRUE);

		if($tUserID > 0)
		{
			//get user info
			$UserInfo = GetUserInfo($tUserID);
			$ActionResults = "Welcome back, " . $UserInfo["FirstName"] . " " . $UserInfo["LastName"] . "!";
		
			//refresh cookies
			setcookie("cookie_al", $cookie_al, time()+AUTOLOGIN_LIFETIME);
			setcookie("cookie_ap", md5($cookie_ap), time()+AUTOLOGIN_LIFETIME);
		}
	}

	//create session in database
	$Query = "INSERT INTO session VALUES ('$sid', $tUserID, now())";
	mysql_query($Query, $DatabaseLink);
	
	if(USE_COOKIES)
	{
		//set cookie
		setcookie("sid", $sid, time()+SESSION_LIFETIME);
	}

?>