PHP:使用带有数组ex的$ _SESSION ['SESSION_NAME']。 $ _SESSION [ '秩'] [ '功率']

时间:2016-05-12 15:17:05

标签: php arrays session

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

$_SESSION基本上是一个数组,它将stored作为global数组,所以是否可以将数组分配到会话中?

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

----------------------------------------------- -----------------------------------------------

的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 |此文件中存在错误:'警告: / Applications / XAMPP / xamppfiles / htdocs / php /(file_name).php中的非法字符串偏移'power'(line_number)

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?
}

1 个答案:

答案 0 :(得分:0)

当然,你可以肯定会在会话中填写任何内容。

至于你的警告,因为关键的电源&#39;不存在。

在使用&#39; isset&#39;访问密钥之前,始终检查密钥是否存在是一种很好的做法。功能

例如:

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

<强>更新

至于具体的警告,因为$ _SESSION [&#39; rank&#39;]是一个字符串,你不能把它当成一个数组。