Skip to main content
deleted 6 characters in body
Source Link
Koala Yeung
  • 209
  • 2
  • 16

I'm writing a custom script that requires a proper Drupal 8 user session to work. This is the current version:

<?php

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

$drupal_root = __DIR__;
define('DRUPAL_DIR', $drupal_root);

// Specify relative path to the drupal root.
$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$request = Request::createFromGlobals();
var_dump($request->getSession()); exit;

// Retrieve the site path
// Bootstrap drupal to different levels
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();
$kernel->handlePageCache($request);
$kernel->prepareLegacyRequest($request);
 
var_dump($request->getSession());
// get session handler and retrieve session details
$session_handler = \Drupal::getContainer()->get('session_handler.storage');
$session_id = $session_handler->getId();
var_dump($session_id);
$logged_in = (bool) $session_handler->read($session_id);

// inspect values
var_dump($request->getSession());
var_dump($session_id);
var_dump($logged_in); exit;

I'd expect $request->getSession() to return something, $session_id to be the Drupal's session id, and the $logged_in variable to be truthy. Instead I keep getting this output:

NULL string(0) "" bool(false)

Update 1: I executed this on a browser, which I have an active session with my Drupal. This script is placed in a subfolder of the Drupal installation, so it should not be a cookie path issue.

Update 2: Added var_dump($request->getSession()) to the script.

I'm writing a custom script that requires a proper Drupal 8 user session to work. This is the current version:

<?php

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

$drupal_root = __DIR__;
define('DRUPAL_DIR', $drupal_root);

// Specify relative path to the drupal root.
$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$request = Request::createFromGlobals();
var_dump($request->getSession()); exit;

// Retrieve the site path
// Bootstrap drupal to different levels
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();
$kernel->handlePageCache($request);
$kernel->prepareLegacyRequest($request);
 
var_dump($request->getSession());

$session_handler = \Drupal::getContainer()->get('session_handler.storage');
$session_id = $session_handler->getId();
var_dump($session_id);
$logged_in = (bool) $session_handler->read($session_id);
var_dump($logged_in); exit;

I'd expect $request->getSession() to return something, $session_id to be the Drupal's session id, and the $logged_in variable to be truthy. Instead I keep getting this output:

NULL string(0) "" bool(false)

Update 1: I executed this on a browser, which I have an active session with my Drupal. This script is placed in a subfolder of the Drupal installation, so it should not be a cookie path issue.

Update 2: Added var_dump($request->getSession()) to the script.

I'm writing a custom script that requires a proper Drupal 8 user session to work. This is the current version:

<?php

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

$drupal_root = __DIR__;
define('DRUPAL_DIR', $drupal_root);

// Specify relative path to the drupal root.
$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$request = Request::createFromGlobals();

// Bootstrap drupal to different levels
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();
$kernel->handlePageCache($request);
$kernel->prepareLegacyRequest($request);

// get session handler and retrieve session details
$session_handler = \Drupal::getContainer()->get('session_handler.storage');
$session_id = $session_handler->getId();
$logged_in = (bool) $session_handler->read($session_id);

// inspect values
var_dump($request->getSession());
var_dump($session_id);
var_dump($logged_in); exit;

I'd expect $request->getSession() to return something, $session_id to be the Drupal's session id, and the $logged_in variable to be truthy. Instead I keep getting this output:

NULL string(0) "" bool(false)

Update 1: I executed this on a browser, which I have an active session with my Drupal. This script is placed in a subfolder of the Drupal installation, so it should not be a cookie path issue.

Update 2: Added var_dump($request->getSession()) to the script.

added 266 characters in body
Source Link
Koala Yeung
  • 209
  • 2
  • 16

I'm writing a custom script that requires a proper Drupal 8 user session to work. This is the current version:

<?php

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

$drupal_root = __DIR__;
define('DRUPAL_DIR', '/path/to/my/drupal8'$drupal_root);

// Specify relative path to the drupal root.
$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$request = Request::createFromGlobals();
var_dump($request->getSession()); exit;

// Retrieve the site path
// Bootstrap drupal to different levels
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->prepareLegacyRequest>boot();
$kernel->handlePageCache($request);
$kernel->boot>prepareLegacyRequest($request);

var_dump($request->getSession());

$session_handler = \Drupal::getContainer()->get('session_handler.storage');
$session_id = $session_handler->getId();
var_dump($session_id);
$logged_in = (bool) $session_handler->read($session_id);
var_dump($logged_in); exit;

I'd expect $request->getSession() to return something, $session_id to be the Drupal's session id, and the $logged_in variable to be truthy. Instead I keep getting this output:

NULL string(0) "" bool(false)
 

Update 1: I executed this on a browser, which I have an active session with my Drupal. This script is placed in a subfolder of the Drupal installation, so it should not be a cookie path issue.

Update 2: Added var_dump($request->getSession()) to the script.

I'm writing a custom script that requires a proper Drupal 8 user session to work. This is the current version:

<?php

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

define('DRUPAL_DIR', '/path/to/my/drupal8');

// Specify relative path to the drupal root.
$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$request = Request::createFromGlobals();

// Retrieve the site path
// Bootstrap drupal to different levels
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->prepareLegacyRequest($request);
$kernel->boot();

$session_handler = \Drupal::getContainer()->get('session_handler.storage');
$session_id = $session_handler->getId();
var_dump($session_id);
$logged_in = (bool) $session_handler->read($session_id);
var_dump($logged_in); exit;

I'd expect $session_id to be the Drupal's session id and the $logged_in variable to be truthy. Instead I keep getting this output:

string(0) "" bool(false)

Update: I executed this on a browser, which I have an active session with my Drupal. This script is placed in a subfolder of the Drupal installation, so it should not be a cookie path issue.

I'm writing a custom script that requires a proper Drupal 8 user session to work. This is the current version:

<?php

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

$drupal_root = __DIR__;
define('DRUPAL_DIR', $drupal_root);

// Specify relative path to the drupal root.
$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$request = Request::createFromGlobals();
var_dump($request->getSession()); exit;

// Retrieve the site path
// Bootstrap drupal to different levels
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();
$kernel->handlePageCache($request);
$kernel->prepareLegacyRequest($request);

var_dump($request->getSession());

$session_handler = \Drupal::getContainer()->get('session_handler.storage');
$session_id = $session_handler->getId();
var_dump($session_id);
$logged_in = (bool) $session_handler->read($session_id);
var_dump($logged_in); exit;

I'd expect $request->getSession() to return something, $session_id to be the Drupal's session id, and the $logged_in variable to be truthy. Instead I keep getting this output:

NULL string(0) "" bool(false)
 

Update 1: I executed this on a browser, which I have an active session with my Drupal. This script is placed in a subfolder of the Drupal installation, so it should not be a cookie path issue.

Update 2: Added var_dump($request->getSession()) to the script.

added 198 characters in body
Source Link
Koala Yeung
  • 209
  • 2
  • 16

I'm writing a custom script that requires a proper Drupal 8 user session to work. This is the current version:

<?php

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

define('DRUPAL_DIR', '/path/to/my/drupal8');

// Specify relative path to the drupal root.
$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$request = Request::createFromGlobals();

// Retrieve the site path
// Bootstrap drupal to different levels
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->prepareLegacyRequest($request);
$kernel->boot();

$session_handler = \Drupal::getContainer()->get('session_handler.storage');
$session_id = $session_handler->getId();
var_dump($session_id);
$logged_in = (bool) $session_handler->read($session_id);
var_dump($logged_in); exit;

I'd expect $session_id to be the Drupal's session id and the $logged_in variable to be truthy. Instead I keep getting this output:

string(0) "" bool(false)

Update: I executed this on a browser, which I have an active session with my Drupal. This script is placed in a subfolder of the Drupal installation, so it should not be a cookie path issue.

I'm writing a custom script that requires a proper Drupal 8 user session to work. This is the current version:

<?php

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

define('DRUPAL_DIR', '/path/to/my/drupal8');

// Specify relative path to the drupal root.
$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$request = Request::createFromGlobals();

// Retrieve the site path
// Bootstrap drupal to different levels
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->prepareLegacyRequest($request);
$kernel->boot();

$session_handler = \Drupal::getContainer()->get('session_handler.storage');
$session_id = $session_handler->getId();
var_dump($session_id);
$logged_in = (bool) $session_handler->read($session_id);
var_dump($logged_in); exit;

I'd expect $session_id to be the Drupal's session id and the $logged_in variable to be truthy. Instead I keep getting this output:

string(0) "" bool(false)

I'm writing a custom script that requires a proper Drupal 8 user session to work. This is the current version:

<?php

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

define('DRUPAL_DIR', '/path/to/my/drupal8');

// Specify relative path to the drupal root.
$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$request = Request::createFromGlobals();

// Retrieve the site path
// Bootstrap drupal to different levels
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->prepareLegacyRequest($request);
$kernel->boot();

$session_handler = \Drupal::getContainer()->get('session_handler.storage');
$session_id = $session_handler->getId();
var_dump($session_id);
$logged_in = (bool) $session_handler->read($session_id);
var_dump($logged_in); exit;

I'd expect $session_id to be the Drupal's session id and the $logged_in variable to be truthy. Instead I keep getting this output:

string(0) "" bool(false)

Update: I executed this on a browser, which I have an active session with my Drupal. This script is placed in a subfolder of the Drupal installation, so it should not be a cookie path issue.

Source Link
Koala Yeung
  • 209
  • 2
  • 16
Loading