I am running a Codeigniter 2.0 web app, and I am using the sessions library with the DB option on, so for every connection on my website I have a MySQL table called 'ci_sessions'
that stores:
Session_id
- IP address
- User_agent
- Last_activity
- User_data
And I have two issues:
- Google bot with IP
66.249.72.152
- Cron with IP
0.0.0.0
Everytime my server runs the cron or every time the google bot crawls my page a new session is created. So I have hundreds of identical sessions with the IP 0.0.0.0
and hundreds with the IP 66.249.72.152
.
Does anyone know how to stop the bot or the cron from creating sessions? Or if this can not be done... does anyone know how to automatically delete these sessions every once in a while? Thanks!
In case you want to look at this using the
user agent
I created a library drop in, to block bots by their user-agent, using a simple$this->agent->is_robot()
check for drop in session extension.https://github.com/donjakobo/codeigniter-clean-sessions
A possible solution could be to implement a blacklist of IP's that a session should not be created for.
I have created two methods of doing this:
application/config/config.php
system/libraries/session.php
The config file contains an array of IP addresses to ignore.
In the session library, I have updated the constructor, so that it checks to see if the current IP address exists in the above config setting and if so, skips the code that creates or updates the session.
In addition to this, in
_sess_gc
I have added new where_in statement that will delete all session rows that have an IP that matches those in the config. If you implement the constructor method, you don't really need this part.I suggest not to modify core files, I recommend to extend the session class, so do the next thing create MY_Session.php file in ./app/libraries/ and paste the next code
this works
Alter your ci_sessions table and add the UNIQUE constraint to ip_address field. That way there will be no duplicate sessions..
A more complicated approach would be to use a trigger on insert. That will check whether the ip address is among the two addresses that you mentioned above. If yes, then donot insert in the db..