FLEX ile LOGIN

4 Ekim 2008

FLEX ile LOGIN 4

Buradaki açıklamalar http://www.tutorialized.com/tutorials/Adobe-Flex/1 adresindedir

Bölüm 3 - PHP sunucu kodlaması

PHP kodlaması çok temeldir. Veri tabanıyla kullanıcı kodunu karşılaştırır ve sonucu XML olarak döndürür.

PHP Kodlaması:
[php]
<?php

define( "DATABASE_SERVER", "localhost" );
define( "DATABASE_USERNAME", "user" );
define( "DATABASE_PASSWORD", "pass" );
define( "DATABASE_NAME", "flex" );

//connect to the database

$mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD) or die(mysql_error());

//select the database

mysql_select_db( DATABASE_NAME );

//asign the data passed from Flex to variables

$username = mysql_real_escape_string($_POST["username"]);

$password = mysql_real_escape_string($_POST["password"]);

//Query the database to see if the given username/password combination is valid.

$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";

$result = mysql_fetch_array(mysql_query($query));

//start outputting the XML

$output = "<loginsuccess>";

//if the query returned true, the output <loginsuccess>yes</loginsuccess> else output <loginsuccess>no</loginsuccess>

if(!$result) {
    $output .= "no";		
}else{
    $output .= "yes";	
}

$output .= "</loginsuccess<";

//output all the XML

print ($output);

?>[/php]
AÇıklama:

PHP oldukça temel sayfalardan oluşur. Kodlamaya açıklama yazılmış olduğundan anlaşılabilir.

MySQL veri tabanına bağlantı kurulduktan sonra veri tabanından kullanıcı kodu veşifresinin doğruluğunu kontrol ederiz. Eğer doğruysa XML olarak (<loginsuccess>yes</loginsuccess>) çıktı hazırlar ve Flex onu okuyabilir.

Daha önce bu anlatımda aşağıdaki kodlama yapılmıştı:

Kodlama:
if(login_user.lastResult.loginsuccess == "yes") {
    currentState = "Logged In";
} 
lastResult.loginsuccess aslında PHP çıktısı olan <loginsuccess> değerini okur. Yani <loginsuccess> yerine <userLogin> yazınca kodlamada lastResult.loginsuccess yerine lastResult.userLogin kullanmak gerekir.