Pull my SQL commit 

I added the new Primary key (ip_address, user_id)

Not tested yet :P

Edit: Added PHP part.
basic tests... I haven't check the need of all CREATE INDEX statements.