Reputation: 1
I'm using Perl CGI and Apache::Session in my web application.
When setting up an Apache::Session using tie like this:
tie %session, 'Apache::Session::MySQL', $id, {
DataSource => 'dbi:mysql:apache_session',
UserName => 'root',
Password => '',
LockDataSource => 'dbi:mysql:apache_session',
LockUserName => 'root',
LockPassword => ''
};
How do I catch the error that occurs when $id is not a valid session in the database? I need to create a new session ID instead of having the application die with a 500 http error.
This situation occurs when the user has an outdated cookie with a session ID that has been removed from the database.
Upvotes: 0
Views: 310
Reputation: 70732
If the $id
variable has a value of undef
, Apache::Session creates a new session ID. Now if $id
is defined but does not appoint a valid session here then an exception will be thrown.
One way you could do this is to wrap tie
in an eval
block.
eval {
tie %session, 'Apache::Session::MySQL', $id, {
DataSource => 'dbi:mysql:apache_session',
UserName => 'root',
Password => '',
LockDataSource => 'dbi:mysql:apache_session',
LockUserName => 'root',
LockPassword => ''
};
};
If an exception is thrown, you can check to see whether the message indicates that the session isn't valid.
if ( $@ ) { ... }
Upvotes: 1