Drupal 8: Get current user session

The PHP provide us to save values in sessions. For each user, there will be session maintain on the server for holding their values. In Drupal 8 we can able to access session values of a particular user with help of service() function in core.

The below is the syntax and example for accessing session of the particular user.




$session = \Drupal::service('session');
if ($session->has('uc_cart_id')) {
 print "User has session";