Enoch Ng
Enoch Ng

Reputation: 23

Parsing PHP in ActionScript returns "undefined"

I know this question has been asked before, but I've had little success even after visiting similar questions. I'm currently developing a Flash project which involves being able to create an account and log in. However, I keep getting "undefined" from my attempts to parse PHP output.

Here is my ActionScript 3.0 code for the function that contains the code.

function gosubmit(event:MouseEvent) {
    if (username.text != "" && password.text != "") {
        var phpVars:URLVariables = new URLVariables();
        var phpFileRequest:URLRequest = new URLRequest();
        phpFileRequest.url = "php/controlpanel.php";
        phpFileRequest.method = URLRequestMethod.POST;
        phpFileRequest.data = phpVars;

        var phpLoader:URLLoader = new URLLoader();
        phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
        phpLoader.addEventListener(Event.COMPLETE, execResult);

        phpVars.username = username.text;
        phpVars.password = password.text;

        phpLoader.load(phpFileRequest);
        phpLoader.addEventListener(Event.COMPLETE, execResult);

        function execResult(event:Event) {
            trace(event.target.data.execResult);
        }
    }

Here is the PHP code in php/controlpanel.php.

<?php

mysql_connect("localhost", "root", "password");
// change localhost to something else later
// change password to something else later
mysql_select_db("game");

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$salt_mysql = mysql_query("SELECT salt FROM players WHERE username='".$username."'"); 
$salt = salt_row["salt"];    

$unhashed_password = $password.$salt;
$password = hash("SHA256", $unhashed_password); 

$exec_mysql = mysql_query("SELECT * FROM players WHERE username='".$username."' AND password='".$password."'");

if (mysql_num_rows($exec_mysql)) == 1 {
    echo "execResult=login_accepted";
}

if (mysql_num_rows($exec_mysql)) == 0 {
    echo "execResult=login_rejected";
}

else {
    echo "execResult=error";
}

?>

I have compared my code with multiple sources, even copied whole projects from sources trying to figure out what I have done wrong, but with little success. However, there is something weird I found out. Out of pure experimentation, I put the following into controlpanel.php:

=&execResult=test

And, instead of getting "undefined", I got "test". The weird part is that I only used that one line of code; no <?php tags or anything. Moreover, if I put more code beyond that, regardless of what kind of code I entered, it would always show up literally in the Flash output. So I can now hardcode a value and return it into Flash, but that's not what I need. I need to be able to determine a value based on conditionals and have it automatically returned into Flash.

Can anyone help me out? I am truly stumped.

Thanks :)

Upvotes: 0

Views: 513

Answers (1)

Cram Pisam
Cram Pisam

Reputation: 120

Seems that you are executing flash from the editor/debugger, (if you are checking the results with trace function).

Then using a relative url for request "php/controlpanel.php" is like opening the file localy without the execution from server of php directives.

Maybe thats why you get literaly all php code, or the correct value when you only put "=&execResult=test" as the content of the php file.

Try using absolute url with the http:// in order to perform an http request where server and php execution are involved.

Upvotes: 2

Related Questions