开发者

Cookies are not working on a php session file:

I tried the following:

Basically, it should say: If there's no cookie, get lang from web browser (this part works and that's the job of lang.php). If there is cookie, the session will take its value from the cookie. If there's nothing,开发者_Go百科 leave it as English.

session.php:

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

};


The args to setcookie are as follows:

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

As such, the way you're using it seems a little odd. (It's not incorrect that said.) I'd have thought that something like...

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

...would be a bit more obvious, and is perhaps what you're after. (This is what $_COOKIE['lang'] will require.)


The first parameter of setcookie is the cookie name and the second the cookie value. So in this case $_SESSION['lang'] is the cookie name and time() + (3600 * 24 * 30) the value:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜