FLEX PHP 交互 简单登录界面(2)连mySql 源代码

 

继续上篇的登录界面 这回加上数据库 mySql 

首先确定数据库和php 的正确连接 (1)设置php.ini 放在windows 下(2)把extension=php_mySql.dll前的;去掉

(3)把extension_dir="d:/php/ext"改在正确的路径

//开始连接数据库语句

$link = mysql_connect("localhost","root","1234") or die (mysql_error());

参数分别是 ip 用户名 密码

//然后选择数据库

$select=mysql_select_db("db_flex",$link);

Php代码

  1. <?php    
  2. $return="";     
  3. if(isset($_POST[username]) && isset($_POST[userpwd])){  
  4. $uname=$_POST[username];  
  5. $upwd=$_POST[userpwd];  
  6. $link = mysql_connect("localhost","root","1234") or die (mysql_error());  
  7. if($link){  
  8. $select=mysql_select_db("db_flex",$link);  
  9.     mysql_query("set names gb2312");  
  10. if($select)  
  11. $sql=mysql_query("select * from user_list where name='$uname'");//执行sql语句
  12. $result=mysql_fetch_object($sql);  
  13. if($result==false){//没有找到用户名
  14. $return='<users>';  
  15. $return.='<a>nohave</a>';  
  16. $return.='</users>';  
  17.     }else if($result->pwd==$upwd){//用户名正确,密码正确
  18. $return='<users>';  
  19. $return.='<a>ok</a>';  
  20. $return.='</users>';  
  21.     }  
  22. else{//用户名正确,密码错误
  23. $return='<users>';  
  24. $return.='<a>pwderror</a>';  
  25. $return.='</users>';  
  26.     }  
  27. mysql_close();  
  28.     } }  
  29. echo $return;  
  30. ?> 
<?php  
  $return="";   
  if(isset($_POST[username]) && isset($_POST[userpwd])){
  $uname=$_POST[username];
  $upwd=$_POST[userpwd];
  $link = mysql_connect("localhost","root","1234") or die (mysql_error());
  if($link){
    $select=mysql_select_db("db_flex",$link);
	mysql_query("set names gb2312");
	if($select)
	$sql=mysql_query("select * from user_list where name='$uname'");//执行sql语句
	$result=mysql_fetch_object($sql);
	if($result==false){//没有找到用户名
	$return='<users>';
	$return.='<a>nohave</a>';
	$return.='</users>';
	}else if($result->pwd==$upwd){//用户名正确,密码正确
	$return='<users>';
	$return.='<a>ok</a>';
	$return.='</users>';
	}
	else{//用户名正确,密码错误
	$return='<users>';
	$return.='<a>pwderror</a>';
	$return.='</users>';
	}
 
 mysql_close();
	} }
  echo $return;
?>

  flex 端的 httpService RESULT的返回的代码这么写:课参见上篇文章。

private function loginOk(event:ResultEvent):void{

Flex代码

  1. login_result=event.result.html.body.users.a.toString();//根据情况写event.result.后面的内容  
  2. if(login_result=="ok"){  
  3. if(user.text=="admin")  
  4. currentState="Administer";//管理员  
  5. else  
  6. currentState="User";//用户  
  7. }  
  8. if(login_result=="nohave"){  
  9. Alert.show("没有找到这个用户名 请先注册!");  
  10. }  
  11. if(login_result=="pwderror"){  
  12. Alert.show("密码错误! 请从新登陆!");  
  13. }  
   	     login_result=event.result.html.body.users.a.toString();//根据情况写event.result.后面的内容
   	     if(login_result=="ok"){
   	     if(user.text=="admin")
   	     currentState="Administer";//管理员
   	     else
   	     currentState="User";//用户
   	     }
         if(login_result=="nohave"){
          Alert.show("没有找到这个用户名 请先注册!");
         }
         if(login_result=="pwderror"){
          Alert.show("密码错误! 请从新登陆!");
         }
   }

数据库方面是这么设计的

一个表 name  char  8    主键

           pwd    char   8   

增加了注册功能,并在一个页面中请求,关键的代码。

1,request 变量变为三个 增加的变量用于选择,用于判断是登陆还是注册。

<mx:username>
      {username.text}
     </mx:username>
     <mx:userpwd>
      {userpwd.text}
     </mx:userpwd>
     <mx:sendchoice>
      {sendChoice}
     </mx:sendchoice>

2.关于textinput 的方法<mx:TextInput x="92" y="95" id="userpwd" fontSize="12" displayAsPassword="true" maxChars="8" restrict="0-9,a-z"/>

fontSize=12 用12号字 使中文的宋体字,显示的清晰一些。

   displayAsPassword 把输入框的字符显示成密码的形式(****)

   maxChars  是最大输入的字符的个数

   reStrict 为限制的字符类型 0-9 a-z A-Z

3.加强了对用户密码的检测,使输入数据更安全

  if(username.text=="" || userpwd.text=="")
   Alert.show("没有填写用户名或密码");

Flex代码

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">  
  3. <mx:Script>  
  4.     <![CDATA[  
  5.         import mx.collections.ArrayCollection;  
  6.         import mx.rpc.events.ResultEvent;  
  7.         import mx.controls.Alert;  
  8.         [Bindable]  
  9.         private var result1:ArrayCollection;  
  10.         private var login_result:String;  
  11.         [Bindable]  
  12.         private var sendChoice:String;  
  13.         private function goLogin():void{  
  14.             if(username.text=="" || userpwd.text=="")  
  15.             Alert.show("没有填写用户名或密码");  
  16.             else{  
  17.             sendChoice="login";  
  18.             login.send();  
  19.             }  
  20.         }  
  21.         private function goRegis():void{  
  22.             if(username.text=="" || userpwd.text=="")  
  23.             Alert.show("没有填写用户名或密码");  
  24.             else{  
  25.             sendChoice="regis";  
  26.             login.send();  
  27.             }         
  28.         }  
  29.         private function resultHandler(event:ResultEvent):void{  
  30.          login_result=event.result.html.body.users.a.toString();  
  31.          if(login_result=="ok"){  
  32.           Alert.show("欢迎,登录成功");  
  33.          }  
  34.          if(login_result=="nohave"){  
  35.           Alert.show("没有找到这个用户名 请先注册!");  
  36.          }  
  37.          if(login_result=="pwderror"){  
  38.           Alert.show("密码错误! 请从新登陆!");  
  39.          }  
  40.          if(login_result=="nameishave"){  
  41.           Alert.show("用户名已经存在,请选择别的用户名注册");  
  42.          }  
  43.          if(login_result=="regisok"){  
  44.           Alert.show("注册成功,请从新登陆");  
  45.          }  
  46.          if(login_result=="error"){  
  47.           Alert.show("错误");  
  48.          }  
  49.         }  
  50.     ]]>  
  51. </mx:Script>  
  52.     <mx:HTTPService id="login" method="POST" showBusyCursor="true" url="http://localhost/flexlogin.php"
  53.         result="resultHandler(event)">  
  54.     <mx:request xmlns="">  
  55.         <mx:username>  
  56.             {username.text}  
  57.         </mx:username>  
  58.         <mx:userpwd>  
  59.             {userpwd.text}  
  60.         </mx:userpwd>  
  61.         <mx:sendchoice>  
  62.             {sendChoice}  
  63.         </mx:sendchoice>  
  64.     </mx:request>  
  65.     </mx:HTTPService>  
  66.     <mx:Panel width="310" height="265" layout="absolute" title="登录" fontSize="12" fontWeight="normal">  
  67.         <mx:TextInput x="93" y="51" id="username" fontSize="12" restrict="0-9,a-z" maxChars="8"/>  
  68.         <mx:TextInput x="92" y="95" id="userpwd" fontSize="12" displayAsPassword="true" maxChars="8" restrict="0-9,a-z"/>  
  69.         <mx:Button x="78" y="154" label="登录" id="btn1" click="goLogin()" fontWeight="normal" fontSize="12"/>  
  70.         <mx:Label x="32" y="53" text="用户名:" fontSize="12"/>  
  71.         <mx:Label x="43" y="97" text="密码:" fontSize="12"/>  
  72.         <mx:Button x="154" y="154" label="注册" fontSize="12" fontWeight="normal" id="btn2" click="goRegis()"/>  
  73.         <mx:Label x="10" y="10" text="测试用 用户名 user 密码 1234" fontSize="12" width="243"/>  
  74.     </mx:Panel>  
  75. </mx:Application> 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
	<![CDATA[
		import mx.collections.ArrayCollection;
		import mx.rpc.events.ResultEvent;
		import mx.controls.Alert;
		[Bindable]
		private var result1:ArrayCollection;
		private var login_result:String;
		[Bindable]
		private var sendChoice:String;
		private function goLogin():void{
			if(username.text=="" || userpwd.text=="")
			Alert.show("没有填写用户名或密码");
			else{
			sendChoice="login";
			login.send();
	     	}
		}
		private function goRegis():void{
			if(username.text=="" || userpwd.text=="")
			Alert.show("没有填写用户名或密码");
			else{
			sendChoice="regis";
			login.send();
			}		
		}
		
		private function resultHandler(event:ResultEvent):void{
   	     login_result=event.result.html.body.users.a.toString();
   	     if(login_result=="ok"){
          Alert.show("欢迎,登录成功");
   	     }
         if(login_result=="nohave"){
          Alert.show("没有找到这个用户名 请先注册!");
         }
         if(login_result=="pwderror"){
          Alert.show("密码错误! 请从新登陆!");
         }
         if(login_result=="nameishave"){
          Alert.show("用户名已经存在,请选择别的用户名注册");
         }
         if(login_result=="regisok"){
          Alert.show("注册成功,请从新登陆");
         }
         if(login_result=="error"){
          Alert.show("错误");
         }
		}
	]]>
</mx:Script>
    <mx:HTTPService id="login" method="POST" showBusyCursor="true" url="http://localhost/flexlogin.php" 
    	result="resultHandler(event)">
    <mx:request xmlns="">
    	<mx:username>
    		{username.text}
    	</mx:username>
    	<mx:userpwd>
    		{userpwd.text}
    	</mx:userpwd>
    	<mx:sendchoice>
    		{sendChoice}
    	</mx:sendchoice>
    </mx:request>
    </mx:HTTPService>
	<mx:Panel width="310" height="265" layout="absolute" title="登录" fontSize="12" fontWeight="normal">
		<mx:TextInput x="93" y="51" id="username" fontSize="12" restrict="0-9,a-z" maxChars="8"/>
		<mx:TextInput x="92" y="95" id="userpwd" fontSize="12" displayAsPassword="true" maxChars="8" restrict="0-9,a-z"/>
		<mx:Button x="78" y="154" label="登录" id="btn1" click="goLogin()" fontWeight="normal" fontSize="12"/>
		<mx:Label x="32" y="53" text="用户名:" fontSize="12"/>
		<mx:Label x="43" y="97" text="密码:" fontSize="12"/>
		<mx:Button x="154" y="154" label="注册" fontSize="12" fontWeight="normal" id="btn2" click="goRegis()"/>
		<mx:Label x="10" y="10" text="测试用 用户名 user 密码 1234" fontSize="12" width="243"/>
	</mx:Panel>
</mx:Application>

Php代码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>flex login</title>  
  6. </head>  
  7. <body>  
  8. <?php    
  9. $return="";     
  10. if(isset($_POST[username]) && isset($_POST[userpwd]) && isset($_POST[sendchoice])){  
  11. $uname=$_POST[username];  
  12. $upwd=$_POST[userpwd];  
  13. $choice=$_POST[sendchoice];  
  14. $link = mysql_connect("localhost","root","1234") or die (mysql_error());  
  15. if($link){  
  16. $select=mysql_select_db("db_flex",$link);  
  17.     mysql_query("set names gb2312");  
  18. if($choice=="login"){  
  19. $sql=mysql_query("select * from user_list where name='$uname'");  
  20. $result=mysql_fetch_object($sql);  
  21. if($result==false){  
  22. $return='<users>';  
  23. $return.='<a>nohave</a>';  
  24. $return.='</users>';  
  25.          }else if($result->pwd==$upwd){  
  26. $return='<users>';  
  27. $return.='<a>ok</a>';  
  28. $return.='</users>';  
  29.          }else{  
  30. $return='<users>';  
  31. $return.='<a>pwderror</a>';  
  32. $return.='</users>';  
  33.          }  
  34.     }  
  35. if($choice=="regis"){  
  36. $sql=mysql_query("select * from user_list where name='$uname'");  
  37. $result=mysql_fetch_object($sql);  
  38. if($result==false){  
  39. $sql_1=mysql_query("insert into user_list(name,pwd) values('$uname','$upwd')",$link);  
  40. if($sql_1){  
  41. $return='<users>';  
  42. $return.='<a>regisok</a>';  
  43. $return.='</users>';  
  44.            }  
  45.          }else{  
  46. $return='<users>';  
  47. $return.='<a>nameishave</a>';  
  48. $return.='</users>';  
  49.          }  
  50.     }  
  51. }  
  52. mysql_close();  
  53. } else{  
  54. $return='<users>';  
  55. $return.='<a>error</a>';  
  56. $return.='</users>';  
  57. }  
  58. echo $return;  
  59. ?>  
  60. </body>  
  61. </html> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>flex login</title>
</head>

<body>
<?php  
  $return="";   
  if(isset($_POST[username]) && isset($_POST[userpwd]) && isset($_POST[sendchoice])){
  $uname=$_POST[username];
  $upwd=$_POST[userpwd];
  $choice=$_POST[sendchoice];
  $link = mysql_connect("localhost","root","1234") or die (mysql_error());
  if($link){
    $select=mysql_select_db("db_flex",$link);
	mysql_query("set names gb2312");
	if($choice=="login"){
	  $sql=mysql_query("select * from user_list where name='$uname'");
	  $result=mysql_fetch_object($sql);
	     if($result==false){
	     $return='<users>';
	     $return.='<a>nohave</a>';
	     $return.='</users>';
	     }else if($result->pwd==$upwd){
	     $return='<users>';
	     $return.='<a>ok</a>';
	     $return.='</users>';
	     }else{
	     $return='<users>';
	     $return.='<a>pwderror</a>';
	     $return.='</users>';
	     }
    }
	if($choice=="regis"){
	  $sql=mysql_query("select * from user_list where name='$uname'");
	  $result=mysql_fetch_object($sql);
	     if($result==false){
		   $sql_1=mysql_query("insert into user_list(name,pwd) values('$uname','$upwd')",$link);
		   if($sql_1){
	         $return='<users>';
	         $return.='<a>regisok</a>';
	         $return.='</users>';
		   }
	     }else{
	       $return='<users>';
	       $return.='<a>nameishave</a>';
	       $return.='</users>';
	     }
	}
 }
 mysql_close();
} else{
$return='<users>';
$return.='<a>error</a>';
$return.='</users>';
}

  echo $return;
?>
</body>
</html>
posted @ 2010-01-14 08:56  桀歌  阅读(1019)  评论(1)    收藏  举报