代码之家  ›  专栏  ›  技术社区  ›  wyc

Cookie无法处理php会话文件:

  •  0
  • wyc  · 技术社区  · 14 年前

    我尝试了以下方法:

    基本上,它应该说:如果没有cookie,就从web浏览器获取lang(这部分工作,这就是 lang.php )如果存在cookie,会话将从cookie中获取其值。如果没有,就用英语吧。

    第七讲 :

    /* Class constructor */
    function Session(){
        $this->time = time();
        $this->startSession();
    }
    
    function cf($filename){//function to clean a filename string so it is a valid filename
        $fp = explode('/',$filename);
        $num = count($fp);
        return $fp[$num-1];
    }
    
    /**
     * startSession - Performs all the actions necessary to
     * initialize this session object. Tries to determine if the
     * the user has logged in already, and sets the variables
     * accordingly. Also takes advantage of this page load to
     * update the active visitors tables.
     */
    function startSession(){
        session_start();   //Tell PHP to start the session
    
        /* Set referrer page */
        if(isset($_SESSION['url'])){
            $this->referrer = $search = $this->cf($_SESSION['url']);
        }else{
            $this->referrer = "/";
        }
    
        /* Set current url */
        $this->url = $_SESSION['url'] = $this->cf($_SERVER['PHP_SELF']);
    
        /* Set user-determined language: */
        //set up languages array:
        $langs = array('en','es','zh');
        //
        if(isset($_GET['lang'])){
            if(in_array($_GET['lang'],$langs)){
                $this->lang =  $_SESSION['lang'] = $_GET['lang'];
                setcookie($_SESSION['lang'], time() + (3600 * 24 * 30));
            }
        }
        else if(isSet($_COOKIE['lang'])) {
            $_SESSION['lang'] = $_COOKIE['lang'];
        }
        else {
            $_SESSION['lang'] = 'en';
        }
    }
    

    };

    2 回复  |  直到 14 年前
        1
  •  1
  •   John Parker    14 年前

    ARGS到 setcookie 具体如下:

    bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
    

    因此,你使用它的方式似乎有点奇怪。(说得没错)我本以为…

    setcookie('lang', $_SESSION['lang'], time() + (3600 * 24 * 30));
    

    ……会更明显一些,也许是你想要的。(这是$_cookie['lang']需要的。)

        2
  •  1
  •   Gumbo    14 年前

    的第一个参数 setcookie 是Cookie名称,第二个是Cookie值。所以在这种情况下 $_SESSION['lang'] 是cookie名和 time() + (3600 * 24 * 30) 价值:

    setcookie($_SESSION['lang'], time() + (3600 * 24 * 30));