PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to test if a session already exists?

 May 12, 2022     symfony     No comments   

Issue

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?


Solution

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...



Answered By - Jovan Perovic
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing