lundi 13 mars 2017

AS3 not recieving myStatus from php login page

I am not receiving the myStatus when retrieving it from the php login. I get p_id and full name but not the status. I am parsing it to separate out the & but keep getting a null result from login. section 6 and section 7 are the areas I think I am having issues can anyone help?

login AS3

    package  {

import flash.display.MovieClip;
import flash.text.*;
import flash.events.*;
import flash.ui.Keyboard;
import flash.net.*;
import MainDocument;


public class Login extends MovieClip {


    public function Login() {
        // constructor code

        // modify existing text boxes
        login_txt.tabEnabled = true;
        login_txt.tabIndex = 1;
        login_txt.border = true;
        login_txt.borderColor = 0xAAAAAA;
        login_txt.background = true;
        login_txt.backgroundColor = 0xFFFFDD;

        pwd_txt.tabEnabled = true;
        pwd_txt.tabIndex = 2;
        pwd_txt.border = true;
        pwd_txt.borderColor = 0xAAAAAA;
        pwd_txt.background = true;
        pwd_txt.backgroundColor = 0xFFFFDD;

        login_btn.tabEnabled = true;
        login_btn.tabIndex = 3;

        //add button event listeners

        login_btn.addEventListener(MouseEvent.MOUSE_UP, doLogin);
        login_close_btn.addEventListener(MouseEvent.MOUSE_UP, doClose);
        addEventListener(KeyboardEvent.KEY_DOWN, onEnter);
    } // end construtor

            private function onEnter(e:KeyboardEvent):void{
        // nothing yet

        if(e.keyCode == Keyboard.ENTER){
            trace("User presses keyboard ENTER key");
            doLogin(null); // must be null to meet the need for a parameter
        }// end if
    }// end function

     // SECTION 1 – This is a function we need for removing extra whitespace coming from CWP server 
function trimWhitespace($string:String):String {
if ($string == null) {
    return "";
} //end if
return $string.replace(/^\s+|\s+$/g, ""); // see regular expressions
} // end function          

private function doLogin(e:MouseEvent):void{            
//trace("User presses OK button");

// SECTION 2 – URL request variable declared with path to server script
var req:URLRequest=new URLRequest(MainDocument.localPath + "login.php");
req.method = URLRequestMethod.POST; 

// SECTION 3 – Upload variables are named as URL variables
var vars:URLVariables=new URLVariables();
vars.login = login_txt.text;
vars.pwd = pwd_txt.text;
req.data = vars;    

// SECTION 4 – The URL Loader class instance is set to perform the HTTP                                                                                                                             POST
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;  
loader.load(req);

// SECTION 5 – listeners are added for both the HTTP status and COMPLETE    
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,onHTTPStatus); 
loader.addEventListener(Event.COMPLETE, retrieveData);

// SECTION 6 – The complete handler function is embedded within the doLogin()                                                                                                                                                                                                  function
function retrieveData(e:Event):void { 
    // For the CWP server we must parse our own incoming data
    var rawString:String = trimWhitespace(unescape(e.target.data));     
    var stArray:Array = rawString.split("&"); //split on ampersand  character
    trace("retrieve data");
    for(var i:uint; i<stArray.length; i++){
        stArray[i] = trimWhitespace(stArray[i]);
        var pair:Array = stArray[i].split("=");
        pair[0] = trimWhitespace(pair[0]);
        switch (pair[0]){
            case "myStatus":
                var ms:String = trimWhitespace(pair[1]);    //  status data value
                break;
            case "p_id":
                var id:int = int(trimWhitespace(pair[1]));  // player id
                break;
            case "fullname":
                var fn:String = trimWhitespace(pair[1]);    // fullname
                break;
            default: // this is all the other junk---> dump
                //do nothing
        } // end switch
    } //end for 

// SECTION 7 – A switch statement to deal with cases of myStatus
    switch (ms){
        case "NOTOK" :
            MainDocument.doc.showMsg("There is a communication problem\nTry Again!");
            break;
        case "OK" :
            if(fn != "INVALID"){                            
                MainDocument.doc.showMsg("Welcome " + fn);                      
                //MainDocument.player_id = id;
                //MainDocument.player_name = fn;
                MainDocument.doc.removeLogin();
                MainDocument.doc.turnOffButton("both");                         
                //MainDocument.doc.enable_chat_button();
                //MainDocument.doc.activateConsole();
                ////////////////////////
                // start game here
                ////////////////////////
            }else{
                MainDocument.doc.showMsg("Login or password is incorrect. Try again, or \nif you are not a member,  please register");
                MainDocument.doc.turnOnButton("register");
                MainDocument.doc.turnOffButton("login");                            
            }
            break;
        default:
            //MainDocument.doc.showMsg("An unknown problem has occured");
    } // end switch             
} // end function   

// SECTION 8 – Handler function for HTTP status – also embedded             
 function onHTTPStatus(event:HTTPStatusEvent):void { 
    //trace("HTTP response code " + event.status); 
    if(event.status!=200){
        MainDocument.doc.showMsg("There is an I/O Error #" + event.status);
    } // end if
} // end function               
} // end function doLogin

    private function doClose(e:MouseEvent):void{
        // nothing yet
        trace("User presses Close button");

        MainDocument.doc.showMsg(""); // clears any message
        MainDocument.doc.removeLogin();
        MainDocument.doc.turnOnButton("both");
    }// end function


   }//end class

 }// end package

login.php

    <?php


 require_once("settings.inc.php");




 // Create vars and load with incoming POST data 


 $login = $_POST['login'];

 if(!isset($login)){

 $login = $_GET['login'];

 }

 $pwd = $_POST['pwd'];

 if(!isset($pwd)){

 $pwd = $_GET['pwd'];

 } 

//=========================================================================================

// MAIN

//=========================================================================================

 if(!(isset($login) && isset($pwd))){

 print "myStatus=NOTOK&dummy=dummy";

 }else{

$query = "SELECT `p_id`,`p_name` FROM `player` WHERE `p_login`='$login' AND `p_pwd`='$pwd'";
$result = mysqli_query($link,$query);
$row = mysqli_fetch_row($result);

if($row[0] != null && $row[0] != ''){

    $p_id = $row[0];
    $fullname =  trim($row[1]);
    $myStatus = $row[2];
    print "myStatus=OK&p_id=" . trim($p_id) . "&fullname=" . trim($fullname) . "&dummy=dummy";      

}else{

    $fullname = "INVALID";  
    print "myStatus=OK&fullname=" . trim($fullname) . "&dummy=dummy";       

} //end else     

 } //end else               

 ?>

Aucun commentaire:

Enregistrer un commentaire