Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

LSL - PHP - MySQL Question

Web Magic
Registered User
Join date: 30 May 2008
Posts: 12
10-25-2008 18:45
So I am trying to register residents in-world to my joomla site and something is wrong. If someone could take a look at my code, that would be much appreciated (and thanks to those who have already helped:)

LSL
CODE

// Global variables
key http_request_id;
integer inUse = FALSE;
string usrName;
string usrKey;
integer password;

string phpForm = "http://www.mywebsite.com/register.php";

// Split function on "|"
string splitf(string strSplit, integer i)
{
list split = llParseString2List(strSplit, ["|"],[]);
return llList2String(split, i);
}

default
{
touch_start(integer total_number)
{
if (inUse) {
llSay(0, "System registering "+ usrName +", Please wait.");
} else {
inUse = TRUE;

usrName = llKey2Name(llDetectedKey(0));
password = llRound(llFrand(899)+100);

http_request_id = llHTTPRequest(phpForm, [HTTP_METHOD,"POST",HTTP_MIMETYPE,"application/x-www-form-urlencoded"], "&password="+ (string)password + "&username="+ usrName);
}
}

http_response(key request_id, integer status, list metadata, string body)
{
if (request_id == http_request_id && status == 200)
{
if (splitf(body, 0) == "usrReg")
{
if (splitf(body, 1) == "ok")
{
llInstantMessage(usrKey, "Your Login name is: "+ usrName + ". Your password is "+ (string)password +".");
}

if (splitf(body, 1) == "fail")
{
llInstantMessage(usrKey, "Registration failed, error: "+ splitf(body, 2));
}

inUse = FALSE;
}
} else {
llSay(0, "Website encounterd a problem, please try again later.");
}
}
}


CODE

<?php session_start();

$hostname='myhost';
$username='myuser';
$password='mypass';
$dbname='mydatabase';

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);
// username and password sent from SL
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
//I want to verify if the user exists already
$sql="SELECT * FROM jos_users WHERE username='$myusername' ";
$result=mysql_query($sql);

while ($row = mysql_fetch_array($result))
{
$username = $row['username'];
}
$sql1="SELECT * FROM jos_users WHERE username='$myusername'";
$result1=mysql_query($sql1);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result1);
// If result matched $myusername, table row must be 1 row
if($count==1)
//I have no idea if this is right in regard to the http_response in the LSL
{
echo "You are already registered";
}
else
{
//This is the encryption to convert the password to joomla database - I am pretty sure this part is right, at least the conversion
$part = explode(":",$password);
$salt = $part[1];
$encrypted_password = md5($mypassword . $salt).":".$salt;
;
//The following is updating the user information into the four tables necessary to register someone in joomla. the number of tables and information is correct, though someone may want to look at my code
mysql_query("INSERT INTO jos_users (name, username, password) VALUES('$myusername' , '$myusername' , '$encrypted_password' ) ");

$id1 = mysql_connect();

mysql_query("INSERT INTO jos_comprofiler (id, user_id) VALUES ('$id1' , '$id1' ) ");

mysql_query("INSERT INTO jos_core_acl_aro (value, name) VALUES ('$id1' , '$username' ) ");

$id2 = mysql_connect();

mysql_query("INSERT INTO jos_core_acl_groups_aro_map (group_id, aro_id) VALUES ('18' , '$id2' ) ");
}
//Do I need to echo something for the http_response
mysql_close();
?>


Again thanks in advance and once I get this working I will post the final code as I know there are many joomla/sl users out there.

Web
Osgeld Barmy
Registered User
Join date: 22 Mar 2005
Posts: 3,336
10-26-2008 01:42
what version of joomla are you using?

i love 1.5.x but it tends to screw up everything established (im currently fighting with it)

maybe this will help ...

http://forum.joomla.org/viewtopic.php?f=432&t=207689
Pedro McMillan
SLOODLE Developer
Join date: 28 Jul 2007
Posts: 231
10-26-2008 01:59
First thing I notice is that the body of your HTTP request from LSL starts with an ampersand (&;). It would be worth taking that out. It shouldn't be a problem really, but it's worth being thorough!

A quick explanation of how PHP output and LSL HTTP response works: everything you echo ends up directly in the body of the HTTP response. For example, if I do this in PHP:

CODE

echo "blah";
echo "|foobar";


...then my HTTP response body in LSL will be "blah|foobar". You don't need to do anything fancy at all, but you do need to make sure everything is output correctly. Judging by your LSL script, one of the very first things your PHP script should do is echo "usrReg".

Next, if you attempt to connect to the database, judging by your LSL script, you should output "|fail|Failed to connect to the database". That makes sure your object in LSL knows registration failed, and the reason why.

Some of your SQL stuff is a bit confusing. Your first query gets all records where the username matches the avatar's name, and then proceeds to copy the username from the database. Bear in mind, your querying *only* retrieved usernames matching the avatar name in the first place, so that whole bit is not achieving anything:

CODE

// TAKE THIS BIT OUT...
$sql="SELECT * FROM jos_users WHERE username='$myusername' ";
$result=mysql_query($sql);

while ($row = mysql_fetch_array($result))
{
$username = $row['username'];
}
// ... DOWN TO HERE


Your next bit (the $sql1 stuff) checks for an existing avatar, which is fine. However, judging by your LSL script it ought to output "|fail|Username already registered", so that again, your object in SL knows the registration failed and why.

Now we come to your encryption.... I'm guessing you copied this from some Joomla code somewhere? First of all, you've confused your variable names... "$password" contains your MySQL password, not your user password. Second, you're trying to split it up at a colon to extract a password and the encryption 'salt', and yet you are not passing that salt (excuse the pun!) from LSL at all. To be honest, I have no idea how Joomla encrypts its passwords, but presumably the salt is defined somewhere in the site configuration. Best to check with a Joomla guru for this.

Likewise, I can't tell you if the database insertion code is correct. It looks a little odd, but then so does lots of code for Content Management Systems!

Finally, you'll need to output "|ok" near the end to indicate if registration was successful.


That's everything I can see at first glance. Hope it's helpful!
Alicia Sautereau
if (!social) hide;
Join date: 20 Feb 2007
Posts: 3,125
10-26-2008 09:31
From: someone

// If result matched $myusername, table row must be 1 row
if($count==1)
//I have no idea if this is right in regard to the http_response in the LSL
{
echo "You are already registered";
}

echo "usrReg|fail|You are already registered"; // usrReg the command, fail is the registration attempt and then the error message


From: someone

//Do I need to echo something for the http_response
mysql_close();

Yes! :)
for the script to know that their registration succeeded, it must get a respond

echo "usrReg|ok";

no idea either about the joombla workings :x