HttpObject
by Hallsofvallhalla · in Torque 3D Professional · 03/18/2010 (12:36 pm) · 1 replies
I am new to Torque(purchased a couple days ago) and spent quite a bit of time trying to get my Gui login to work through my mysql database. I searched the forums and found little info on how to get it working and found others wondering the same thing. I am a binary owner only so without source it really made this harder.
I finally got it working so I thought I would post how I got it working. I took from several sources already here. Like I said I am new to Torque so if this is a bad way of doing it or there is a resolve that i missed then I apologize. if someone sees a way for me to make this better please post it. I am still learning how the GUI works with variables and such.
Firstly I build the GUI with username and password fields. Not going to explain that just make sure to name your text fields to username and password.
I then added these functions to the bottom of my login.gui
i could not find a way to add a & in the url passing without modding source so I used explode in the php script to separate the username and password after the ===.
my only problem now is the accessing database...., i had to put that in there because there is a delay of about 3 - 5 seconds on the information to be retrieved..anyone know why? its localhost..or does anyone know a better way of doing this, like freezing everything until the answer comes back?
here is the php script
just change the values to match your database and fields.
I finally got it working so I thought I would post how I got it working. I took from several sources already here. Like I said I am new to Torque so if this is a bad way of doing it or there is a resolve that i missed then I apologize. if someone sees a way for me to make this better please post it. I am still learning how the GUI works with variables and such.
Firstly I build the GUI with username and password fields. Not going to explain that just make sure to name your text fields to username and password.
I then added these functions to the bottom of my login.gui
function netlogin()
{
%username = $playername;
%password = $password;
%server = "localhost:80";
%path = "/torque/";
%script = "login.php";
%query = "username="@%username@"==="@%password;
%hto = new HTTPObject(CharLogin);
%hto.get(%server,%path @ %script,%query);
}i could not find a way to add a & in the url passing without modding source so I used explode in the php script to separate the username and password after the ===.
function CharLogin::onLine(%this,%line)
{
if (StrStr(%line, "success") != -1)
{
resultguitext.text = "Log in Successful";
return;
}
if (StrStr(%line, "failure") != -1)
{
resultguitext.text = "Username or Password is incorrect";
return;
}
else
{
resultguitext.text = "Accessing Database...";
}
}my only problem now is the accessing database...., i had to put that in there because there is a delay of about 3 - 5 seconds on the information to be retrieved..anyone know why? its localhost..or does anyone know a better way of doing this, like freezing everything until the answer comes back?
here is the php script
<?php
$username = $_GET['username'];
$exploded_username = explode('===',$username);
$newusername = $exploded_username[0];
$password = $exploded_username[1];
include "connect.php";
$query="SELECT * from torque where username='$newusername' AND password = '$password'";
$query2=mysql_query($query) or die("Could not query players table");
$query3=mysql_fetch_array($query2);
if (isset($query3['username']))
{
echo "success";
}
else
{
echo "failure";
}
?>just change the values to match your database and fields.
About the author
Been in game design for about 4 years now. Some of my titles are Forsaken sanctum 3d mmo and a web browser mmorpg. Urban Realms - WB mmo. Planetary Wars - WB mmo. Quests Of Crocania WB mmo. also teach tutorials on Web Development.
Torque 3D Owner Ronny Bangsund
Torque Cheerleaders
Prepared statements + not blindly accepting any user input would be much safer.
(Alternatively a wrapper like DBI to ease the transition between different database types)
I dunno about the delay, though. But if you are running MySQL on Windows all bets are off!