Reply
Wolfee Yaffle
Posts: 38
Topics: 3
Kudos: 1
Blog Posts: 0
Registered: ‎08-14-2010
0 Kudos

Can a lsl script write text to a website using javascript instead of PHP?

[ Edited ]

I've been wanting to create a visitor tracker with a web interface for my land. What I want is a lsl script that will stay inactive until I call on it from the website, then it will send a list of who is on my land to the website. I'm not sure how to script the request button or have the lsl script write info to the website. Also my web hosting plan doesn't support PHP. It's the free hosting plan that comes with your domain if registered from Doteasy. Would I need to upgrade my plan? Or is there a way to do it under javascript? I've been working with a lsl script that reads text from my website, but I'd like one that writes TO it. I don't want the lsl script to remember everyone who came by, just display who is currently there.

Darkie Minotaur
Posts: 1,616
Topics: 8
Kudos: 154
Blog Posts: 0
Registered: ‎02-23-2010
0 Kudos

Re: Can a lsl script write text to a website using javascript instead of PHP?

[ Edited ]

Reply to Wolfee Yaffle - view message

What you would nee is so called HTTP in - a call to the script inworld with the script acting as a webserver. You don't need php for that - javascript can do that . The problem is, that the URL of the server can change - and will under certain cirumstances (read here). So the answer to that part would be: you can, but it's not be a good solution.

What you usually do, is that the in-world server reports it's URL to a php file which in turn stores it. But that would require PHP and some means to store data (either write a file or a database).

 

ETA:

If you're going to update your hosting plan, be aware that having php doesn't mean that you can call your lsl script - most hosting providers block calls to odd ports which LL uses.

Wolfee Yaffle
Posts: 38
Topics: 3
Kudos: 1
Blog Posts: 0
Registered: ‎08-14-2010
0 Kudos

Re: Can a lsl script write text to a website using javascript instead of PHP?

Reply to Darkie Minotaur - view message

I'm not sure I understand how that would send text TO a website, which is what I want it to do so that I can see who is on my land without having to log in.

Rolig Loon
Posts: 24,951
Registered: ‎10-05-2009
0 Kudos

Re: Can a lsl script write text to a website using javascript instead of PHP?

[ Edited ]

Reply to Wolfee Yaffle - view message

You could use something like this ....

http1 = llHTTPRequest(URL + "&name=" + avname ,[HTTP_METHOD, "POST"],""); // Send the data string to URL

where name is a variable that the script in your external server expects to see, and avname is the value you are passing.

Not as dumb as I look
Member
Talia Davidov
Posts: 149
0 Kudos

Re: Can a lsl script write text to a website using javascript instead of PHP?

[ Edited ]

Reply to Wolfee Yaffle - view message

The issue Darkie is talking about is that if you want an on demand report from a scripted object in world, you are going to need to know the URL of that scripted object so you can send it the command to send you the report.

As example, lets say you make a button on your website, you press that button.  Then your website sends a command to your prim in world to send you the current residents.  Your prim then sends you the informaiton.

It is the "your website sends a command to your prim" part that is the issue.  To do that, you are going to need to know the URL of the prim in world, and this is where Darkie is warning you of the changing URL of prims.

There was a quick solution that was on the wiki but the author has deleted it.  I will see if I can find it agian.

Anyway, none of this tells you how to actually send the results, which I am sure someone with more LSL http knowledge will chime in with.

edit to add: found the solution on the authors wiki http://was.fm/wiki/Permanent_Primitive_URL

edit to add2: and it looks like while I was typing my reply Rolig gave you pointer on how to do the communication itself :smileyhappy:

 

Member
Ela Talaj
Posts: 813
0 Kudos

Re: Can a lsl script write text to a website using javascript instead of PHP?

Reply to Wolfee Yaffle - view message

In-world objects would always send messages to website and receive messages back in HTTP. On the in-world side the engine assembling HTTP messages to send and processing those received must be written in LSL. On the web page side the processing engine could be written in any language that the web site supports. It could be PHP, it could be C++, it could be Java or it could be Javascript. PHP is used most often only because it is supported by most websites and has convenient methods to talk to databases.

Elegant Solutions by The Gold Club
Darkie Minotaur
Posts: 1,616
Topics: 8
Kudos: 154
Blog Posts: 0
Registered: ‎02-23-2010
0 Kudos

Re: Can a lsl script write text to a website using javascript instead of PHP?

Reply to Wolfee Yaffle - view message

Well, now you should have about everything you need:

- a 3rd party database so you don't need one as descibed by Talia

- the http_response event to receive requests from your website

To send requests from your website, you can either use a frame that calls your tiny URL (see Talia's posting) that shows the reply on your website or Ajax (see here for an introduction) - the events could be anything like pressing a button on the page, loading the page or a timer. This is standard javascript web programming.

Posts: 4,688
Registered: ‎09-15-2009
0 Kudos

Re: Can a lsl script write text to a website using javascript instead of PHP?

[ Edited ]

Reply to Wolfee Yaffle - view message

From the department of diagnostic scripting (an in-joke which you should ignore)

Your javascript needs to make an HTTPRequest to the URL of your SL object (as described by Darkie, et al).  This is the programmed equivalent of a person clicking on a hyperlink to get a web page.  A quick Google-search for "javascript HTTPRequest" finds any number of examples but this is a good illustration:

 

Javascript HTTPRequest

function httpGet(theUrl)
{
    var xmlHttp = null;
    xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false );
    xmlHttp.send( null );
    return xmlHttp.responseText;
}

 

In your LSL script you need an http_request() event handler (what to do when someone sends you an HTTPRequest).  The wiki http://wiki.secondlife.com/wiki/Http_request illustrates that nicely.  You need to edit the final 'else' part of the example.  Put there all the processing you want to do (such as firing a sensor to see who's around).  In the sensor() event-handler you assemble the names into a string and return that to your web-site with the llHTTPResponse() function http://wiki.secondlife.com/wiki/LlHTTPResponse.  !! Remember !! If there's no-one around a sensor() event won't be raised, you'll need to handle no_sensor().

 ETA:  Thus, LSL writes TO the web-site in response to a request FROM that web-site.  What you then do with that response and if/how you store it depends on the rest of your site's setup.  The essential difference between PHP and javascript is that PHP runs server-side, while javascript is client-side.  That is, a PHP page is usually an unopened document on the server.  When the server gets a request for that page it opens it, processes it, sends whatever the result is and closes it again.  With javascript you've already opened the page in your browser and are processing it there.  The SL script is the one sitting doing nothing until it gets the request and gives the response.

Occam's Razor (House version): the simplest explanation is invariably ... someone screwed up.
Wolfee Yaffle
Posts: 38
Topics: 3
Kudos: 1
Blog Posts: 0
Registered: ‎08-14-2010
0 Kudos

Re: Can a lsl script write text to a website using javascript instead of PHP?

[ Edited ]

Reply to PeterCanessa Oh - view message

Thanks for all the help everyone, but I have absolutely no idea what I'm doing. I don't know any PHP or javascript. I'm trying to learn js but it seems too much work just to send data. I can do the LSL scripting side of things, but the web scripting side is just too confusing. I don't suppose I can request someone to build this for me? Here is what I've done so far. I did use the tinyurl thing btw.

lsl script

default
{
    state_entry()
    {
    }
    touch_start(integer total_number)
    {
        llHTTPRequest("http://www.wolfeedarkfang.com/test.html", [HTTP_METHOD,"POST"], "");
    }
    http_request(key id, string method, string body)
    {
        llOwnerSay("request - " + body);
        //llHTTPResponse(id, 200, body);
    }
    http_response(key id, integer status, list metadata, string body)
    {
        llOwnerSay("response - " + body);
        //llHTTPResponse(id, 200, body);
    }
}

You can see I currently have most functions //'d out until i figure out what they do and how it all works. I'm using ownersay so thescript tells me the results. It just spits out the contents of the test.html file though. Nothing else...

 test.html contents

function httpGet(http://tiny.cc/swivs)
{
    var xmlHttp = null;
    xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false );
    xmlHttp.send( null );
    return xmlHttp.responseText;
}

 I'm sure the web side is done completely wrong, and I have no idea what to do with the info listed. I may just give up.

Posts: 4,688
Registered: ‎09-15-2009
0 Kudos

Re: Can a lsl script write text to a website using javascript instead of PHP?

[ Edited ]

Reply to Wolfee Yaffle - view message


Wolfee Yaffle wrote:

I may just give up.


Weeeellll, you aren't going to get it done soon, that's for sure.  On the other hand do you need it enough to pay anyone for it?  If you really need it and don't want to learn programming for any other reason then go ahead - the place to post is in the wanted and/or in-world employment forum.  On the other hand, if you're willing to stick at it and learn you've got two fairly major tasks ahead of you: i) reading all the LSL wiki links I gave you so you can re-do your LSL script, ii) learning about javascript.  You can forget all about PHP, that's completely different to what you're trying to do.

We're here to help with the scripting side.  All I'll say about the javascript is:

Occam's Razor (House version): the simplest explanation is invariably ... someone screwed up.