WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Warning: Unknown(): A session is active. You cannot change the session module's ini settings at this time. in Unknown on line 0
Categories : PHP, Sessions, Databases, MySQL Click here to Update Your Picture
Dan Parry
Date : Feb 25th 2006
Grade : 1 of 5 (graded 1 times)
Viewed : 5318
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Dan Parry
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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