PHP:将$_SESSION[';SESSION_NAME';]与数组ex.$_SESSION[';r


PHP: Using $_SESSION['SESSION_NAME'] with array ex. $_SESSION['rank']['power']

Using $_SESSION['Session_Name']['Session_Array']

$_SESSION基本上是一个数组,它将stored作为global数组,那么有可能将数组分配到会话中吗?

我正在使用一个非常需要它的排名系统。以下是我用来测试的文件结构:(包含PHP标记,但未在脚本中显示)

index.php


session_start();
include 'testsessions.php';
include 'ranks.php';
include 'errorchecking.php';

ranks.php


$ranks = [
    /* Global rankname => Rank prefix */
    'Owner'         => '[Owner]', // + Developers
    'Co-Owner'      => '[Co-Owner]',
    'Administrator' => '[Admin]',
    'Moderator'     => '[Mod]',
    'Spy'           => '[Default]',
    'Default'       => '[Default]',
    'Not_logged_in' => '(Guest)'
];

if($_SESSION['rank'] === $ranks['Owner']){
    $_SESSION['rank'] = [
        /* Permission node => setting */
        'power' => 9999, // This is the first thing the error system will check, and also the "strenght" of the group, like how much the group have permission to.
        'has_click' => array($buttons['all'], $links['public'], $links['chat'], $links['forum'], $navigation['all']),
        'has_kick_power' => 'max'
        //etc
    ];
}
echo $_SESSION['rank']['has_kick_power']; // This will work normal, keep reading and you will see the error.

现在我不关心SQL注入,我的文件中已经包含了它。

testsessions.php


$_SESSION['user_id'] = '0'; // Unique_ID
$_SESSION['First_Name'] = 'Fname';
$_SESSION['Last_Name'] = 'Lname';
$_SESSION['username'] = 'Admin';     $username = $_SESSION['username'];
$_SESSION['password'] = 'changeme';  $password = $_SESSION['password'];  encrypt($password);   // encrypt() is a function I've made for hashing..
$_SESSION['loginkey'] = "$username$password";
$_SESSION['rank'] = 'Owner';
$_SESSION['active'] = 'Always';

errorchecking.php|此文件中存在错误:

"警告:在行(line_number)上的/Applications/XAMPP/examplepfiles/htdocs/php/(file_name).php中的非法字符串偏移量'power''

if($_SESSION['rank']['power'] < 30){
    echo 'Sorry, your rank power is too low to view this page!';
    // And also now I am only using ECHO for testing, my system is better then that :)
// Including that it still says 'Sorry, your rank power is too low to view this page!' even though the Owner's rank power is 9999.. I think that is because of the error?
}

当然,您可以在会话中添加任何内容。

至于你的警告,那是因为关键的"力量"不存在。

在使用"isset"函数访问密钥之前,最好始终检查密钥是否存在。

例如:

if( isset($_SESSION['rank']['power']) && $_SESSION['rank']['power'] < 30){
etc..

更新

至于具体的警告,由于$_SESSION['rank']是一个字符串,因此不能将其视为数组。