Q: Session no longer exists when resend WWW Unity3d and PHP I created a system that sends the user id by Unity3D and on the server creates a session and sends the session back to the client (on Unity3D), so it all works! But when I send back from Unity3D to the server the session is simply gone! I already checked the browser and it works, it just does not work on Unity3D!
Does anyone have any idea what it is?
C# on Unity3d
void Start () {
   WWW w = new WWW (url+ "?id=" + userId);    
   StartCoroutine (SessionWWW (w));
}

private IEnumerator SessionWWW (WWW _w){
    yield return _w;  

    PlayerPrefs.SetString("cookie", _w.text);

    Debug.Log (PlayerPrefs.GetString("cookie"));
    Debug.Log (_w.text);
}

void Update(){
    if (Input.GetKeyDown (KeyCode.P)) {
        string session= PlayerPrefs.GetString("cookie");
        WWW w = new WWW (url "?sessionid=" + session);    
        StartCoroutine (GetSessionWWW (w));
    }
}

private IEnumerator GetSessionWWW (WWW _w){
    yield return _w;  
    if (_w.text == "ok") {
        Debug.Log ("received!");
    } else {
        Debug.Log (_w.text);
    }
}

PHP script:
<?PHP
session_start();

if(isset($_GET['id'])){
    $id = $_GET['id'];
    $_SESSION['session'] = $id;
    echo $_SESSION['session'];
}
if(isset($_GET['sessionid'])){
    if(isset($_SESSION['session'])){
        echo $_SESSION['session'];
    }

}
?>


A: If your wanting to set the session id of the current session in PHP use the function session_id($id) BEFORE session_start.
https://www.php.net/manual/en/function.session-id.php
<?php

//do you need both of these?
if(isset($_GET['id'])){
    $id = $_GET['id'];
    session_id($id);
}
if(isset($_GET['sessionid'])){
    $sessionid = $_GET['sessionid'];
    session_id($sessionid);
}

session_start();

print_r($_SESSION)
?>


session_id ([ string $id ] ) : string 
If id is specified, it will
  replace the current session id. session_id() needs to be called before
  session_start() for that purpose. Depending on the session handler,
  not all characters are allowed within the session id. For example, the
  file session handler only allows characters in the range a-z A-Z 0-9 ,
  (comma) and - (minus)

you might also want to validate before setting the session id to prevent mistakes from your code and if this is on the internet other people can find it too.
(from the link above)
function session_valid_id($session_id)
{
    return preg_match('/^[-,a-zA-Z0-9]{1,128}$/', $session_id) > 0;
}


$id = $_GET['id'];
if (session_valid_id($id)) {
    session_id($id);
}

