这个就是我看的那个文件内容:
<?
/* ------------------------------------------------------------------------ 
* session_mysql.php 
* ------------------------------------------------------------------------ 
* PHP4 MySQL Session Handler 
* Version 1.00 
* by Ying Zhang ([email protected]
* 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 dont 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 <[email protected]>. 

* ------------------------------------------------------------------------ 
* 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. 
*确信你的php4有mysql支持,然后把这个脚本拷贝到和你的php脚本有关的目录。 
* ------------------------------------------------------------------------ 
* USAGE:(使用方法) 
* ------------------------------------------------------------------------ 
* Include this file in your scripts before you call session_start(), you 
* dont have to do anything special after that. 
*包含这个文件到你要使用session的文件中,必须在调用session_start()之前,否则, 
*会很惨的,不要怪我没告诉你。 这样就不需要再做什么工作了,还和你以前用session的方法一样。 
*/ $SESS_DBHOST = "192.168.0.7"; /* database server hostname */ 
$SESS_DBNAME = "lqsh"; /* database name */ 
$SESS_DBUSER = "root"; /* database user */ 
$SESS_DBPASS = "duxin"; /* 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>Cant 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"); 
?> 

解决方案 »

  1.   

    这是个测试文件:
    <? 
    /* ------------------------------------------------------------------------ 
    * test.php 
    * ------------------------------------------------------------------------ 
    * PHP4 Customer Session Handler Test Script 
    * Version 1.00 
    * by Ying Zhang ([email protected]
    * Last Modified: May 21 2000 
    */ 
    /* default to DBM handler */ 
    if (! isset($handler)) { 
    $handler = "mysql"; 

    /* 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; 

    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> 
      

  2.   

    去掉session_mysql.php文件中最后一个空行。
    session_start();前不能有输出,包括空格和空行警告提示说
    Warning: Cannot send session cache limiter - headers already sent (output started at d:\mywork\lqsh\main\include\session_mysql.php:148) in d:\mywork\lqsh\main\include\session_test.php on line 28意思是
    在session_test.php 文件的28行即session_start();出发现嵌入文件session_mysql.php的148行处有输出,而148行正好是?>后的空行
      

  3.   

    sessiont_start()可以不放在页首。
    但是必须放在开始使用Session变量的前面。
    而且要保证sessiont_start()前没有输出。
      

  4.   

    最好是将session_start();放在页面,以避免不必要的麻烦!
      

  5.   

    sessiont_start()可以不放在页首,但要放在开始使用Session变量的前面,而且sessiont_start()前不准有输出语句,例如echo , print 等。