0

Currently I am working in One PHP Symfony Project Regarding the Performance Evaluation of an Employee. So I need to know how we can get PHP session variable of an employee (Employee Role) Please see the php file ( $role = $_SESSION['empRole']; ) to an external js file.(js file also attached). I am using PHP Symfony framework.

Actually I need to check who is logged in to the website.if it is admin/core members / Hr. I need to apply validation to some fileds only when the Coremembers login the website.

Here is the code section that I need to pass $role to External JS file(in pastebin)

    public function executeIndex(sfWebRequest $request) {
        if (isset($_SESSION['empId'])) {
            $role = $_SESSION['empRole'];
            if ($role == "admin") {
                $empId = $this->getUser()->getAttribute('empId');
                $team_members = GroupwareEmployeesPeer::getAllSubordinatesId($empId`enter code here`);
                $nc = new Criteria();
                $nc->add(PeD`enter code here`ates`enter code here`Peer::NAME, 'Dashboard');
                $resultset = PeDatesPeer::doSelect($nc);

So Can you guys give me a solution regarding this, since I am stuck with this for a long time.

Please see thepastebin code for php and external js file.

https://pastebin.com/kbkLjSFa - PHP File.

https://pastebin.com/M60Rg64U - JS file

12
  • 1
    Since you are using Symfony, you might want to look into the build-in security/firewall/user-roles features. Commented Jul 8, 2017 at 6:39
  • Sorry I didn't get you. Can you please explain in brief? Since I am new to this PHP Symphony. Commented Jul 8, 2017 at 6:41
  • 1
    Have look here: symfony.com/doc/current/security.html Symfony does the whole session management including user roles for you, if you like. After that it is just a simple is_granted('SOME_ROLE') in your template files, to transfer permissions to JavaScript. Commented Jul 8, 2017 at 6:43
  • Sorry, Is there any other way i can get the php variable to an external file? I am not getting about security.xml. Can you please refer the code i have attached and please guide me to fix the issue? Commented Jul 8, 2017 at 6:54
  • My Actual problem is I need to get PHP variable from Php file and I need to use it in another external js variable. Commented Jul 8, 2017 at 7:00

2 Answers 2

1

Yes you can get the php variable into the js file.

Method I:

Rename your .js file to .js.php and pass the php variable as a query string to this file where you link this file in your page.

So, if you have some test.js file, rename it to test.js.php and use it in your page like this

<script src="test.js.php?session_var=<?= $your_var_here;?>&any_var=<?= $any_php_var;?>"></script>

And in your js file, retrieve the values from query string parameters So inside test.js.php, you can simply

var some_var = "<?= $_GET['session_var']";
var any_var = "<?= $_GET['any_var'];?>";
//rest of your logic goes here

Method II:

Use an AJAX request.

var some_var, another_var; 
$.ajax({
  url : '/url-which-returns-session-vars', //return the session data in json from this url
  asnyc : false,
  dataType : 'json', 
  success : function(res){
     some_var = res.some_var;
     another_var = res.another_var;
  }
});
//rest of the logic goes here.

Both of these methods work. But I prefer the first method, since I don't have to write extra code.

Edit: Suppose the response of the ajax call looks like this

{
   some_var : 'value here',
   another_var : 'another value here'
}

So res in the success function argument contains this response and since the response is a JSON you can access these values using the . notation. Thus,

some_var = res.some_var;
another_var = res.another_var;

and since these variables are global variables, you can use them anywhere in your code in this file.

Sign up to request clarification or add additional context in comments.

3 Comments

Hello, I can't rename it to test.js.php. What should I do?
Can you please describe the logic behind some_var = res.some_var; another_var = res.another_var; these variables, since i am new to this?
I need to use only this $role = $_SESSION['empRole']; So how can I write the code, so that I can access the same in External js file. Thanks if you are giving the code.
0
 ?> <script> var php_variable = '<?php $variable ?>'; </script> <?php

Global variable should be declared before external js file include . in js file access it using this variable name php_variable

After the above code . jquery external file should be included here

<script src="external.js" ></script>

external.js

consol.log(php_variable);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.