Home » ActionScript 3.0, Adobe, Flex 3, PHP

FLEX 3 e PHP (parte 3): caricare e inviare variabili utilizzando la classe URLLoader per simulare un’operazione di login

14 January 2009 One Comment

In questo tutorial sfrutteremo le nuove classi URLRequest e URLVariables fornite da ActionScript 3.0 per passare le variabili al file PHP con un metodo più elegante e strutturato.

Le operazioni che effettueremo saranno le seguenti:
- FLEX: Invio delle variabili username e password da Flex a PHP
- PHP: analisi del contenuto delle variabili per verificare la corrispondenza dei dati acquisiti. Nel caso i dati fossero corretti verrà inviata a Flex una variabile status per indicare se l’utente ha effettuato il login con successo o meno
- FLEX: Visualizzazione del risultato dell’operazione in una TextArea.

Effettueremo inoltre il monitoraggio del caricamento dei dati acquisiti da PHP, in modo da creare un eventuale preloader che, in questo caso risulterà inutile visto che i dati acquisiti saranno di pochi byte, ma potrebbe risultare utile nel caso di una grosse quantità di dati.

Di seguito il codice MXML commentato passo-passo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
	xmlns:mx="http://www.adobe.com/2006/mxml" 
	layout="absolute"
	creationComplete="init(event)" >
 
	<mx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			import flash.net.URLLoader;
			import flash.net.URLRequest;
 
			private var loader:URLLoader = new URLLoader();
 
			private const USERNAME:String = "Fabio";
			private const PASSWORD:String = "Biondi";
 
 
			private function init(evt:FlexEvent):void
			{
				// Path del file PHP
				var request:URLRequest = new URLRequest("assets/checkLogin.php");
 
				// Variabili da inviare
				var params:URLVariables = new URLVariables();
				params.username = USERNAME
				params.password = PASSWORD
				request.data = params;
 
				// Listener per monitorare la percentuale di dati caricati
				loader.addEventListener(ProgressEvent.PROGRESS, onProgress);
 
				// Listener per monitorare se il caricamento è stato completato
				loader.addEventListener(Event.COMPLETE, onComplete);
 
				// Caricamento del file PHP e invio delle variabili
				loader.load(request);
			}
 
 
			private function onProgress(e:ProgressEvent):void
			{
				// Visualizzazione percentuale dati caricati
				var percLoaded:Number = int((e.bytesLoaded / e.bytesTotal)*100)
				outputArea_txt.text += "Loaded: " + percLoaded + "%n";
			}
 
 
			private function onComplete(e:Event):void
			{
				// Recupero dalla variabile 'status' inviata da PHP 
				var txtVars:URLVariables = new URLVariables(e.target.data);
 
				// Visualizzazione 'status'
				outputArea_txt.text += txtVars.status;
 
			}
 
		]]>
	</mx:Script>
 
 
 
 
 
	<mx:TextArea width="300" height="60"
		id="outputArea_txt" y="20"/>
 
 
</mx:Application>

Il codice PHP (contenuto in assets/checkLogin.php):

1
2
3
4
5
6
7
<?php
 
    if ($_REQUEST['username'] == "Fabio" && $_REQUEST['password'] == "Biondi")
        echo "status=User Logged";
    else
        echo "status=Wrong User/Pass";
?>

Download Source Code

One Comment »

  • R. said:

    Thank you, working fine w. EasyPHP on localhost.

    Illes,
    Hungary

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.