|
|
|
Having read an article on custom sessions stored in a database, I decided to rewrite the class and build it into a site framework I was working on. All went well until I started using a header() redirect on the login form, When the redirect completed I frequently received this message:
Duplicate entry '**ID_REMOVED**' for key 1
Warning: Unknown(): A session is active. You cannot change the session module's ini settings at this time. in Unknown on line 0
This also started appearing if a page was refreshed quickly. Thinking it was a configuration problem I started researching and contacted my hosting company
The class function for writing the session looked like this
| <?
/* Write new data to database */
function _write($ses_id, $data) {
$session_sql = "UPDATE " . $this->ses_table
. " SET ses_time='" . time()
. "', ses_value='$data' WHERE ses_id='$ses_id'";
$session_res = @mysql_query ($session_sql, $this->dblink) or die (mysql_error());
if (!$session_res) {
return FALSE;
}
if (mysql_affected_rows ()) {
return TRUE;
}
$session_sql = "INSERT INTO " . $this->ses_table
. " (ses_id, ses_time, ses_start, ses_value)"
. " VALUES ('$ses_id', '" . time()
. "', '" . time() . "', '$data')";
$session_res = @mysql_query ($session_sql, $this->dblink) or die (mysql_error());
if (!$session_res) {
return FALSE;
} else {
return TRUE;
}
}
?> | |
The problem turned out to be within this function itself rather than the configuration
The problem stemmed from the way the return from UPDATE is handled and the scripts reliance on the mysql_affected_rows() command
If the UPDATE command is executed on a record and no values are altered the command exits with a value of 0 meaning that the result of mysql_affected_rows() is false
This occurs on redirects and fast refreshes because the time value hasn't changed so no values are altered in the row, mysql_affected_rows() is false and the function continues as no condition is met to return a value... the following INSERT then causes the problem
The solution was do execute a simple SELECT to get the number of rows for the session ID and if mysql_affected_rows() evaluated to false but the number of records is 1 return true
I hope this helps someone as it took me a while to figure this out
|
|
| This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | A login page that require username, password and userlevel. Categories : PHP, Security, Sessions, MySQL, Databases | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | color codes for positive and negative numbers Categories : PHP, MySQL, Databases, HTML | | | Authorize Me! An authentication script. Categories : MySQL, Databases, Authentication, PHP | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Linked comboboxes with php-mysql & javascript Categories : PHP, Java Script, Databases, MySQL | | | mysql_escape_string Categories : PHP, MySQL, Databases, Strings | | | Automatically printing the contents of an sql table in MySQL. Categories : MySQL, PHP, HTML and PHP, Databases | |
|
|
|