I can see vars in a textbox, but my script does not work?
Hi,
this is a very common problem..
Loadvariables just sends a request to the server, and the movie continues to play. After some time (say a few seconds), flash will have received a response from the server.
If you are staring at one of those text fields, you just ... wait a moment
If you want to use the data in your script, you have to add some waiting to your script.
technique a - frame loop:
1st frame in row:
set _some variable you expect_ to a value that will not be sent (if you are expecting a "status" with possible values "ok" or "fail", set it to "waiting")
loadvariables
play (in case the movie was stopped and this is a button action)
2nd frame - no code
3rd frame:
if the particular variable still has the _impossible_ value, go back one frame
4th frame - data is ready for use
If your data does not contain a suitable variable for the checking, just add one.
Possible extension: use getTimer when sending to the request and in frame 3 to detect a possible timeout (server does not respond, sends a scrit error instead of data, ...)
Frames 2 and 3 can be used to show a "server busy" animation
technique b - event handler:
if you use a data movieclip (loadvars object), you can use the onclipevent(data) or the onload function. In that case code using the variables would be placed in the event function, or the event function would advance a stopped movie to the next frame
Ok here we go ... It's 3:38 after midnight and I still cand find a solution..
I have a flashmovie that asks for a username and a password, then it sends them to a php script which conects to a database, checks for the username and password provided and if it finds them it returns a You made it message and a variable called user_check with the value "ok".
Now the thing is that when I check into the flash movie the variable user_check, it seems like it does not has the OK value in it.
Yes the testing textbox that I created displays the OK value just fine and yes I have places a delay mechanism with a loop that says
If user_check == "ok" goto and play the frame that shows a menu else if user_check =="no" goto the beggining of the movie so the user can enter again a username and password, BUT the IF condition in flash acts like the user_check variable is empty or with a different value than the OK value that php script sends.
The php script works fine and it has no bugs as far as I have seen so I believe the problem has something to do with flash.
PHP code is here:
<?
$dbcnx = mysql_connect("mylocalhost","myusername","mypasswo rd");
mysql_select_db("access_list",$dbcnx);
$result=mysql_query("SELECT LoginName, LoginPassword FROM users");
$authorized="no";
$username=$_GET['username'];
$password=$_GET['password'];
while ($row = mysql_fetch_array($result)){
if ($username==$row["LoginName"] and $password== $row["LoginPassword"]) {
print('&_level0.check_status=You Made it');
print('&_level0.user_check=ok');
$authorized="yes";
}
}
If ($authorized=="no"){
print('&_level0.check_status=Authorization Failed');
print('&_level0.user_check=no');
}
?>
Actionscript IF command is here:
if (_level0.user_check=="ok") {
gotoAndStop(45);
}
a) important security hint: passing username and password via GET method might mean that some systems will reveal the secret data when scrolling through the browser history - use POST method
b) database hint: do not read all the users...
select from users where LoginName = '$username' AND LoginPassword = '$password'
c) try to arrange the movie like that:
(frame label "form"): movie stopped, form with submit button on stage, button code
user_check = ""
loadVariables("login.php", ..., "POST")
play()
next frame (framelabel "loop") - no actions
next frame:
if(user_check == "") gotoAndPlay("loop")
else if(user_check == "ok") gotoAndPlay("success")
else gotoAndPlay("form")
<?
$username=$_POST['username'];
$password=$_POST['password'];
$dbcnx = mysql_connect("myhost","myusername","mypassword");
mysql_select_db("access_list",$dbcnx);
$ask=('SELECT LoginName, LoginPassword FROM users WHERE LoginName="'.$username.'" AND LoginPassword="'.$password.'"');
$result=mysql_query($ask,$dbcnx);
$row=mysql_fetch_array($result);
if ($row['LoginName']==$username AND $row['LoginPassword']==$password) {
print('&_level0.check_status=Success');
print('&_level0.user_check=ok');
} else {
print('&_level0.check_status=Failure');
}
?>
Scripts works ok no problem here
What happens is again that flash seems to Ignore that user_check variable has a value of ok. Instead it thinks that it is still empty and it goes into an infinite loop!
Actionscript for checking is here :
if (user_check=="") {
gotoAndPlay(44);
} else if (user_check=="ok") {
gotoAndStop(45);
} else {
gotoAndStop(1);
}
44 frame: the one that that returns the timeline to 43 keyframe that contains this checking code
45 frame: the frame where menu appears is login is correct.
1 frame: stop action and where the form appears.
I hate being kind of pain in the ass here but i have spent about 4 hours again with no result.
I'm not very good with PHP yet, BUt I don't advice you to have your php script to DIRECTLY connect with the SQL database, open a HUGE ASS security hole. Just letting you know
There is one thing to avoid: never build an app that gets the mysql password and / or complete select commands from the browser unless you are very sure it is protected enough
Originally posted by kasracer I'm not very good with PHP yet, BUt I don't advice you to have your php script to DIRECTLY connect with the SQL database, open a HUGE ASS security hole. Just letting you know
why is there a security hole ? I always connect directly to my database using :
Code:
<?
$mysqlhost="serverhost";
$mysqldb="database";
$mysqluser="username";
$mysqlpwd="password";
$conn = mysql_connect($mysqlhost, $mysqluser, $mysqlpwd) or die(mysql_error());
mysql_select_db($mysqldb, $conn) or die(mysql_error());
?>
Every PHP&MySQL tutorial will teach you the same thing...
PS: to the starter of this thread : Is your .swf file on the same server as the php script(s) you're running ? otherwise Flash&PHP won't be able to communicate.
A good coding practice is to put your database username and password in a separate file outside your public_html directory and use include(''); to make this data available to your PHP script. This is done to prevent an HTTP user from viewing the file if they create conditions under which your script won't execute and will get displayed as simple text revealing all the sensitive data.
Anothing thing is to create separate accounts for different actions. For all PHP pages that access the database from a remote host the user I created only has the select command. Any user that has higher privileges has to be localhost only.
Hi,
I've set up a simple username/password section in my site using one of the tutorials from this site, it works fine but all the usernames / passwords are internal in my Flash movie and therefore when I add new ones I will have to republish the site, is there any way of geting it to read a simple external text file that would contain all of my username/passwords? I also found a tutorial on getting flash to load text into a movie from an external file so really I'm just trying to combine the two…
Would somebody be able to direct me to an appropriate tutorial or .fla? I don't want to use PHP or other language.
Any help would be appreciated,
Cheers Wayne
mmm well yes you can read in a text file. Check out the tutorials that came with flash it should have a walkthrough on how to do it. The only problem is that when you use a text document it wouldn't be secure unless you have them encrypted and then write some encryption into your flash program. I think you have to setup the text file like
if ur on a free server chances r that there will be ads on the page, so u need something like this in the php:
PHP Code:
print("?name=$name&id=$id&waste=");
when u view a normal web site the ad code is after the rest of the code. so is it if u use loadVars. but if u didnt have the &waste= part that code would be part of the id variable. now it is part of the waste variable. then in your flash u can say that waste =1; and all of the ad code is gone.
there is a problem with this mehtod and that is that the ad code will sometimes contain & or something and then a new variable is created. if anyone know of a better way, please share it.
Reading a .txt file in Flash is really simple but not suitable for passwords.
Just put a .txt file on your server and type the address in a browser. Yep! Everyone can read all your names and passwords in a beautifull Courier font type.
So there goes the basic idea of usernames and passwords: security.
Conclusion: Flash alone is not suitable for passwords.
And IggyV doesn't help either: the passwords
in your .swf are relatively save (if the fla is published with the "protect from import".) Creating an .as (= text file!) is readable for everybody.
The only way to use pretty safe ways of the name and password stuff: a serverside scripting and/or a database. (PHP, CGI, MySQL and if you really want trouble: (MicroSoft) ASP).
I kind of feel like an ass for posting a simple question in the middle of your database conversation, but I need somebody who knows flash and PHP to pinpoint my issue.
I have attached the .fla (which is loaded into my _root swf) and a txt file with my PHP script, which is based on Musicman's tutorial on emailing with PHP. I am working with PHP 4.3.5, but I have tried to use all the updated scripting that Musicman suggested.
I have obviously missed/don't understand something, because the swf will do what it's supposed to, and the script seems fine, but they don't connect to each other.
Please help and offer an explanation if you can...I really want to understand this stuff, but I'm a lot less knowledgable in PHP than with Flash.