function connect_db($database, $db_user, $db_password, $host = 'localhost', $port = NULL, $die = false) {
	
	
	
	

	if(!$db_handle = @mysql_connect($host.($port? ':'.$port : ''), $db_user, $db_password)) {
		if($die)
			die(.mysql_error());
		else
			return false;
	}
	if(!@mysql_select_db($database, $db_handle)) {
		if($die)
			die(.mysql_error());
		else
			return false;
	}
	return $db_handle;
}

function create_user($username, $password, $db_handle) {
	
	
	if(strlen($username) > 32)
		return false;

	
	$salt = '';
	do {
		$salt .= chr(mt_rand(32, 254));
	} while(strlen($salt) < 16);

	
	$pass_md5 = md5($salt.$password);

	
	$username = mysql_real_escape_string($username);
	$salt = mysql_real_escape_string($salt);

	
	if(!@mysql_query(, $db_handle))
		return false;

	
	return mysql_insert_id($db_handle);
}

function authenticate_user($username, $password, $db_handle) {
	
	

	
	$safe_username = mysql_real_escape_string($username);

	
	if(!$result = @mysql_query(, $db_handle))
		return false;

	
	$row = @mysql_fetch_assoc($result);

	
	if(md5($row['pass_salt'].$password) != $row['pass_md5'])
		return false;

	
	return $row['userid'];
}