(Sorry, due to copyright reasons, I can't remove the English in the following code, so I have to add some comments
=====================================================================
<?
/* ------------------------------------------------------------------------
* session_mysql.php
* ------------------------------------------------------------------------
* php4 mysql session handler
* version 1.00
* by ying zhang (ying@)
* last modified: may 21 2000
*
* ------------------------------------------------------------------------
* terms of usage:
* ------------------------------------------------------------------------
* you are free to use this library in any way you want, no warranties are
* expressed or implied. this works for me, but i don't guarantee that it
* works for you, use at your own risk.
*
* while not required to do so, i would appreciate it if you would retain
* this header information. if you make any modifications or improvements,
* please send them via email to ying zhang <ying@>.
*
* ------------------------------------------------------------------------
* description:
* ------------------------------------------------------------------------
* this library tells the php4 session handler to write to a mysql database
* instead of creating individual files for each session.
*
* create a new database in mysql called "sessions" like so:
*
* create table sessions (
* sesskey char(32) not null,
* expiry int(11) unsigned not null,
* value text not null,
* primary key (sesskey)
* );
*
* ------------------------------------------------------------------------
* installation:
* ------------------------------------------------------------------------
* make sure you have mysql support compiled into php4. then copy this
* script to a directory that is accessible by the rest of your php
* scripts.
* Be sure that your php4 has mysql support, and then copy this script to a directory related to your php script.
* ------------------------------------------------------------------------
* usage: (How to use)
* ------------------------------------------------------------------------
* include this file in your scripts before you call session_start(), you
* don't have to do anything special after that.
* Include this file into the file you want to use session, you must call session_start(), otherwise,
* It will be miserable, don't blame me for not telling you. This way you don't need to do any more work, and it's the same as you used to use session.
*/
$sess_dbhost = "localhost"; /* database server hostname */
$sess_dbname = "sessions"; /* database name */
$sess_dbuser = "phpsession"; /* database user */
$sess_dbpass = "phpsession"; /* database password */
$sess_dbh = "";
$sess_life = get_cfg_var("session.gc_maxlifetime");
function sess_open($save_path, $session_name) {
global $sess_dbhost, $sess_dbname, $sess_dbuser, $sess_dbpass, $sess_dbh;
if (! $sess_dbh = mysql_pconnect($sess_dbhost, $sess_dbuser, $sess_dbpass)) {
echo "<li>can't connect to $sess_dbhost as $sess_dbuser";
echo "<li>mysql error: ", mysql_error();
die;
}
if (! mysql_select_db($sess_dbname, $sess_dbh)) {
echo "<li>unable to select database $sess_dbname";
die;
}
return true;
}
function sess_close() {
return true;
}
function sess_read($key) {
global $sess_dbh, $sess_life;
$qry = "select value from sessions where sesskey = '$key' and expiry > " . time();
$qid = mysql_query($qry, $sess_dbh);
if (list($value) = mysql_fetch_row($qid)) {
return $value;
}
return false;
}
function sess_write($key, $val) {
global $sess_dbh, $sess_life;
$expiry = time() + $sess_life;
$value = addslashes($val);
$qry = "insert into sessions values ('$key', $expiry, '$value')";
$qid = mysql_query($qry, $sess_dbh);
if (! $qid) {
$qry = "update sessions set expiry = $expiry, value = '$value' where sesskey
= '$key' and expiry > " . time();
$qid = mysql_query($qry, $sess_dbh);
}
return $qid;
}
function sess_destroy($key) {
global $sess_dbh;
$qry = "delete from sessions where sesskey = '$key'";
$qid = mysql_query($qry, $sess_dbh);
return $qid;
}
function sess_gc($maxlifetime) {
global $sess_dbh;
$qry = "delete from sessions where expiry < " . time();
$qid = mysql_query($qry, $sess_dbh);
return mysql_affected_rows($sess_dbh);
}
session_set_save_handler(
"sess_open",
"sess_close",
"sess_read",
"sess_write",
"sess_destroy",
"sess_gc");
?>
=================================================================
Customize the interface when using dbm files
=================================================================
<?
/* ------------------------------------------------------------------------
* session_dbm.php
* ------------------------------------------------------------------------
* php4 dbm session handler
* version 1.00
* by ying zhang (ying@)
* last modified: may 21 2000
*
* ------------------------------------------------------------------------
* terms of usage:
* ------------------------------------------------------------------------
* you are free to use this library in any way you want, no warranties are
* expressed or implied. this works for me, but i don't guarantee that it
* works for you, use at your own risk.
*
* while not required to do so, i would appreciate it if you would retain
* this header information. if you make any modifications or improvements,
* please send them via email to ying zhang <ying@>.
*
* ------------------------------------------------------------------------
* description:
* ------------------------------------------------------------------------
* this library tells the php4 session handler to write to a dbm file
* instead of creating individual files for each session.
*
* ------------------------------------------------------------------------
* installation:
* ------------------------------------------------------------------------
* make sure you have dbm support compiled into php4. then copy this
* script to a directory that is accessible by the rest of your php
* scripts.
* Confirm that your php4 has dbm support. Copy this file in your php script directory.
* ------------------------------------------------------------------------
* usage:
* ------------------------------------------------------------------------
* include this file in your scripts before you call session_start(), you
* don't have to do anything special after that.
* Please include this file before calling session_start(). There is no need to do any work after that.
*/
$sess_dbm = "";
$sess_life = get_cfg_var("session.gc_maxlifetime");
function sess_open($save_path, $session_name) {
global $sess_dbm;
$sess_dbm = dbmopen("$save_path/$session_name", "c");
return ($sess_dbm);
}
function sess_close() {
global $sess_dbm;
dbmclose($sess_dbm);
return true;
}
function sess_read($key) {
global $sess_dbm, $sess_life;
$var = "";
if ($tmp = dbmfetch($sess_dbm, $key)) {
$expires_at = substr($tmp, 0, strpos($tmp, "│"));
if ($expires_at > time()) {
$var = substr($tmp, strpos($tmp, "│") + 1);
}
}
return $var;
}
function sess_write($key, $val) {
global $sess_dbm, $sess_life;
dbmreplace($sess_dbm, $key, time() + $sess_life . "│" . $val);
return true;
}
function sess_destroy($key) {
global $sess_dbm;
dbmdele($sess_dbm, $key);
return true;
}
function sess_gc($maxlifetime) {
global $sess_dbm;
$now = time();
$key = dbmfirstkey($sess_dbm);
while ($key) {
if ($tmp = dbmfetch($sess_dbm, $key)) {
$expires_at = substr($tmp, 0, strpos($tmp, "│"));
if ($now > $expires_at) {
sess_destroy($key);
}
}
$key = dbmnextkey($sess_dbm, $key);
}
}
session_set_save_handler(
"sess_open",
"sess_close",
"sess_read",
"sess_write",
"sess_destroy",
"sess_gc");
?>
=================================================================
There is no need to say more about how to use it, because these functions are called by the PHP engine and have nothing to do with us. We just need to match the above
Just set it up, you are still using the previous session function.
Just look at the following code:)
session customized test code
==================================================================
<?
/* ------------------------------------------------------------------------
*
* ------------------------------------------------------------------------
* php4 customer session handler test script
* version 1.00
* by ying zhang (ying@)
* last modified: may 21 2000
*/
/* default to dbm handler */
if (! isset($handler)) {
$handler = "dbm";
}
/* default action is increment */
if (! isset($action)) {
$action = "increment";
}
/* load up the appropriate session handling script, depending on the handler */
if ($handler == "dbm") {
include("session_dbm.php");
} elseif ($handler == "mysql") {
include("session_mysql.php");
} else {
echo "<li>unrecognized handler ($handler)";
die;
}
/* start the session and register a simple counter */
session_start();
session_register("count");
/* figure out what we should do, depending on the action */
switch ($action) {
case "increment" :
$count = isset($count) ? $count + 1 : 0;
break;
case "destroy" :
session_destroy();
break;
case "gc" :
$maxlife = get_cfg_var("session.gc_maxlifetime");
sess_gc($maxlife);
break;
default:
echo "<li>unknown action ($action)";
break;
}
?>
<h1>session test script</h1>
<ul>
<li>handler: <b><?=$handler?></b>
<li>action: <b><?=$action?></b>
<li>count: <b><?=$count?></b>
</ul>
<hr size=1>
<form>
<table>
<tr>
<td>handler:</td>
<td>
<select name="handler">
<option value="dbm">dbm</option>
<option value="mysql">mysql</option>
</select>
</td>
</tr>
<tr>
<td>action:</td>
<td>
<select name="action">
<option value="increment">increment</option>
<option value="destroy">session destroy</option>
<option value="gc">force garbage collection</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><br><input type="submit"></td>
</tr>
</table>
</form>
=======================================================================
3. Session application examples
The following examples are for reference only. You can customize or not, sessions, as you like
(1) Used for user authentication
<?
session_start();
$dbh = mysql_connect("localhost:3306","xxxx","xxxx");
mysql_select_db("znsoft");//Select a database
$query="select userid from reguser where userid='$userid' and pass='$pass' ";
//$userid $pass is the user name and password passed by the login form
$res=mysql_query($query,$dbh);
if($row=mysql_fetch($res))
{
$reguser=$row[0];
?>
<script>
alert("ok, buddy, welcome!");
</script>
<?
}
else
{
$reguser="";
?>
<script>
alert("sorry, you are not a registered user!");
</script>
<?
//Put the code yourself
}
session_register("reguser");
?>
Check if you are logged in on another page
================
<?
session_start();
if(isset($reguser)&&$reguser!="")//Are already logged in
{
echo "Welcome, buddy";
}
else//No login
echo "Please register";
?>
Exit function
===============================
<?
session_destroy();
//or $reguser="";
?>
(2) Used to pass variables
This program is used to pass variables between pages
<?
$name="";
if(!sesion_is_registered("name"))//The session variable is not registered
session_register("name");//Register variable name
?>
Page 2
===================
<?
echo $name;
//I don't want to use it anymore, delete it
if(session_is_registered("name"))// Whether to register, if already registered
session_unregister("name");//Of course it's deleted
?>