Create Table Sessions

Submitted by:Pitter Alderson

Date added:29 April, 2013

Category:MySQL

Saving the IP as Int we save a lot of space and permit do beautiful selects

Tags: sessions

Code Snippet:

    CREATE TABLE Sessions (
session_id INT UNSIGNED NOT NULL AUTOINCREMENT,
ip_address INT UNSIGNED NOT NULL,
session_data TEXT NOT NULL,
PRIMARY KEY (session_id),
INDEX (ip_address)
) ENGINE=INNODB

// INSERT a new dummy record
INSERT INTO Sessions VALUES (NULL, INET_ATON('192.168.0.2'), 'some session data');

//Find all sessions coming FROM a local subnet
SELECT
sessin_id,
ip_address as ip_raw,
INET_NTOA(ip_address) as ip,
session_data
FROM Sessions
WHERE ip_address
BETWEEN INET_ATON('192.168.0.1')
AND INET_ATON('192.168.0.255')
 
 

Comments