通过php ECHO <script>。 我想知道正确的方法[关闭]

I have a url path that will send through this myGlobalFunction(); from a different file.

So the following script tag will catch the user on the website root and forward him to the login page. I can't seem to make this working. I know i am missing the single and double quotes. :(

if (!isset($_SESSION['role']))
{
  echo '<script>window.location.assign("'.myGlobalFunction().'/onboardingform/core/admin/login.php")</script>';
  exit();

  } else if (isset($_SESSION['role'])) {


        if($_SESSION['role'] =='CLIENT') {

              echo "<script>window.location.assign('/onboardingform/core/client/index.php')</script>";
              exit();

        } else if($_SESSION['role'] =='RESELLER') {

              echo "<script>window.location.assign('/onboardingform/core/reseller/index.php')</script>";
              exit();

        } else if($_SESSION['role'] =='CSR') {

              echo "<script>window.location.assign('/onboardingform/core/csr/index.php')</script>";
              exit();

        } else if($_SESSION['role'] == 'ADMIN') {


?>

You're not concatenating the strings properly. Use . operator to concatenate the string like this.

<?php

echo '<script>window.location.assign("'. myGlobalFunction().'/onboardingform/core/admin/login.php")</script>';

And there is no need of echo statement inside another echo.

if (!isset($_SESSION['role']))
{
  echo "<script>window.location.assign('" . myGlobalFunction() . "/onboardingform/core/admin/login.php')</script>";
  exit();
}