|
|
|
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
|
|
| bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | 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 | | | A login page that require username, password and userlevel. Categories : PHP, Security, Sessions, MySQL, Databases | | | Simple PHP Form Auto Generation based on MySQL query Categories : PHP, Form Processing, Databases, MySQL, Sessions | | | PHP-CSL PHP Code Snippet Library, A very handy application designed to save you many hours by storing all your code snippets, classes and functions. Categories : PHP, Utilities, MySQL, Databases, To PHP | | | phpMyAdmin is intended to handle the adminstration of MySQL
over the web. Categories : Databases, MySQL, Complete Programs, PHP | | | Simple database class Categories : PHP, PHP Classes, MySQL, Databases | | | Link Manager for Link Exchangers Categories : PHP, PHP Classes, Databases, MySQL, CURL | | | This program will take data from a user via a web based form, validate it, show it
to the user for re-validation, and finally insert it into the database. Plenty of
sanity checking on the fields in the form.
Categories : MySQL, HTML and PHP, PHP, Complete Programs, Databases | | | formating MySQL Timestamp to get a propper Output.
related : Timestamp, ereg_replace, mysql, substr Categories : MySQL, PHP, Date Time, Databases | | | PHP Transfer data from text file to Mysql Table Categories : PHP, PHP Classes, Filesystem, Databases, MySQL | | | Shopping Cart e-Commerce Solution Categories : Complete Programs, PHP, MySQL, Databases | | | Education Center is a set of PHP-scripts to administer a corporate education and examination system via Internet/intranet written in PHP for MySQL.
Categories : PHP, Databases, MySQL, Complete Programs | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | | | Authorize Me! An authentication script. Categories : MySQL, Databases, Authentication, PHP | |
|
|