PDA

View Full Version : as3.0 login with php and mysql



roll4dasoul
May 17th, 2009, 03:30 AM
Hello everyone,

Currently I'm trying to make a login with flash. Using actiosncript 3.0 I'm able to connected to a single php with stored info on it. Everything works fine just between php and as3.0. Then I try to connect it to the database (instead of static info in the php file) and it breaks. I ran some tests and was able to connect to the database with php and print the same strings as the static php file...yet the static php file works and the one connecting to the db does not (but they print the same exact responses wtf). Any help would be great because I'm lost.

Not working php code:



<?php

$n=$_POST['usernameStr'];
$p=$_POST['passwordStr'];

$hostname='localhost';
$username= 'x';
$password= 'x';
$conn = mysql_connect($hostname, $username, $password, 'test')
or die ('Cannot open database');
//database login (end) working


if(!empty($_POST)){
$query= "SELECT * FROM test.user WHERE uname='$n' and pw='$p'";
$result=mysql_query($query);
$count = mysql_num_rows($result);

if($count==1) {
print "resultCode=LOGGED_IN";
}
else
{
print "resultCode=NOT_LOGGED_IN";
}
}

?>ps. I know security isn't there but I'm just trying to connect at the moment I'll deal with the other stuff later. Also here is the static working php file.


<?php

error_reporting(0); // disable all error reporting

$user = $_POST['usernameStr'];
$pass = $_POST['passwordStr'];

$storedPassword = "1cb251ec0d568de6a929b520c4aed8d1";

// At least some POST data is required

if(!empty($_POST))
{
if($user == "guest" && md5($pass) == $storedPassword)
{
print "resultCode=LOGGED_IN";
}
else
{
print "resultCode=NOT_LOGGED_IN";
}
}

?>Lastly I will also include my as3.0:

var phpFile:String = "http://localhost/risingArtistry/blog2.php";
var loggedIn:Boolean = false;

function loginClick (e:MouseEvent):void{
var usernameStr:String = usernameTxt.text;
var passwordStr:String = passwordTxt.text;

var allFields:Boolean = true;

if(usernameStr.length < 1)
{ allFields = false; }
if (passwordStr.length < 1)
{ allFields = false; }

if (!allFields)
{addChild(error1Msg);
error1Msg.x = 400;
error1Msg.y = 300;
addChild(closeBtn1);
error1Msg.statusTxt.text = "please enter your username and password";
closeBtn1.x = 540;
closeBtn1.y = 215;
}

var variables:URLVariables = new URLVariables();
variables.usernameStr = usernameStr;
variables.passwordStr = passwordStr;

var urlRequest:URLRequest = new URLRequest(phpFile);
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = variables;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, sendHandler);
loader.load(urlRequest);
}

function sendHandler(e:Event):void
{
var loader:URLLoader = URLLoader(e.target);
var variables:URLVariables = new URLVariables(loader.data);

if(variables.resultCode == "LOGGED_IN")
{
// message sent
loggedIn = true;
//form.statusTxt.htmlText = "<font color=\"#009933\">Logged in!</font>";
gotoAndStop("secret_page");
}
else if(variables.resultCode == "NOT_LOGGED_IN")
{
// message not sent
addChild(error1Msg);
error1Msg.x = 400;
error1Msg.y = 300;
addChild(closeBtn1);
error1Msg.statusTxt.text = "The Username/Password you entered was incorrect";
closeBtn1.x = 540;
closeBtn1.y = 215;
}
else
{
// unknown response
error1Msg.statusTxt.htmlText = "<font color=\"#FF0000\">Unknown ERROR</font>";
}
}

//php info (end)//

stop();

If anyone has any suggestions please let me know. This is first time I've tried connecting to a database with flash. Also if anyone knows a good place to go to get info on this type of stuff that would be awesome...I could only find a few books on this and no tutorials.

Dimava
May 17th, 2009, 10:16 AM
I would try to debug it in the following way:



Print out your $query to the screen
You can even take it a step further and print out every one of the variables (i.e.
print "Posted username: $n <br> Posted Password: $p <br> Query: $query <br> Count: $count<br>";
Run that exact query in phpmyadmin see if you get exactly one result


Let me know how that goes

Navee
May 17th, 2009, 11:05 AM
Everything looks good from here. I have not converted and of my AS2 database projects to AS3 (because they are working and its pointless!) When you connect to your php script from flash are you sure the values @$n and $p are posting from flash...curious?

1. Not that it should make a difference, but in your flash script I noticed that you declared the username and password variables and defined them with the appropriate .text fields. My thoughts are the variable names are the same as the URLVariables you are passing from flash. Maybe you should just assign the .text lines @ the URLVariables.

2. In my headbanging days of getting flash to work with php/mysql I have always had a problem with using double quotes in the $query. It works via php/mysql but try wrapping your $query with single quotes

$query = 'SELECT ...etc';

3. Lastly, a good friend of mine Matthew Keefe wrote the book Flash and PHP Bible (Wiley Publishing) and he could point you in the right direction. He has a site where you will find resources on the issues you are having or may eventually run into. http://scriptplayground.com/

Cheers

Shaedo
May 17th, 2009, 12:02 PM
if you run your php in your browser including inputting the variables what kind of response do you get ?

eg

www.youwebaddressyourrunning.com/yourconnect.php?usernameStr=yourusername&passwordStr=123456 (http://www.youwebaddressyourrunning.com/yourconnect.php?usernameStr=yourusername&passwordStr=123456)

would strongly advise you get your php working in your browser ,including taking and spitting out variables correctly, before linking it with swf....

best of luck!

roll4dasoul
May 17th, 2009, 02:44 PM
hmm still trying a few things...the php to mysql is fine. I tried some debugging with an html page and checked the values that were being entered in the form to the ones with sql in phpMyAdmin and they were the same. Plus once i login on a browser the screen prints either LOGGED_IN or NOT_LOGGED_IN appropriately. However even though this works i still think the problem is in the php and I say that because I tried to run a trace in flash with the working script for the resultCode variable and that worked. Then i ran a trace for the same variable (variables.resultCode) and the output panel said undefined for the none working php.

Also tried the single quotes around query but no luck....i'll look at the scriptplayground site too. If there anymore ideas that would be great. Thanks for the replies so far.

roll4dasoul
May 18th, 2009, 01:37 PM
hurray problem solved. One of my friends and I (dimava) found there was a blank line before the php was declared in the php file. This was adding extra characters to the resultCode variable causing an undefined result when referencing the variable. wow all of that non sense for one blank line in a php file, wtf.