Part I: Swishmax
Hey guys,
First you will need two textboxes, with the VARIABLE names of user and pass(I always keep the names relevant, to help remember)
Second, I made a little submit button, with only an onpress gotoandplay(13)
(Actually, I used a label)
Actual code:
Code: |
on (release,keyPress("<Enter>")) |
Now we will start with the code for the label and login, and then go from
there.
Code: |
onFrame (13) |
Line 1: Do this on frame 13(Kinda self explanatory)
2: Set label for my submit button, I use labels to keep things organized
3: I am using loadvariable for a SPRITE, so make sure you group everything as a
sprite
4: Close tag
5: On frame 19, The reason I have the delay in here is it takes a little while
for php to run through the code and give us back the response, so give it a few
frames.
6: You will understand this once we get to php, because the php sends us back a
variable &login and then we check that variable.
7: On nothing I have removed what I don't want if the login correctly
8: The reason I do this is because I want to be able to reference the variable
user later, to show them their username(You would
reference by using _root.user)
9: What? Php says you logged in wrong.
10: This is another textfield with the variable name of 'res', I use this to
display the message try again. You can also go to a different frame, anything
you want here.
You can include this code in any swishmax file you want. As long as your php
file is in the same place, it will work in any swishmax file
Part II: Php
To start things off, you will need a simple mysql
table in your database with the fields Username and PASSWORD. These are how
they are referenced in this code. Also, the table's name will be login in this
example
To create this database:
Code: |
CREATE TABLE `login` ( |
Now we can FINALLY get to our php
Code first, explain later (The
name is login.php)
Code: |
<?php |
Ok guys, I will explain only the lines I said before(In
my comment about understanding other tutorials..
$query = "SELECT Username, PASSWORD FROM login WHERE Username = '$user'
AND PASSWORD = '$pass' AND position = 'Member'";
$rows = mysql_query($query) or die ("You has an
Error in login.php; $query.". mysql_error()); //Query the db
$numrows = mysql_numrows($rows);
Here we check for the entry. First, you see the mysql_query, which gets the
info from the database. The main thing here is mysql_numrows. This returns the
number of rows I get from the database. If there would be a matching username
and password, then it would return a 1, otherwise, it would be 0
if($numrows > 0) else
If we don't get any rows back, then we tell swish the password and username is
wrong.
THE CODE BELOW IS OPTIONAL
$result = @mysql_query("SELECT Username FROM
login WHERE Username='$user'");
while ( $row = mysql_fetch_array($result))
Here we are getting the information from that row. Since I have _root.user =
user, this is what that is for. Say you wanted to know the username for later
use, like a welcome message? That is where this comes in at.
|