继续上篇的登录界面 这回加上数据库 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
$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{
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("没有填写用户名或密码");
<?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>
<!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>