2015-02-24 17:15


  • php
  • unity3d
  • javascript

I know that PHP happens server-side and javascript is executed once the page loads in the browser. I'm trying to figure out how to save a variable in PHP so that it can be used in a javascript function later on. Here are the bits of my code where I'm trying to do that.

This all happens in the same index.php script:

    // If session token exists, set javascript variable 
    if($_SESSION['token'] != null) 
        // Store session token as a variable 
        $sessionToken = $_SESSION['token']; 

      // Turn that PHP variable into a javascript variable
      var sessionToken = '<? echo $sessionToken; ?>';

      // This function works. It sends a string to the Unity web player
      // I'm trying to call it using a PHP variable as a parameter
      function SessionTokenToUnity($token) {
        // Send message to unity web player from browser
        u.getUnity().SendMessage("Settings", "MyFunction", $token);

      // Call function with sessionToken which was set by PHP earlier as a parameter

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享