Opening an sql connection from your game isn’t really safe. So you should write, use a server software or find another solution like the one I wrote this article.

Why you should use http protocol instead of server software doing this ?

  • These software might have bugs and if you don’t know any low level programming language, you can’t fix it.
  • Hard integration process. If you don’t have any experience, in this way will take lot of time.
  • If you are not developing a mmo game, generally http protocol will be enough to do what you want.

Of course server software will be faster but if few hundred milisecond latency won’t be a problem for your game this is the way to follow.

First send a request without header parameter or if you want to send request with specific header you can also set in begining but it is less safe.

var form = new WWWForm();
form.AddField("hash",hash);
form.AddField("userName",username);
form.AddField("password",password);
form.AddField("query","login");
var w = WWW(URL,form);
yield w;
if (w.error != null){
        print(w.error);
}else{
	result = w.text;
	w.Dispose();
}

Now, we send our request but each time we try to send another request unity will recreate header so we should save the session id to send with same id on other requests.

var v : String[] = Regex.Split(w.responseHeaders["SET-COOKIE"],";");
header.Add("Cookie",v[0]);

Do not forget to clear or recreate header on your login function cause “Add” function doesn’t overwrite the same key.

After login you can send requests without username&password because you already sent this information to server and server cached this with your session id. From now on you can send requests like this:

var w = WWW(URL,form.data,header);
yield w;
if (w.error != null){
	print(w.error);
}else{
	result = w.text;
	w.Dispose();
}

And get the results of requests.

By the way Unity WebPlayer doesn’t provide a function to read session,cookie etc. But you don’t need to read because Unity is not recreating header. Browser sending and saving header and not changing it so you can authenticate without saving&sending header information.

Have a nice day.

One Response to How to send requests with SessionID in Unity Standalone
  1. How do I track a user’s session in Unity web player? That is, how do I know the session in the unity web player is active?


[top]

Leave a Reply

Your email address will not be published. Required fields are marked *