sessionドメイン間のいくつかのスキームを処理する

11769 ワード

      session      ,               ,         ,              。    :  9streets.cn a.9streets.cn  ,    a.com b.com  ,            。
   :
      ,           :    
<?php    
    include("DBsession.php");    
    $_SESSION['usssser_oke'] = 'xxssssss';    
    $_SESSION['user_oke'] = 'xxsss';      
?>  


DBsession.php


    <?php    
    /**  
      
    /**      api QQ   session  ,         
     *   session     
     */    
        
    /**  
     * CREATE TABLE `tbl_session` (  
     *     `session_id` varchar(255) binary NOT NULL default '',  
     *     `session_expires` int(10) unsigned NOT NULL default '0',  
     *     `session_data` text,  
     *     PRIMARY KEY  (`session_id`)  
     *    ) ENGINE=MyISAM;  
     */    
        
    class Session     
    {    
        //mysql         
        const db_host = "localhost"; //       ip       
        
        //          
        const db_user = "root";   //                 
        
        //         
        const db_pwd = ""; //                   
        
        //       
        const db_name = "thinkphp";      //              
        
        //        
        const db_table = "tbl_session"; //              
        
        //mysql-handle    
        private $db_handle;    
        
        //session-lifetime    
        private $lifeTime;    
        
        function open($savePath, $sessName)     
        {    
            // get session-lifetime    
            $this->lifeTime = get_cfg_var("session.gc_maxlifetime");    
        
            // open database-connection    
            $db_handle = @mysql_connect(self::db_host, self::db_user, self::db_pwd);    
        
            $dbSel = @mysql_select_db(self::db_name, $db_handle);    
        
            // return success    
            if(!$db_handle || !$dbSel)    
                return false;    
        
            $this->db_handle = $db_handle;    
            return true;    
        }    
        
        function close()     
        {    
            $this->gc(ini_get('session.gc_maxlifetime'));    
            // close database-connection    
            return @mysql_close($this->db_handle);    
        }    
        
        function read($sessID)     
        {    
            // fetch session-data    
            $res = @mysql_query("SELECT session_data AS d FROM ".self::db_table."   
                WHERE session_id = '$sessID'  
                AND session_expires > ".time(), $this->db_handle);    
        
            // return data or an empty string at failure    
            if($row = @mysql_fetch_assoc($res))    
                return $row['d'];    
        
            return "";    
        }    
        
        function write($sessID, $sessData)     
        {    
            // new session-expire-time    
            $newExp = time() + $this->lifeTime;    
        
            // is a session with this id in the database?    
            $res = @mysql_query("SELECT * FROM ".self::db_table."   
                WHERE session_id = '$sessID'", $this->db_handle);    
        
            // if yes,    
            if(@mysql_num_rows($res))     
            {    
                // ...update session-data    
                @mysql_query("UPDATE ".self::db_table."   
                    SET session_expires = '$newExp',  
                    session_data = '$sessData'  
                    WHERE session_id = '$sessID'", $this->db_handle);    
        
                // if something happened, return true    
                if(@mysql_affected_rows($this->db_handle))    
                    return true;    
            }    
            else // if no session-data was found,    
            {    
                // create a new row    
                @mysql_query("INSERT INTO ".self::db_table." (  
                    session_id,  
                    session_expires,  
                    session_data)  
                    VALUES(  
                        '$sessID',  
                        '$newExp',  
                        '$sessData')", $this->db_handle);    
                // if row was created, return true    
                if(@mysql_affected_rows($this->db_handle))    
                    return true;    
            }    
        
            // an unknown error occured    
            return false;    
        }    
        
        function destroy($sessID)     
        {    
            // delete session-data    
            @mysql_query("DELETE FROM ".self::db_table." WHERE session_id = '$sessID'", $this->db_handle);    
        
            // if session was deleted, return true,    
            if(@mysql_affected_rows($this->db_handle))    
                return true;    
        
            // ...else return false    
            return false;    
        }    
        
        function gc($sessMaxLifeTime)     
        {    
            // delete old sessions    
            @mysql_query("DELETE FROM ".self::db_table." WHERE session_expires < ".time(), $this->db_handle);    
        
            // return affected rows    
            return @mysql_affected_rows($this->db_handle);    
        }    
    }    
        
    /**  
     *   session       
     * ini_set("session.cookie_domain", ".domain.com");  
     * .domain.com       ,         '.'  
     */    
    define("MAIN_DOMAIN", ".ichezone.com");   //         
        
    /**  
     *         session    
     * COOKIE_DOMAIN = false        
     * COOKIE_DOMAIN = true         
     *       
     *         MAIN_DOMAIN    
     */    
    define("COOKIE_DOMAIN", true);     
    if (defined("COOKIE_DOMAIN") && COOKIE_DOMAIN)    
    {    
        if (defined("MAIN_DOMAIN"))    
            @ini_set("session.cookie_domain", MAIN_DOMAIN);    
    }    
        
    /**  
     *       ,         session    
     * USER_SESSION = false        
     * USER_SESSION = true         
     *       
     *         mysql     
     */    
     ini_set('session.name', 'phpsessid');//   session id       
    define("USER_SESSION", true);    
    if (defined("USER_SESSION") && USER_SESSION)    
    {    
        @ini_set("session.save_handler", "user");    
        $session = new Session;    
        @session_module_name("user");    
        @session_set_save_handler(    
            array(&$session, "open"),    
            array(&$session, "close"),    
            array(&$session, "read"),    
            array(&$session, "write"),    
            array(&$session, "destroy"),    
            array(&$session, "gc"));    
    }    
    //@ini_set("session.save_path", dirname(__file__)."/tmp");    
    //@session_id("demo");    
    //ini_set("session.save_path",dirname(__file__)."/tmp");    
    session_start();
    include("DBsession.php");    
    $_SESSION['usssser_oke'] = 'xxssssss';    
    $_SESSION['user_oke'] = 'xxsss';     
    ?>   


   :
PHP Code        

    <?php  
    ini_set('session.name', 'sid');//   session id     
    ini_set('session.use_trans_sid', 0);//    GET/POST       
    ini_set('session.use_cookies', 1);//   COOKIE    SESSION ID      
    ini_set('session.cookie_path', '/');  
    ini_set('session.cookie_domain', '.ichezone.com');//        SESSION ID   COOKIE,             
    session_start();  
    $_SESSION['usssser_oke'] = 'xxssssss';  
    $_SESSION['user_oke'] = 'xxsss';    
    ?>  

     :
PHP Code        

        :  
    session_start();  
    setcookie("session_id",session_id(),time()+3600*24*365*10,"/",".ichezone.com");  
     $_SESSION['user_name'] = '    ';  
        :  
    session_id($_COOKIE['session_id']);  
    session_start();  
    $_SESSION['user_sssme'] = 222222;  

     :
PHP Code        

        :  
    ini_set("session.cookie_domain",'.ichezone.com');// :      sesson_start()  ,         
    session_start();  
    $_SESSION['user_name'] = '  www  ';  
        :  
    ini_set("session.cookie_domain",'.ichezone.com');// :      sesson_start()  ,         
    session_start();  
    $_SESSION['user_nassse'] = '  www  ';   

     :

   p3p  ,    ,     x.com   ,y.com    y.com     sessionid ,sessionid      ,   seesion      ,    sessionid   。    y.com              ,     ,          cookie ,  p3p    。   php    :
 
PHP Code        

    header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');   


 

              :

    127.0.0.1        www.a.com  
    127.0.0.1        www.b.com  
      
      :   a_setcookie.php   ,    :  
      
    <?php  
    header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');  
    setcookie("test", $_GET['id'], time()+3600, "/", ".a.com");  
    ?>  
      
      :   a_getcookie.php   ,    :  
      
    <?php  
    var_dump($_COOKIE);  
    ?>  
      
      :   b_setcookie.php   ,    :  
      
    <script src="http://www.a.com/a_setcookie.php?id=www.b.com"></script>  
      
    ----------------------------  
      
             ,           :  
      
    http://www.b.com/b_setcookie.php  
    http://www.a.com/a_getcookie.php  
      
         ,   b.com    ,      a.com    cookie 。              

 

参照:http://www.9streets.cn/art-php-578.html