It feels like yesterday that I started to play Second Life. Â We were all noobs once! Â Now I have a habbit of referring to Second Life as a ‘game’, but can it really be referred to as that? Â I mean really, Second Life is more of a real Second Life. Â I know people inside of this virtual world that really get married, date, etc… the possibilites are endless, and sometimes odd. Â The thing is, Second Life for me is a business, not a mere game. Â I have two virtual stores where individuals can come and buy items, textures, and scripts. Â With these two stores Second Life is actually supplimenting my income and paying my web hosting fees, go figure! Â
So anyhow, at my stores I have a few items that need to be downloaded externally, from my website. Â To do this I needed to incorporate Second Life’s LSL scripting language with my PHP based website and MySql Database. Â I started to search on how to do this as I was new to LSL and had done PHP for sometime. Â So now I have this huge script (huge for LSL that is) that takes care of what I need and now I’m releasing it to the public for critique.
I am only releasing the LSL part of the script, you can play with it how you want, edit, etc… but do not resell it at all. Â I login to Second Life daily and if I see this script floating around, you can bet I’ll file a DMCA against you. Â SO… without further ado, here’s the code:
// The right amount to be paid.
string hover_text = "Touch Me";
string objName = "TGA Tanned Asian Kit";
string SECRET_STRING="xxx";
list l;
key botKey = "xxx";
// The URL of your website.
string http_host = "xxx";
// The PHP that handles the $_POST variables.
string http_path_sender = "xxx";
// We're sending only, not receiving.
//string http_path_reciever = "/SL/SendToReciever.php";
Â
Â
Â
vector red = <1.0,0.0,0.0>;
vector green = <0.0,1.0,0.0>;
vector yellow = <1.0,1.0,0.0>;
vector blue = <0.0,0.0,1.0>;
Â
string http_url_sender;
key http_request_id; // internal id for every http sender request/response.
list http_parms = [HTTP_METHOD,"POST",HTTP_MIMETYPE,"application/x-www-form-urlencoded"]; // details of request.
//string http_body; Â // URLescaped variables. ie. name=Maria%20Torvalds&sex=F
Â
integer x;
integer total_number;
key usrNow;
default
{
   state_entry()
   {
     // get permission from the owner to pay out money using the llGiveMoney function.
     if(llGetScriptState("group") == TRUE){
       llSetScriptState("group", FALSE);
     }
     if(llGetScriptState("group1") == TRUE){
       llSetScriptState("group1", FALSE);
     }
     //llSetScriptState("group", FALSE);
     //llSetScriptState("group1", FALSE);
     http_url_sender = "http://" + http_host + http_path_sender;
     for (x = 0; x < total_number; x++)
     {  // x is the student touch number
       // usrNow is the key of the student
       usrNow = llDetectedKey(0);
       //llGiveInventory(usrNow, llGetInventoryName(INVENTORY_NOTECARD, 0));
     }
     if(llGetObjectName() != objName){
       llSetText("Please place all items back in the box. \n Without the scripts inside the original box \n Nothing will work correctly.", red, 1.0);
       state default;
     }
     state startscript;
   }
}
state startscript
{
   state_entry()
   {
     llSetText(hover_text, yellow, 1);
   }
   touch_start(integer total_number)
   { // Event waiting for a student touch
     integer x;
Â
     for (x = 0; x < total_number; x++)
     {  // x is the student touch number
       // usrNow is the key of the student
       usrNow = llDetectedKey(x);
       //llSay(0, "Avatar Key: " + (string)usrNow);
     }
     if(usrNow != llGetOwner()){
       llSay(0,"Only the owner of this object can use it, killing script.");
       state default;
     }
     llGiveInventory(usrNow, "Instructions");
     llGiveInventory(usrNow,llGetInventoryName(INVENTORY_LANDMARK,0));
     llDialog(usrNow, "Please read the \"Instructions\" notecard first. \n \n Have you read the notecard and are you ready to redeem your purchase?",["Yes", "No"], -33);
     integer ListenID = llListen(-33, "", usrNow, "");
   }
   listen(integer chan, string name, key id, string mes)
   {
     if(id == llGetOwnerKey(id)){//won't listen to objects unless they aren't in the region.
       if(mes == "Yes")
       {
         state start_send;
       }
       else if(mes == "No")
       {
         state default;
       }
     }
Â
   }
}
state start_send
{
   state_entry()
   {
     llSetText("Sending data to web server.", yellow, 1.0);
     llInstantMessage(usrNow,"Beginning the data process.");
     //llSay(0, "Avatar Key: " + (string)usrNow);
Â
     //** Create http_body
     //** maybe (sure) a function for speed up this creation proccess is needed when goes more complex
     string avname = llKey2Name(usrNow);
     llSetText("Acquired avatar name.", yellow, 1.0);
     llInstantMessage(usrNow,"Acquired avatar name.");
     string item = llGetObjectName();
     llSetText("Acquired object name.", yellow, 1.0);
     llInstantMessage(usrNow,"Acquired object name.");
     // Generating fake password
     float rnd_1 = llFrand(999999999) + 100000000;
     llSetText("Password generated.", yellow, 1.0);
     llInstantMessage(usrNow,"Password generated.");
     // Now IM the avatar that paid me
     llWhisper(0,"The folling information will be an Instant Message with important information regarding your account on my site.");
     llInstantMessage(usrNow,"\n~*=*~*=*~*=*~\nUsername: "+avname +"\nPassword: "+(string)rnd_1+"\n Please DO NOT LOSE THIS INFORMATION \n~*=*~*=*~*=*~");
     llSetText("Password given to avatar.", yellow, 1.0);
     // Now do the database stuff with the PHP file.
     integer i;
     string body;
     l = ["avatar",llKey2Name(usrNow),"object",llGetObjectName(),"pass",rnd_1,"objectID",llGetObjectDesc()];
     integer len=llGetListLength(l) & 0xFFFE; // make it even
     for (i=0;i<len;i+=2)
     {
       string varname=llList2String(l,i);
       string varvalue=llList2String(l,i + 1);
       if (i>0) body+="&";
       body+=llEscapeURL(varname)+"="+llEscapeURL(varvalue);
     }
     //   string data=llEscapeURL(SECRET_STRING)+(string)SECRET_NUMBER;
     string data=llMD5String(SECRET_STRING,0);
     //llWhisper(0,"Data code is:" + data);
     //** and do the request
     http_request_id = llHTTPRequest(http_url_sender+"?hash="+data, http_parms, body);
   }
   http_response(key request_id, integer status, list metadata, string body)
   { //** This event handle the response of our previous request
     if (request_id == http_request_id) {
       llSetText("Connecting to server..", red, 1.0);
       if (body)
       {  //** Print output of http request IF connection was succed
         //llSetText(body, green, 1.0);
         llSetText("Server contacted, please wait...", yellow, 1.0);
         llWhisper(0, "Server response:\n---\n" + body + "\n---");
         llWhisper(0, "Information downloaded, removing script.");
         state remove_script;
       }
       else
       {  //** IF NOT make a fake response with error text
         llWhisper(0, "Maybe the server is off-line. Connection was impossible.");
         body = "ERROR. Remote Script (" + http_host + ") unavailable. Please try again in a few minutes.";
         llWhisper(0, "Resetting script");
         state startscript;
       }
     }
     state startscript;
   }
}
Â
state remove_script
{
   state_entry()
   {
     llSetText("Touch to Join \n Phyrax Designs Group \n to be notified of product updates \n and specials along the way.", yellow, 1);
     llRequestAgentData(botKey, DATA_ONLINE);
   }
   dataserver(key queryid, string data)Â
     {
       if(data =="1"){
         llSetScriptState("group", TRUE);
       }else{
         llSetScriptState("group1", TRUE);
       }
       llRemoveInventory(llGetScriptName());
     }
}
Â
December 12th, 2009 at 2:01 am
Hi Phyrax,
I am a school teacher. I am planning to make a script in second life for my students to upload their multiple choice answers to a database through php.
Can you give me some tips on uploading data from SL to an external database by php?
December 12th, 2009 at 12:16 pm
Sure thing, the fact is that when posting to a php script outside of second life, you need to use llHTTPRequest() with a sort of array like list(”varName”,”varValue”) Couple that with the POST method of llHTTPRequest() you’re actually sending data with that method to the website or script you decide, then just assign variables using the $_POST['varName'] you decide. I’ll be writing a blog post within the next few days on this issue at my new blog if you wanna keep watching that one. Also there is a little bit of code on the LSL Wiki if you want to check it out, though it covers RPC versus llHTTPRequest() it can still get the basics done. RPC to PHP
December 15th, 2009 at 8:20 am
Thank you Jay.