3

我在设置会话时遇到问题,当我设置: $_SESSION["predefinedvar"] = "SOMETHING" 它会记住会话变量,但是,当我执行以下操作时: $_SESSION = $class->somearray; 它不会保存会话数组。

我在网上搜索过,并从这个搜索中尝试了谷歌的前几页: php session assign array

当我执行 print_r($_SESSION) 或 print_r($l->details); 时,输出与此类似。这是脚本给我的输出(来自 $_SESSION 和 $l->details)

Array
(
    [1] => Array
        (
            [id] => 1
            [username] => Usernamehere
            [password] => EnCryptedPasswordHere
            [mail] => em@il.com
            [ip] => ::1
            [registerred] => 1349111531
            [loggedin] => 0
            [vip] => 0
        )

    [2] => Array
        (
            [id] => 1
            [city] => 1
            [family] => 0
            [pm] => 0
            [mod] => 0
        )

    [3] => Array
        (
            [id] => 1
            [url] => images/pb/1.png
        )

    [4] => Array
        (
            [id] => 1
            [rank] => 1
            [subrank] => 5000
        )

    [5] => Array
        (
            [id] => 1
            [hp] => 100
            [str] => 0
            [def] => 0
        )

    [6] => Array
        (
            [id] => 1
            [money] => 0
            [bank] => 0
            [bullets] => 0
            [points] => 0
        )

)

目前我有以下代码

登录.c.php

<?php
if(!$required) die("Du har ikke adgang til denne filen.");

class login {

    var $username, $password;
    var $details;
    var $salt = "SALTHIDDEN";
    var $error;
    var $justcheck;

    function onPost($username, $password, $justcheck = FALSE) {
        if($justcheck) $this->justcheck = $justcheck;
        if(!$username || !$password) die("Du må skrive inn både brukernavn og passord.");
        $this->username = htmlspecialchars(addslashes($username));
        $this->password = sha1(md5($this->salt . $password));
    }

    function checkDetails() {
        $query = mysql_query("SELECT * FROM users WHERE username = '".$this->username."' AND password = '".$this->password."' LIMIT 1");
        $num = mysql_num_rows($query);
        if($num > 0) {
            $this->details[1] = mysql_fetch_assoc($query);
            return TRUE;
        }
        else {
            $this->error[] = "Feil brukernavn eller passord.";
            return FALSE;
        }
    }

    function getEverything() {
        $this->details[2] = mysql_fetch_assoc(mysql_query("SELECT * FROM user_c WHERE id='".$this->details[1][id]."' LIMIT 1"));
        $this->details[3] = mysql_fetch_assoc(mysql_query("SELECT * FROM user_p WHERE id='".$this->details[1][id]."' LIMIT 1"));
        $this->details[4] = mysql_fetch_assoc(mysql_query("SELECT * FROM user_r WHERE id='".$this->details[1][id]."' LIMIT 1"));
        $this->details[5] = mysql_fetch_assoc(mysql_query("SELECT * FROM user_s WHERE id='".$this->details[1][id]."' LIMIT 1"));
        $this->details[6] = mysql_fetch_assoc(mysql_query("SELECT * FROM user_i WHERE id='".$this->details[1][id]."' LIMIT 1"));
    }
    function returnValue() {
        if($this->error) foreach($this->error as $e) echo "$e<br />";
        if(!$this->justcheck && !$this->error) die("<a href=\"#main\">Du er nå innlogget som $username<br />Klikk her hvis du ikke viderekobles til hovedkvarteret.</a><meta http-equiv=\"Refresh\" content=\"0; url=#main\">");
    }

}

这个文件是 POST.php 文件:

<?php

require_once("path/hidden/db.php");

switch(key($_GET)) {
    default:
        echo "Nothing to do here";
    break;

    case "login":
        $required = TRUE;
        require_once("path/hidden/login.c.php");
        session_start();
        $l = new login();
        $l->onPost($_POST["username"], $_POST["password"]);
        if($l->checkDetails()) {
            $l->getEverything();
            session_start();
            $_SESSION = $l->details;
        }
        $_SESSION[RandomVar] = "RandomVal";
        //$l->returnValue();
        print_r($_SESSION);
        print_r($l->details);
        //echo session_id();
    break;
}
4

3 回答 3

2

您需要为 Session 变量指定一个名称。您尝试了以下操作:

 $_SESSION = $class->somearray

但是你需要为你的数组分配一个键:

$_SESSION['my_array'] = $class->somearray
于 2012-11-27T18:01:36.333 回答
1

要将数组存储到会话中,您需要将该数组与键绑定。

例如 $_SESSION['key']=$array_variable;

于 2012-11-27T18:28:54.863 回答
0

代替:

$_SESSION = $l->details;

尝试这个:

foreach($l->details as $strKey => $strValue) {
    $_SESSION[ $strKey ] = $strValue;
}
于 2012-11-27T18:04:29.137 回答