programming noob needs perl help
I found a simple script that will allow me to use flash to do registration and log-ins so that users can resume their games where they left off at a previous time. The script I have only has a few variables. I have been able to modify the button in flash to include 2 extra variables besides the username and password, so now the script writes email and the level to the txt file (database). The only problem is that I can't seem to get the email and level back from the txt file and into flash.
if ($query->param('action') eq 'check') { I believe this is telling it that it is doing the login action as opposed to new registration
my $found = undef;
if (open (ACC, $acc_file)) {
while ( my $query1 = CGI->new(\*ACC)) {
(my $username = $query1->param('username')) || last;
($username eq $query->param('username'))
and ($query1->param('password') eq $query->param('password'))
and $found++;
}
That part just checked to make sure the username matched the password in the database. Well, being the complete noob that I am, I tried adding and ($query1->param('level') eq $query->param ('level')) but that only made it so that the login checked to see if the level varaible matched, and since I'm not asking the user to provide which level they were in last in a form, that makes no sense.
close(ACC);
$error = $found ? undef : 'incorrect username/password';
} else {
$error = 'unable to open user database';
}
}
my $status = $error || 'Ok';
print "status=$status";
So, what do I add to this to tell the script to get the variable 'level' from the txt database to send back to flash?
Did any of that make sense?
Wil posted this at 14:39 — 14th April 2002.
They have: 601 posts
Joined: Nov 2001
Hm. That's a strange way of doing things.
Can you post a sample of your database file, maybe one line so I can see how you extract information from the database?
It looks to me like it's opening up the file, checks for an username, but matches the password agaisnt the one supplied by the user so theoetically it doesn't check the password at all. Hm.
- wil
zen posted this at 14:44 — 14th April 2002.
They have: 8 posts
Joined: Apr 2002
Okay, the first 3 lines were all sent because of a form in the registration page in flash, and they are the username, password, and email. The last one is the level, and it is a variable set by flash so that when the user logs in he/she can resume the game where they left off.
This is what one use of the script writes:
username=user
password=pass
email=email%40domain.com
lev=1
=
The final '=' must be a separator, right?
Wil posted this at 14:47 — 14th April 2002.
They have: 601 posts
Joined: Nov 2001
It could be a seperator if everyone's data is stored in one big text file, or are they all in indivdual files?
Will 'level' always be passed to the database? As in, will a value always be present?
- wil
zen posted this at 14:55 — 14th April 2002.
They have: 8 posts
Joined: Apr 2002
Yes, it's writting one file, not individual files. I just thought it was curious that it would use the = as a seperator or delimitor (or whatever the technical term is) when it's using those to equate the variable name with the variable.
Yes, I have will have it set so that when the user registers, lev=1, and when the user completes a level, flash will access the script and change it. Oh, hehe, I haven't done that part yet and I'm starting to scratch my head. hehe... how will I do that?
Anyway, thankyou soooo much for your amazingly fast responses!!!!
Wil posted this at 15:15 — 14th April 2002.
They have: 601 posts
Joined: Nov 2001
Aha. Bingo. Now I get it. Oh my god, that's a silly way of doing things. Basically, what the script is doing is keep all your name/value variables in the hash %query and then what it's doing is read the file and split the name/value pairs (that's why they're deliminated by a =) and load them into the env. var. %query1. My gosh, that's ugly.
Anyway, you want to set the var. "lev" (not "level" as this is how it's written in the file) to be the value in the database. An ugly way to do this would be just to edit it to:
my $found = undef;
if (open (ACC, $acc_file)) {
while ( my $query1 = CGI->new(\*ACC)) {
(my $username = $query1->param('username')) || last;
($username eq $query->param('username'))
and ($query1->param('password') eq $query->param('password'))
and $found++;
$query->param('lev') = $query1->param('lev');
}
Try that and see what it does.
- wil
zen posted this at 15:19 — 14th April 2002.
They have: 8 posts
Joined: Apr 2002
Wil,
I don't want to take advantage, but you seem so willing to help and I am so lost when it comes to programming!
Here is the script minus the header:
use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
my $acc_file = 'accounts';
my @required_fields = qw(username password);
unless ( -f $acc_file) {
(open(ACC, ">$acc_file") && close(ACC) && chmod 0600, $acc_file) || die "cannot create user database";
}
my $query = CGI->new();
my $error;
$error = 'not enough parameters' unless $query->param('action');
print CGI->header( -type=>'text/plain' );
if ($query->param('action') eq 'new') {
if (open(ACC, "$acc_file")) {
while (my $username = CGI->new(\*ACC)->param('username')) {
if ($username eq $query->param('username')) {
($error = 'username already exists');
last;
}
}
close(ACC);
} else {
$error = 'unable to open user database';
}
if ( !$error
&& (@required_fields == grep { $query->param($_) } @required_fields) ) {
$query->delete('action');
if (open(ACC, ">>$acc_file")) {
$query->save(\*ACC);
close(ACC);
} else {
$error = 'unable to open user database';
}
} else {
$error ||= "not enough parameters";
}
}
if ($query->param('action') eq 'check') {
my $found = undef;
if (open (ACC, $acc_file)) {
while ( my $query1 = CGI->new(\*ACC)) {
(my $username = $query1->param('username')) || last;
($username eq $query->param('username'))
and ($query1->param('password') eq $query->param('password'))
and $found++;
}
close(ACC);
$error = $found ? undef : 'incorrect username/password';
} else {
$error = 'unable to open user database';
}
}
my $status = $error || 'Ok';
print "status=$status";
also in that folder goes the .htaccess file which looks like this:
Order allow,deny
Deny from all
The script writes to a file called accounts
and the button in flash after I modified it:
on (release) {
loadVariablesNum ("http://www.mydomain.com/cgi-bin/access.cgi?action=" + _root.query +"&username=" + _root.username + "&password=" + _root.password + "&email=" + _root.email + "&lev=" + _root.lev, 0);
gotoAndPlay (2);
_root.status = 5;
}
Wil posted this at 15:27 — 14th April 2002.
They have: 601 posts
Joined: Nov 2001
OK. What about where the information is returned to when the user logs in again. What does your flash/non flash buttons look like then?
Did you try what I posted above? That might work on extracting the 'lev' out of the database. If not, I'll try something different.
- wil
zen posted this at 15:39 — 14th April 2002.
They have: 8 posts
Joined: Apr 2002
I tried that and now the flash sits there trying to log in.
Perhaps I pasted it wrong. I will try again.
What does flash look like. Well, for right now, I have the flash setup so that after you hit the login button (with the code) it forwards to a scene where it shows you all the data in the file ( this is for me so I can tell if it's working ), but right now it only gets back the username and password.
zen posted this at 16:01 — 14th April 2002.
They have: 8 posts
Joined: Apr 2002
Okay, I now realize that I will probaby not be able to use this script this way..
So far, I have been able to successfully use the script to write four things into the database: username, password, email, and level. The first 2 were a part of the script when I got it. I am also successfully at recieving username & password back into flash.
Aside from the problem that I cannot get the script to send email and lev back to flash, I now recognize that the script was not made to change an entry after it is made. In other words, it appears to me by looking at this script that I cannot use the script to make lev=2 after it has already been written as lev=1.
I think the only way that I can solve this problem is to set it up so that a user can register at level 1 (after the completed the intro level (0)) and when they complete level 1, the will be forwarded to a flash scene that uses a second (very similar to registratioN) scene that access a copy of the script with a different name that will write a new database for lev=2 users. So, if I do it that way, I can get around the fact that this script cannot change the database, but...
I still need to get the lev varable back out of the database.
zen posted this at 16:27 — 14th April 2002.
They have: 8 posts
Joined: Apr 2002
That is what I will do.
I will just make a separate copy of the cgi script for every level in the game. When the user first registers, it will write their data to a database assigned to level-1 using the script for registering at level-1. Because of this, I no longer have to write a variable 'lev' because every entry in that database will be level-1. When the user starts the next level, I will pass the username and password to the flash file for that level, so that it can write those variables if the user completes level-2 using the script for level-2. Then the level-2 database will contain only the names of level-2 users. When a user leaves the game and returns to log in, I will include a field that asks which level they wish to resume at, and have flash access the appropriate login-script for that level. This also gives the advantage of login in at a lower level if they wish.
Wil posted this at 19:51 — 15th April 2002.
They have: 601 posts
Joined: Nov 2001
OK. Now I'm totally confused as to what you're trying to do
Did you get that level bit sorted or have you abandoned the idea? Can I ask where did you get the script from - I might know of better alternatives lying around, or I can help you fix this one up to work again.
Cheers
- wil
zen posted this at 20:23 — 15th April 2002.
They have: 8 posts
Joined: Apr 2002
I've looked around for something, but this script was the closest I could find. I thought I had it figured, but I just realized that I don't think there is a way to get the database to show highscores...
As a preface (I'm creating a multi-level game in Macromedia Flash)
Here's my objectives for a perl (or php) script:
A simple database that contains username, password, email address (optional) and level.
To access the database for the following purposes,
-Write to database from macromedia flash SWF file to create a database of the above mention data (registration).
-Read database from flash SWF file (login) to so that the flash file can send the user to the correct level (game).
-ReWrite to existing database to change the level variable.
-Read database from flash or html to display users sorted by level variable.
I know my host supports PHP because I ran a phpinfo.php3 on it and it gave all the info on it. But, I've never used telnet and can't figure out how to create a database. So, after not finding any php that work with flash for this purpose, I gave up on that. The closest perl script I've been able to find to do this is the one I posted previously, but it appears ill-suited and flawed.
My question to you, Wil, is do you have any familiarity with Flash Actionscript. It was written based on javascripting language. This is the type of thing that would probably be easy for you on the perl end, but if you don't know flash, it may not be so easy.
How hard would it be for you to write something like this from scratch. (and do you accept donations via paypal)
Wil posted this at 09:46 — 16th April 2002.
They have: 601 posts
Joined: Nov 2001
Hi
I have some experience with Flash scripting, yes. Not much 'raw' experience, but rather using Flash 5 and click and point to select the code and actions needed.
What you're trying to do with PHP/Perl sounds simple. You don't *have* to have a database backend, a flat-file backend will be fine. Well, it does depend on the number of uses you have too. How many gamers are we talking here?
Sure, I'm more than willing to help you out - keep your dollars in your pocket, though - if I can help you out through TWF then the whole community benefits which is worth more than any dollar, IMO. And it gives others a chance to chip in with their $.02. I learned a lot from these style of communities and I'm a great believer in them.
Right then, back to business... what are the next steps here. I'm not sure that the program you have is necessarily 'broken'. I'm sure what you have can be tweaked rather easily. Send me a copy to [email protected] and I'll take a look at it from there. Have you got a working url for the flash as well?
Cheers
- wil
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.