会话处理程序类给出“页面没有正确重定向”

时间:2014-08-29 07:12:44

标签: php session

我正在使用会话处理程序类,如下所示

Session_Handler.php

<?php
/**
 * Session Handler License
 * ===========
 *
 * @author      
 * @copyright   
 * @link       
 * @version     1.0
 */
class sessionHandle {  
    // session-lifetime
    var $lifeTime;
    // mysql-handle
    var $dbHandle;
    var $is_debug = false;

    function open($savePath = '', $sessName = '') {
     global $server, $database, $user, $password;

       $this->lifeTime = get_cfg_var("session.gc_maxlifetime");
       $dbHandle    = @mysql_connect($server,$user,$password);
       $dbSel       = @mysql_select_db($database,$dbHandle);

        if($this->is_debug)
        {
            if($dbHandle)
            $t = 'connected';
            else
            $t = 'not connected';

            $this->dataLog($server.','.$user.','.$password.','.$database.','.$t);
        }   

       // return success
       if(!$dbHandle || !$dbSel)
           return false;

       $this->dbHandle = $dbHandle;
       return true;
    }
    function close() {
        $this->gc(ini_get('session.gc_maxlifetime'));
        return @mysql_close($this->dbHandle);
    }

    function read($sessID) {
        global $registry,$globalSettings;
        $query = "SELECT session_data AS d FROM `conf_event_data_log_session`
                            WHERE session_id = '$sessID'
                            AND session_expires > ".time();
       if($this->is_debug)
        {
            $this->dataLog($query);
        }                        
        $res = mysql_query($query, $this->dbHandle);

        // return data or an empty string at failure
        if($row = mysql_fetch_assoc($res)){
            if($this->is_debug)
            {
                $this->dataLog('DATA ->>>>>'."\n".$row['d']);
            }   
            return $row['d']; 
        }
        return "";
    }

    function write($sessID, $sessData) {

        // new session-expire-time
        global $registry, $globalSettings;

        $current_event = addslashes($_REQUEST['current_event']);
        $q = "SELECT * FROM conf_events WHERE url = '" . $current_event . "'";
         if($this->is_debug || true)
        {
            $this->dataLog($q);
        }
         $query                 = mysql_query($q);

        $current_event_id   = mysql_fetch_row($query);
         $event_id              = $current_event_id[0];
         //$sessData_new            =  $this->unserialize_php($sessData);
         $sessData_new          = $sessData_new['z93my8/SztfFxc/d0w=='];//attendee_data encoded array
         $attendee_id           = $sessData_new['attendee_id'];


         $browser_arr   = $this->getBrowser();
         $device_type   = 'Computer';
        //MOBILE DETECT
        $detect = new Mobile_Detect();
        if ($detect->isMobile() ||  $detect->isiOS() || $detect->isAndroidOS() || $detect->isTablet() ) 
        {
            if($detect->version('iPad'))
            {
                $device_type        = 'iPad';
                $browser_arr['platform']        = $detect->version('iPad');
            }
            if($detect->version('iPhone'))
            {
                $device_type        = 'iPhone';
                $browser_arr['platform']        = $detect->version('iPhone');
            }
            if($detect->version('Android'))
            {
                $device_type        = 'Android';
                $browser_arr['platform']        = $detect->version('Android');
            }

             if ($detect->version('Windows Phone')) {
                $device_type        = 'Windows Phone';
                $browser_arr['platform']        = $detect->version('Windows Phone');
            }

        }


        $newExp = time() + $this->lifeTime;
        $res = mysql_query("SELECT * FROM `conf_event_data_log_session`  WHERE session_id = '$sessID'",$this->dbHandle);

        if(mysql_num_rows($res)) {
            $query = "UPDATE `conf_event_data_log_session`  SET session_expires = '$newExp', 
                            session_data = '".$sessData."',  event_id    ='$event_id',  attendee_id  ='$attendee_id',login_update    ='".date('Y-m-d H:i:s')."', 
                            delete_test  ='".mysql_real_escape_string($current_event)."',ip_address = '".mysql_real_escape_string($this->getClientIP())."',device_type = '$device_type',operating_system = '".mysql_real_escape_string($browser_arr['platform'])."' ,
                            browser_type = '".mysql_real_escape_string($browser_arr['name'])."',browser_version = '".mysql_real_escape_string($browser_arr['version'])."',user_agent='".mysql_real_escape_string($_SERVER['HTTP_USER_AGENT'])."' 
                         WHERE session_id = '$sessID'";
            if($this->is_debug)
            {
                $this->dataLog($query);
            }             
            mysql_query($query, $this->dbHandle);



            // if something happened, return true
            if(mysql_affected_rows($this->dbHandle))
                return true;
        }else {
            $query =  "INSERT INTO `conf_event_data_log_session` (session_id, session_expires, session_data, event_id, attendee_id, delete_test, login_time,ip_address,device_type,operating_system,
                        browser_type,browser_version,user_agent )
                         VALUES('".mysql_real_escape_string($sessID)."','$newExp','".$sessData."','$event_id','$attendee_id','".mysql_real_escape_string($current_event)."','".date('Y-m-d H:i:s')."','".mysql_real_escape_string($this->getClientIP())."','".mysql_real_escape_string($device_type)."','".mysql_real_escape_string($browser_arr['platform'])."',
                         '".mysql_real_escape_string($browser_arr['name'])."','".mysql_real_escape_string($browser_arr['version'])."','".mysql_real_escape_string($_SERVER['HTTP_USER_AGENT'])."')";
            if($this->is_debug)
            {
                $this->dataLog($query);
            }   

            mysql_query($query, $this->dbHandle);


            // if row was created, return true
            if(mysql_affected_rows($this->dbHandle))
                return true;
        }
        // an unknown error occured
        return false;
    }
    function destroy($sessID) {
        $query = "DELETE FROM `conf_event_data_log_session` WHERE session_id = '$sessID'";
        if($this->is_debug)
        {
            $this->dataLog($query);
        }
        mysql_query($query, $this->dbHandle);

       if(mysql_affected_rows($this->dbHandle))
            return true;

        // ...else return false
        return false;
    }
    function gc($sessMaxLifeTime) {
        // delete old sessions
        $query = "DELETE FROM `conf_event_data_log_session` WHERE session_expires < ".time();
       if($this->is_debug)
        {
            $this->dataLog($query);
        }
        mysql_query($query, $this->dbHandle);

        // return affected rows
        return mysql_affected_rows($this->dbHandle);
    }

    function unserialize_php($session_data) {
        $return_data = array();
        $offset = 0;
        while ($offset < strlen($session_data)) {
            if (!strstr(substr($session_data, $offset), "|")) {
                throw new Exception("invalid data, remaining: " . substr($session_data, $offset));
            }
            $pos = strpos($session_data, "|", $offset);
            $num = $pos - $offset;
            $varname = substr($session_data, $offset, $num);
            $offset += $num + 1;
            $data = unserialize(substr($session_data, $offset));
            $return_data[$varname] = $data;
            $offset += strlen(serialize($data));
        }
        return $return_data;
    }
    function getClientIP() {
        if (isset($_SERVER)) {

           if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
               return $_SERVER["HTTP_X_FORWARDED_FOR"];

           if (isset($_SERVER["HTTP_CLIENT_IP"]))
               return $_SERVER["HTTP_CLIENT_IP"];

           return $_SERVER["REMOTE_ADDR"];
        }

        if (getenv('HTTP_X_FORWARDED_FOR'))
           return getenv('HTTP_X_FORWARDED_FOR');

        if (getenv('HTTP_CLIENT_IP'))
           return getenv('HTTP_CLIENT_IP');

        return getenv('REMOTE_ADDR');
    }
    function getBrowser()
    {
        $u_agent = $_SERVER['HTTP_USER_AGENT'];
        //$u_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:10.0) like Gecko';//$_SERVER['HTTP_USER_AGENT'];
        $bname = 'Unknown';
        $platform = 'Unknown';
        $version= "";

        //First get the platform?
        if (preg_match('/linux/i', $u_agent)) {
            $platform = 'linux';
        }
        elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
            $platform = 'mac';
        }
        elseif (preg_match('/windows|win32/i', $u_agent)) {
            $platform = 'windows';
        }

        // Next get the name of the useragent yes seperately and for good reason
        if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
        {
            $bname = 'Internet Explorer';
            $ub = "MSIE";
        }

        elseif(preg_match('/Trident\/\d{1,2}.\d{1,2}; rv:([0-9]*)/',$u_agent,$matches_naeem))
        {
            $bname = 'Internet Explorer';
            $ub = "MSIE";
        }

        elseif(preg_match('/Firefox/i',$u_agent))
        {
            $bname = 'Mozilla Firefox';
            $ub = "Firefox";
        }
        elseif(preg_match('/Chrome/i',$u_agent))
        {
            $bname = 'Google Chrome';
            $ub = "Chrome";
        }
        elseif(preg_match('/Safari/i',$u_agent))
        {
            $bname = 'Safari';
            $ub = "Safari";
        }
        elseif(preg_match('/Opera/i',$u_agent))
        {
            $bname = 'Opera';
            $ub = "Opera";
        }
        elseif(preg_match('/Netscape/i',$u_agent))
        {
            $bname = 'Netscape';
            $ub = "Netscape";
        }

        // finally get the correct version number
        $known = array('Version', $ub, 'other');
        $pattern = '#(?<browser>' . join('|', $known) .')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';

        if (!preg_match_all($pattern, $u_agent, $matches)) {
            // we have no matching number just continue
        }

        // see how many we have
        $i = count($matches['browser']);
        if ($i != 1) {
            //we will have two since we are not using 'other' argument yet
            //see if version is before or after the name
            if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
                $version= $matches['version'][0];
            }
            else {
                $version= $matches['version'][1];
            }
        }
        else {
            $version= $matches['version'][0];
        }

        // check if we have a number
        if ($version==null || $version=="") {

            if($matches_naeem[1])
                $version = $matches_naeem[1];
            else
                $version = "?";

        }

        return array(
            'userAgent' => $u_agent,
            'name'      => $bname,
            'version'   => $version,
            'platform'  => $platform,
            'pattern'    => $pattern
        );
    }

    function dataLog($data)
    {
        $fhandle = fopen('session_log.txt','a+');
        fwrite($fhandle, $data."\n");
        fclose($fhandle);
    }
}

$session = new sessionHandle();
$session_status = session_set_save_handler(
                         array($session,"open"),
                         array($session,"close"),
                         array($session,"read"),
                         array($session,"write"),
                         array($session,"destroy"),
                         array($session,"gc"));
//session_write_close();
register_shutdown_function('session_write_close');
//session_start();
// etc...
?>

现在它在几天或几个月后显示错误消息“页面未正确重定向”。

index.php文件

require_once('WABRegistry/helpers/Session_Handler.php');
session_start();

它运行正常但是几天或几个月之后我们不得不要求支持人员重启apache并解决了我们的问题但是这次它没有用同样的错误可以请你帮忙

编辑: 它没有进入function write($sessID,$sessData) {

1 个答案:

答案 0 :(得分:0)

当我将$sessData_new['z93my8/SztfFxc/d0w=='];更改为$_SESSION['z93my8/SztfFxc/d0w=='];它开始工作时,我使用{{1}}是我的错。

我也知道会话处理程序的写入功能没有写入可以在数据库中存储值的文件