3

I have a problem when setting sessions, when I set a: $_SESSION["predefinedvar"] = "SOMETHING" It will remember the session variable, but however, when I do a: $_SESSION = $class->somearray; It won't save the session array.

I have searched the web, and tried the first few pages from Google from this search: php session assign array

The output is similar to this when I do an print_r($_SESSION) or print_r($l->details); This is the output the script gives me (both from $_SESSION and $l->details)

Array
(
    [1] => Array
        (
            [id] => 1
            [username] => Usernamehere
            [password] => EnCryptedPasswordHere
            [mail] => [email protected]
            [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
        )

)

Currently I have the following codes

login.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\">");
    }

}

and this file is the POST.php file:

<?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;
}
2
  • test to name the session. $_SESSION['user_details'] = $l->details Commented Nov 27, 2012 at 18:01
  • Thanks, just did it when I saw the answer from Pé de Leão and it worked :) Commented Nov 27, 2012 at 18:06

3 Answers 3

2

You need to assign a name for the Session variable. You tried the following:

 $_SESSION = $class->somearray

But you need to assign a key for your array:

$_SESSION['my_array'] = $class->somearray
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, so it isn't enough for it to get the ID from an array? Anyway, thank you, it worked :D
1

for storing an array into session you need to bind that array with key.

e.g $_SESSION['key']=$array_variable;

Comments

0

Instead of:

$_SESSION = $l->details;

Try this:

foreach($l->details as $strKey => $strValue) {
    $_SESSION[ $strKey ] = $strValue;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.