<?
	/*
	** File: check_sid
	** Description: Keeps Session IDs valid
	** Version: 2.0
	** Created: 09/18/98
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	*/

	/* 
	** clean out old session ids
	** time()-3600 is 1 hour ago 
	*/
	$Query = "DELETE FROM session WHERE LastAction < '" .
		date("Y-m-d H:i:s", (time()-3600)) . 
		"'";
	$DatabaseResult = mysql_query($Query, $DatabaseLink);

	/*
	** find out who this is 
	*/
	if(isset($sid))
	{
		//we have sid, check if it's valid
		$Query = "SELECT User ";
		$Query .= "FROM session ";
		$Query .= "WHERE ID='$sid' ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
				
		if(mysql_numrows($DatabaseResult))
		{
			list($Session_User) = mysql_fetch_row($DatabaseResult);
		
			// update its time
			$Query = "UPDATE session SET LastAction = now() " .
				"WHERE ID='$sid'";
			mysql_query($Query, $DatabaseLink);

			if($Session_User > 0)
			{
				$UserInfo = GetUserInfo($Session_User);
			}

			if(USE_COOKIES)
			{
				//update session cookie lifetime
				setcookie("sid", $sid, time()+SESSION_LIFETIME);
			}

		}
		else
		{
			//bad sid, so generate a new one
			$ACTION="GENERATE_SID";
			$sid = "";
			
			if(USE_COOKIES)
			{
				//erase cookie
				setcookie("sid");
			}
		}
	
	}
	else
	{
		//missing sid, so generate a new one
		$ACTION="GENERATE_SID";
	}

?>