I will use PHP 4 sessions to make all functions well organized and easy to reuse and for the database system MySQL, although the database interface will be created as a separate class so it can be modified according to everyone's needs.
The SQL structure
Here is a sample SQL structure I've used for this example. The ses_id is the session id (sid) which is also the primary key for the table. I used an INT(11) for it managed manually through the script, again this is just my choice, I've explain some others in the previous part.
The start_time and last_time represent time when the session was created (time of the first page load for this user) and the time of the last page load. Both are stored using UNIX timestamps - because integer operations are generally faster and easier to use.
I've decided to identify an unique session by the ses_id (if available), the user's IP and his browser agent. User's IP address is stored in the user_ip field and user_agent contains the agent information (truncated to 255 characters).
I've added some other fields as well for user identification. The field logged_in shows if a user is logged in or not (1 = logged in, 0 = not logged in) - for this field you can use an ENUM('0', '1'), I've chosen TINYINT for simplity. If a user is logged in, the user_id field is used to identify the user's id as well. The user_rank field is a bit redundant, but that's because I wanted to have information about a user's privileges through the sessions table alone. In this case I've asigned each page a minimum rank required in order to view that page. This allows me to assign different user ranks required in order to view each page. This, of course is just my choice, and everyone can implement it according to their own needs.
There is also a lang field, which is used to store user's preferred language.