Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access the session array with php

Tags:

php

session

I am working in a subfolder on my website: www.example.com/subfolder

Now, i want to set a session that is only accessible within www.example.com/subfolder

To achieve this, i did the following:

private $sessiontimeout= 10800;
        private $subdomain = '/subfolder/';
        private $website = 'example.com';    

function __construct ($table)
            {print_r( $_SESSION );
                $this->table=$table;
                $this->savedusername= $this->getsession('logbook');
                session_set_cookie_params ( $this->sessiontimeout, $this->subdomain, $this->website, 0, 1 );
                ini_set('session.use_only_cookies', 1);
                if (!is_null ($this->savedusername))
                {
                    $resultobj=selectquery ("select last_login_one from $this->table where username=?", "s", (array) $this->savedusername);
                    if ($resultobj['obj']->num_rows() > 0)
                    {
                        $this->last_login=$resultobj['data'][0]['last_login_one'];
                    }
                }
            }

Now when i print the $_SESSION array, it does not display anything, Not even 'Array()'.

Please what am i not doing right?

Thanks

like image 276
Ogugua Belonwu Avatar asked Aug 27 '26 18:08

Ogugua Belonwu


2 Answers

Have you used session_start() anywhere before that code ?

I also suggest using var_dump() instead of print_r() to debug values as print_r doesn't output null values, hence creating some confusion sometimes.

like image 150
Tom Avatar answered Aug 29 '26 09:08

Tom


You need to call session_start first (unless you have session.auto_start set to 1).

like image 21
Michał Wojciechowski Avatar answered Aug 29 '26 08:08

Michał Wojciechowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!