Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP SESSION is available only in subfolders

Tags:

php

session

I can access my SESSIONS from subfolders eg. /teachers, /students on my website but not from files directly under /.

My setup:

Above my pages:

session_set_cookie_params(time()+86400000, '/', '.mysite.com', 0, 1);
    ini_set('session.use_only_cookies', 1);
    if(!isset($_SESSION))
    {
        session_start();
    }

How I set my SESSIONS

session_regenerate_id(true);
$_SESSION['signature'] = $signature;

My PHP.ini

session.save_path = '/home/mysite/cvsessions'

What am I getting wrong?

like image 611
Ogugua Belonwu Avatar asked Aug 04 '16 08:08

Ogugua Belonwu


1 Answers

Clarification: This answer is about using php.ini files in the same folders that contain the PHP files in order to overwrite the system-wide default settings, this answer assumes that you already have a standard default php.ini file whose settings you either can not or should not edit.

I have had similar issues to what you're describing and would hazard a guess that you do not have php.ini files in each active folder, whereas you may only actually be having ONE php.ini file in your root folder and so any of the subfolders (/students, etc.) are not using the root php.ini and simply using the account- or system- default.

Depending on what server system you have set up (for example CPanel install?) then in order to change settings from the default server-wide php.ini, a new php.ini file containing just the custom settings (such as in your case account specific session storage location) needs to be installed into every directory needing to use non-default php.ini settings.

So, step by step:

  • Do you have a php.ini in every folder, such as /,/teachers,/students etc.?

  • Are all these php.ini files the same?

  • All folders should be the same, so all should have their own php.ini copy or none of them should. Otherwise this sort of change in behaviour which is causing your inconsistency issues will occur when changing between one (custom) php.ini setting and another (system default) php.ini setting.

If any folder is missing them or prehaps only your public html root folder has the php.ini file then that means all the other folders are using the default, and so the public html root is looking for sessions in the wrong place -- It's looking in the '/home/mysite/cvsessions' address whereas the default address for PHP sessions is something like /home/tmp.

Does this help or is this well off the mark ?


Better way of checking if session is started:

PHP >= 5.4

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

For versions of PHP < 5.4.0

if(session_id() == '') {
    session_start();
}

Source: https://stackoverflow.com/a/18542272/3536236

like image 139
Martin Avatar answered Oct 18 '22 17:10

Martin