- Home /
SQL in web player
I was reading about SQL for Unity and ran across this phrase:
Since it's using a local file for the database, it is NOT possible to use it for Web Player applications
While breathing hysterically I have come across someone who said:
You would instead use client <-> php / asp <-> database Is it true or are there any recent changes that allow me to use SQL directly in Mono from a web player? The reason I need a database is mainly UserID and easy variable updating (like urls, coordinates and points and stuff). Thanks!also you would never access a regular webhosted database (oracle, mssql, mysql) this way as you do not want to make wildcard remote accounts for users to directly connect and hack in no time due to the login details being sent over to by them and stored in the client.
As far as I can remember one problem is you can only ship your game in webplayer with a subset of .Net assemblies. And this subset does not include any SQL connectors.
I still don't know anything about SQL, I just began to study it. Do you mean that I won't be able to connect the webplayer with my DB if I use SQL? Is there a way to bypass it using PHP for instance?
Answer by shaystibelman · Feb 09, 2012 at 07:09 PM
Posting my code for others to reference:
function checkPassword(){
//set the WWWForm to send to the PHP page
var sendLoginInfo : WWWForm = new WWWForm();
sendLoginInfo.AddField("Username",Username);
sendLoginInfo.AddField("Password",Password);
//send the WWWForm via WWW
var getdata : WWW = new WWW("www.saxum.co/Citta/DataBase/GetUserInfo.php",sendLoginInfo);
yield getdata; //Wait for the data to return
canLogin=getdata.text; //Check if the data contains the TRUE flag
//if TRUE >> Load level
if(canLogin=="true") {
Application.LoadLevel("citta loading");
login=true;
}
}
and the PHP code:
<?php
$con = mysql_connect("host","username","password");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
$insertedUsername=$_REQUEST['Username'];
$insertedPassword=$_REQUEST['Password'];
$sqlCheckPassword="SELECT Password FROM Users WHERE Username = '$Username'";
$query=mysql_query($sqlCheckPassword,$con);
if (!$query){
die('Error: ' . mysql_error());
}
$Password=mysql_fetch_row($query);
if ($insertedPassword==$Password[0]){
echo "true";
}
mysql_close($con);
?>
Answer by Bunny83 · Feb 09, 2012 at 05:45 PM
I just want to clarify some points:
In this question i suggested to use SQLite for standalone builds since it doesn't require the user to install a database server. SQLite is an embedded SQL system which doesn't have a client-server structure. It works on database-files but allows you to use SQL to read / write data. Because SQLite works with local files it can't be used in the Webplayer due to security restrictions.
If you want to create a webbuild you usually host your game on a webserver. In this case it's the easiest to use php & Mysql on the webserver.
Usually you NEVER allow direct access to a database server from the internet. Unity builds can easily be decompiled and therefore your username / password aren't safe. Everybody can manipulate your whole database. That's why you usually allow only local access to the database and let PHP verify and perform the SQL stuff on the server.
I guess you could use any 100% managed SQL connector even from the webplayer, but you have to think about the crossdomain.xml to actually allow the socket connection. But, as already said, everybody can read your access data if you direct connect to a database.
thanks, i already got over that part. I'm now stuck with the data I/O part. How do i build a basic login function?
i have done the interface using GUI. i have the database up and running. i have created a php query to check if the password is correct according to the username inserted.
how do i get the "green light" back to unity and my js interface so that using the same "login" button click would also load the next level!?
Your php page should do the check if the login was successful and return this information. A php page is a normal webpage, but the content can be altered dynamically. In Unity you use either a post or a get-request and send your username / password to the server. The php page then processes this request and returns a webpage (data, text, what ever). This generated result is returned to Unity, because that's the content you've requested from your server.
The php page could return the text "successful" or "failed" or the session ID if you want to use server-side-sessions.
Great! I got it to work! Thank you so very much! $$anonymous$$y doubt was that "echo" was enough to get information back. and my problem was that i put all the WWW code inside the if(GUI.Button) block ins$$anonymous$$d of its own function so i could use Yield.
Answer by IgnoranceIsBliss · Jan 25, 2012 at 11:28 AM
Unity can not directly access databases (at least not without additional assemblies) but it CAN access web-pages using the System.Net classes and it's own WWW classes.
So why not make PHP/ASP or other similar web pages that interact with your web-server? That way you could then access pages that take data from and to your database.
Even better, you don't expose the database to the world - it's safely behind your web pages so you can validate and sanity check the incoming data.
I'll try to read about that, thanks. Do you have an example page to let me check out so I have something to study?
No, but it's pretty straight forward and examples shouldn't be needed if you understand the concept.
Let's say are storing a database of player scores, and want to show a 'Top 10' list at the start of your game.
You create a web page that gives you a fixed structure (say an X$$anonymous$$L file or tab-delimited file) that queries the database for these records. If you don't know PHP or your web language of choice, I'd suggest looking up help for that.
Then in your Unity code, you do something like this. (this is written here in the comments without any checking, so take it with a grain of salt...)
[C# CODE]
HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("http://www.yourserver.com/top10players.php"); HttpWebResponse Resp = (HttpWebResponse)Req.GetResponse(); TextReader Rdr = (TextReader)new StreamReader(Resp.GetResponseStream()); string Content = Rdr.ReadToEnd();
[/C# CODE]
This would read the text from your web page. You could then either break up the string or parse it as an X$$anonymous$$L file and read your list of items.
You could also use the HttpWebRequest functions to POST data to a web page that then saves it to a database.
They are two seperate subjects though - if need examples, you'd search for the 'HttpWebRequest' or 'WWW' tutorials to read the information in to unity, and your PHP/ASP tutorials for how to provide the data on a web page.
ok, so I got the database up and running and I managed to insert data from unity to a table from the editor. But exporting a web player and loading it to my server, didn't let me pass the data to the table. I have already put crossdomain.xml in the root dir of the website (to be sure i also put a copy in every other dir) but nothing.
how come?
There are examples even on this site.
Note that WebRequest is a .NET / $$anonymous$$ono class and some of them are not supported for security reason. In Unity for most http stuff you can use the WWW class that comes with Unity.