note 8581 added to Session handling functions

From: Date: Tue, 19 Sep 2000 00:37:00 +0000
Subject: note 8581 added to Session handling functions
Groups: php.notes 
Request: Send a blank email to php-notes+get-5198@lists.php.net to get a copy of this message
If you are having problems using sessions in a MySQL database, here are 2 things I found that help. The first is NT and the second Linux but could be useful anywhere. NT: The mysql session read routine reads the saved data in to memory to the point where it will correctly indicate a variable is registered but does not actually set the variable. Solution: Just save the variables as extra fields in the database. This gives you complete control over the variables. It is useful when you are saving a small number of important variables on every page. It also gets around some problems with session code doing unpredictable things will variables containing null or zero length strings. For each variable you want to control, add a column to the session table using phpMyAdmin or similar then add the variable to the SQL in both your session read handler and your session write handler. Remember to mention the variables in global statements at the top of each handler so the variables will be visible within the handler functions. Linux: You find the session code just plain refuses to call the mysql session write handler. Solution: Just call the session write handler yourself. To stop the session handler randomly performing duplicate writes, just slip a dummy write handler in to session_set_save_handler. I used a function that just returns true and does nothing else. function nowrite($x, $y) { return(true); } This gives you control on any system and lets you effectively close the session before the end of page. I might even call the real write function session_close(). (A mysql session close usually does nothing so will not conflict. If your session close handler does have something in it, you might want to move that code up to the write handler.) Some of the advantages of a session_close(): - If you have problems with databases, passwords or SQL, you can output diagnostics on the page. (The default session write occurs too late in the page processing to produce diagnostics on the page.) - You can reduce the database usage within your page by processing all the control information in one spot at the top of the page. This is great for pages that make a decision at the start of the page then spend a long time reading various databases to display the results of the decision. You just save your variables straight after the decision then switch to whatever database you want to read based on the decision. Your new session_close() needs the session_id so save the session id in your read handler. Your new session_close() can also perform other tasks such as logging changes to a variable to track usage and navigation problems. Combining both techniques gives you more control and reduces your reliance on the documentaion. It also lets you use all the features of your database to store specific variable types. One disadvantage is you have to add new variables to the table. You might choose to use both techniques and reserve table columns for variables used in every page. Another disadvantage is unset variables occupy space in the database. In MySQL the overhead for empty strings and some other variable types is minimal so is not a problem in most cases. http://www.php.net/manual/ref.session.php

« previous php.notes (#5198) next »