-1

i am trying to change default session cookie parameters. To store the session data i am using mysql and session_set_save_handler()

this is the constructor of the class Session

    public function __construct(){
    // Instantiate new Database object
    $this->db = new Database;

    // Set handler to overide SESSION
    session_set_save_handler(
    array($this, "_open"),
    array($this, "_close"),
    array($this, "_read"),
    array($this, "_write"),
    array($this, "_destroy"),
    array($this, "_gc")
    );

    // Start the session

    session_set_cookie_params(time() + (86400 * 30),"/","",true,true);
    session_start(); 
}

If the line session_set_cookie_param() is set after session_start() i have this error

PHP Warning:  session_set_cookie_params(): Cannot change session cookie parameters when session is active

before the session_start() i have no error but no cookie is set. And when the line is removed the session cookie is set successfully with default php data "PHPESSID" and exptime = Session.

The session_status() right before the session_start() line is equal to 1 (PHP_SESSION_NONE)

If i set params before session_set_save_handler() no cookie is set.

  • I have read the post carefully and add the line regisster_shutdown_function() dont resolve the problem. And yes, i have tryed set the cookies params before but dont work either – Sebastián Mahuzier Jun 19 '20 at 19:00
  • While I do not *believe* this is a duplicate of the referenced question, have the answers to the original question linked above helped? If not, please add details to your question (**edit**) and we can get this reopened. Cheers – Martin Jun 19 '20 at 19:09
  • You also appear to only have three of the four optional values, "path", secure, httponly. You could add the domain value as well... – Martin Jun 19 '20 at 19:12
  • Yeah, i have tryed with `session_set_cookie_params(time() + (86400 * 30),"/","",true,true);` – Sebastián Mahuzier Jun 19 '20 at 19:13
  • I have solved this, adding a few lines to the constructor and params to the cookie, this is the new constructor; – Sebastián Mahuzier Jun 19 '20 at 19:20
  • Glad you found a solution. `:-)` – Martin Jun 19 '20 at 19:31

1 Answers1

0

Check if it was started before starting a session

session_status ( void ) : int
session_status() is used to return the current session status.

PHP_SESSION_DISABLED if sessions are disabled.
PHP_SESSION_NONE if sessions are enabled, but none exists.
PHP_SESSION_ACTIVE if sessions are enabled, and one exists.

<?php
/**
* @return bool
*/
function is_session_started()
{
    if ( php_sapi_name() !== 'cli' ) {
        if ( version_compare(phpversion(), '5.4.0', '>=') ) {
            return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
        } else {
            return session_id() === '' ? FALSE : TRUE;
        }
    }
    return FALSE;
}

// Example
if ( is_session_started() === FALSE ){
 session_start();
}
?>
dılo sürücü
  • 3,821
  • 1
  • 26
  • 28