CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Hmm, erase?

 



Anthony
Deleted

Jan 24, 2000, 4:52 AM

Post #1 of 15 (5102 views)
Hmm, erase? Can't Post

First off, I've decided to hold off on finding a way to restrict file size and/or dimensions in my BB and chat.

Ah, ha .. but a new challenge arises. I'd like to have a function whereas to create a 'Whos On' function that will ultimately, well, tell who's on .. ie logged into chat or the board.

All fine and dandy. Upon login, their user name is appended to a list (in pretty markup) and displayed in a small browser window (via javascript of course) that can be accessed through a hyperlink (again, most likely saying "Who's On!").

I can do that.

What I don't know how to do is erase their name from the list if their browser closes suddenly (or if they change the URL or hit a bookmark).

Now, I have a "Log Off" button and if they would be nice enough to hit that every time they left, there'd be no problem. But you know browsers these days .. no respect for staying stable -- they crash as often as Bill Clinton gets new cigars.

Any ideas?


Borderline
Deleted

Jan 24, 2000, 5:08 AM

Post #2 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

Hi Again Smile

I too had the same problem when I wrote my chat thingy(That I never actualy finished). I delt with it in two ways. First to try and keep it as up to date as possible I used a JavaScript onUnload and onLoad event to place there name in the file and to take them out. And then like you said computers crash, browsers crash (especialy on Windows)etc.. So I ended up time stamping the names and setting a timeout for how long they would remain there between posts.
So when someone posts one of the many things the script did was update the posters time stamp. I would recomend you use DB_File for the database of names and time stamps. Then when someone would click on the "Who's On" link the script would go through the database for that chat room and delete old entries ( I set the limit on 1 hour) then display what was left.

Let me knoe if this helped
Scott


Anthony
Deleted

Jan 24, 2000, 8:00 AM

Post #3 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

Well, cripes .. I'll have to try that.

Curious -- how did you get the JavaScript onUnload to take their name off the list? I understand using onLoad {then something like reload(true)} but how did you get onUnload to work?


Borderline
Deleted

Jan 24, 2000, 8:17 AM

Post #4 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

Well my method was a little lame but It should give you an idea. With a little research I am sure you could come up with something better.
I used a small pop-up window that called a Perl Script to delete the name. The Perl script then returned a simple page that said Welcome or Goodbye and had a simple JavaScript in it to close it's self.(self.close)
Here is the JavaScript that called the pop-up <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


<SCRIPT LANGUAGE="JavaScript">
<!-- JAVASCRIPT --
function doit(){
msg = window.open('index.cgi?add=yes&cat=$db','mywin','resizable=no,width=1,height=1,scrollbars=no');
msg.document.close();
}
function undoit(){
msg = window.open('index.cgi?del=yes&cat=$db','mywin','resizable=no,width=1,height=1,scrollbars=no');
msg.document.close();
}
window.onload=doit;
window.onunload=undoit;
//--END JAVASCRIPT-->
</SCRIPT>
</pre><HR></BLOCKQUOTE>
Ofcoarse the $db was the name of the category they were in.

HTH
Scott


Anthony
Deleted

Jan 24, 2000, 9:54 AM

Post #5 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

Drat! *snaps fingers*

I wanted to avoid a popup window because it slows exit (something users don't like).

Ah well, I'll do that for now and try to think of something else.

Thanks for all your help -- I am sure I'll be posting more.


Borderline
Deleted

Jan 24, 2000, 9:59 AM

Post #6 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

Well something has got to execute the Script somehow before the browser closes. This was the only method I could come up with. Of coarse you could just stick to the times between posts and perhaps make the time out 15 minutes.
There is really no accurate way I know of to do this.
I guess that is why everyone is using IRC or Java Smile

Scott


Anthony
Deleted

Jan 24, 2000, 11:07 AM

Post #7 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

Bleh -- Java bites it, you know.

I know it is possible via some CGI/JavaScript combo .. I've seen it.

Ever seen WBS (before it was closed down by GO Networks)? Their chat system was CGI/JavaScript/HTML and had a nice Who's On function. They didn't use timestamping methods because you could 'lurk' (hand out in the background not posting) for hours at a time .. but the list was accurate up to five minutes either way.

Well, I still appreciate all your thoughts on this -- if I luck (and I do mean luck) into a solution, I'll let you know here, okay?

Anthony Bouvier


Borderline
Deleted

Jan 24, 2000, 11:55 PM

Post #8 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

Hey,

I was thinking about your problem today and I think I came up with a possible solution that is better than what we discussed so far.
You could use a hidden frame that reloads every 5 minutes. You don't even need JavaScript for this. Just print a refreash header with the Perl script the frame calles. Have the same Script that prints the refreash header update a database with the users name and a time stamp.
Now when someone click on "Who's On" you can be accurate up to five minutes. Anyone with a time stame that is over five minutes old you just delete and display the rest.
In all browsers I have worked with refreashing a hidden frame is not even noticable.

Hope this Helps
Scott


Anthony
Deleted

Jan 25, 2000, 5:09 AM

Post #9 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

Still the timestamping and removing from the list those who do not post within five minute intervals, does not keep the list as accurate as I would like. The list would only show those who have recently (within five minutes) logged on.

Ah, ha! But fret not -- I do not walk away from our nice discussion without anything. Your hidden frame suggestion has sparked an idea in this noggin o' mine.

Hmm, example?

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if($action eq 'show'){ &RoomLogin; }
elsif($action eq 'chat'){ &Chat; }
else{ &Entry; }
</pre><HR></BLOCKQUOTE>

Associating $action (which is the current form parameter 'action') with a sub to do the various things.

I think by using something akin to

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


javascript : onClose(this.form.action="Logoff")
</pre><HR></BLOCKQUOTE>

in the hidden frame I may be able to get the script to execute the 'erase' of that user's name if the browser closes (via voluntary exit {bookmark, new location} or involuntary {browser illegal operation or OS crash/freeze}).

Actually, I haven't thought this out. It's too early and I am at work.

See anything I am missing in my thought process? Logically, doesn't it at least -sound- like a valid idea?

Well, I'll think more about it later.

Thanks for your help, Scott.


Anthony Bouvier

[This message has been edited by Anthony (edited 01-25-2000).]

[This message has been edited by Anthony (edited 01-25-2000).]


Borderline
Deleted

Jan 25, 2000, 7:58 AM

Post #10 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

There is no way with JavaScript to do anything after a browser crashes or OS crashes or the user just disconnects from the Internet. JavaScript is not ran by the server it is ran by the Browser.

I think you misunderstood what I was trying to say. If a frame refreshes every five minutes and that frame calles a script for each refresh your timestamp will be accurate to within 5 or 6 minutes. So if a timestamp is over 6 minutes old it must mean the browser crashed or user disconnected etc.. The user posting messages to the chat room will have nothing to do with the timestamp. The timestamp would be generated from the hidden frame refreshing.

Scott
PS:
I prefer this to a bunch of elsif statements
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


SWITCH: for ($q->param('action')) {
/^show$/ and do{ &RoomLogin; last SWITCH; };
/^chat$/ and do{ &Entry; last SWITCH; };
die "Action not understood";
}
</pre><HR></BLOCKQUOTE>
TIMTOWDI Smile

[This message has been edited by Borderline (edited 01-25-2000).]


Anthony
Deleted

Jan 25, 2000, 9:06 AM

Post #11 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

Ah! Darn, you're right .. I didn't take into account a system/browser crash.

And I see I did misunderstand (I said it was too early Smile )-- I'll have to try and see if I can implement your earlier suggestion.

Anthony Bouvier

PS

Ah, though there may be more typing using a bunch of elsif statments -- it does 'read' more like english to the programmer, no? Which is one of the benefits of Perl .. to me at least.

Thanks!


Jasmine
Administrator

Jan 25, 2000, 1:09 PM

Post #12 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

Personally, I don't like the if ... elsif ... else statements myself. I find it really cumbersome for very large programs.

My lastest kick is using references to manage what action the program should take based on form input. For example, one action could be to display "introductory" or "help" information, another could be to display user options. Since I wanted each section to have different headings, suboptions, etc., I found that using subroutine and array references was the easiest way to do this.

Here's how I use references to make my life easy Smile

By creating an array for section elements, individual subroutines for each section, and creating a hash that associates the array and subroutine to an individual section, I was able to easily manage a large program as well as add and delete sections and section elements as needed.

This way, by simply passing an "action=1", the program checks the hash for the appropriate subroutine and array to use. (I prefer using digits because it lends to greater flexibility (eg $FORM{'action'}++ Wink

So first, in a configuration file, I defined arrays that I want to be specific for each section:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


@main = ("Home Page","Instructions");
@accounting_setup = ("Departments","Top Level Categories","Chart of Accounts");
</pre><HR></BLOCKQUOTE>

Then, beneath that, I created a hash that accociates the page heading, array, and subroutine (added in the next step) with an single numeric key:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


%accountmain = (
0 => ["Main",\@main,\&intromain],
1 => ["Accounting Setup",\@accounting_setup,\&introaccounting],
);
</pre><HR></BLOCKQUOTE>

Then, in the body of the program, I use the following line (after calling the read input subroutine):

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if (defined($accountmain{$FORM{'action'}}[2])){
&{$accountmain{$FORM{'action'}}[2]};
}
else {
print "Subroutine for $FORM{'action'} doesn't exist.<P>
The defined sections are:<BLOCKQUOTE>";
@acct = sort {$a <=> $b} keys %accountmain;
foreach (@acct){
print "$accountmain{$_}[0]<BR>\n";
}
print "</BLOCKQUOTE>";
}
</pre><HR></BLOCKQUOTE>

Basically, the if block checks to see if the key (which was passed to the program) exists for %accountmain. If it does, it invokes the subroutine that's referenced in %accountmain and the subroutine knows which array to use. You'll just have to be careful (as usual) that the referenced subroutine exists.

The subs look like:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub intromain {
print qq~
Hello - in $accountmain{$FORM{'action'}}[0]<BR>
<BLOCKQUOTE><FORM><SELECT NAME="blah">
~;

foreach (@{$accountmain{$FORM{'action'}}[1]}){
print "<OPTION>$_<BR>";
}
print "</SELECT></FORM></BLOCKQUOTE>";
}

</pre><HR></BLOCKQUOTE>

You're still invoking a normal subroutine, but need a special format to access the heading and arrays.

Heading (the first element in the section's hash) needs to be accessed using $accountmain{$FORM{'action'}}[0].

The array (the second element in the section's hash needs to be accessed using @{$accountmain{$FORM{'action'}}[1]}

You can see this in action... First section, Second section and Third section (undefined action)

If any of this is confusing, please let me know and I'll be glad to elaborate.


[This message has been edited by Jasmine (edited 01-25-2000).]


Borderline
Deleted

Jan 25, 2000, 1:37 PM

Post #13 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

Jasmine:
That's a neat trick but I run all my code under strict refs Smile.
<ducking and running>

Scott

[This message has been edited by Borderline (edited 01-25-2000).]


Jasmine
Administrator

Jan 25, 2000, 6:07 PM

Post #14 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

The above code works even when using strict refs because they're using hard references, not symbolic references.

eg:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use strict 'refs';

$testing = 5;
$reference = \$testing;

print $$reference;
</pre><HR></BLOCKQUOTE>

will correctly print 5 because it's using a hard reference. However, if you're using symbolic references, like so:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use strict 'refs';

$testing = 5;
$reference = "testing";

print $$reference;
</pre><HR></BLOCKQUOTE>

it will yield an error that the string can't be used while under strict refs.

No harm, no foul Smile


[This message has been edited by Jasmine (edited 01-25-2000).]


Borderline
Deleted

Jan 26, 2000, 9:31 AM

Post #15 of 15 (5102 views)
Re: Hmm, erase? [In reply to] Can't Post

Yes I really should look at code closer before I make a reply.
Not my style but TIMTODI
Scott

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives