Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if a session already exists?

Tags:

symfony

How can I test if a session exists in symfony?

Without the framework we proceed like this:

<?php

if( isset($_SESSION['login'])) {
  echo 'Bonjour '.$_SESSION['login'].' vous etes connecté.<br>';
}

?>

How to do so with the session service in the symfony controller with the Request object?

like image 747
Mouaad Ait Said Avatar asked Oct 21 '25 13:10

Mouaad Ait Said


1 Answers

In Symfony, session service is an instance of Symfony\Component\HttpFoundation\Session\Session. That said, session does provide several method to either check or manipulate session.

In your particular case, you would do something like this:

$session = $request->getSession();

if ( $session->has('login')){
    $message = 'Bonjour '.$session->get('login').' vous etes connecté.<br>';
}

But, noting the comment from @Gerry, his question is a very valid one.

Hope this helps...

like image 183
Jovan Perovic Avatar answered Oct 23 '25 08:10

Jovan Perovic



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!