- 最后登录
- 2017-5-15
- 注册时间
- 2012-3-1
- 阅读权限
- 90
- 积分
- 32973
- 纳金币
- 32806
- 精华
- 12
|
1,创建一个数据库表,使用MySql数据库.创建代码如下:
CREATE TABLE `scores` ( `id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 30 ) NOT NULL ,
`password` VARCHAR( 50 ) NOT NULL
) ENGINE = innodb;
复制代码
2,向数据库表中插入内容:
INSERT INTO `scores` ( `id` , `name` , `password` )
VALUES (
NULL , 'haiweisky', MD5( 'aaaaaa' )
);
复制代码
3,创建一个验证用户基本信息的PHP页面,页面名字是:check_scores.php
<?
// 联接数据库
$host = "localhost"; //主机名
$user = "root"; //用户名
$password = ""; //密码
$dbname = "unity3dTest"; //数据库名称
mysql_connect($host, $user, $password) or die("Cant connect into database");
mysql_select_db($dbname)or die("Cant connect into database");
// =============================================================================
// PROTECT AGAINST SQL INJECTION and CONVERT PASSWORD INTO MD5 formats
function anti_injection_login_senha($sql, $formUse = ***e)
{
$sql = preg_replace("/(from|select|insert|delete|where|drop table|show tables|,|'|#|*|--|\)/i","",$sql);
$sql = trim($sql);
$sql = strip_tags($sql);
if(!$formUse || !get_magic_quotes_gpc())
$sql = addslashes($sql);
$sql = md5(trim($sql));
return $sql;
}
// THIS ONE IS JUST FOR THE NICKNAME PROTECTION AGAINST SQL INJECTION
function anti_injection_login($sql, $formUse = ***e)
{
$sql = preg_replace("/(from|select|insert|delete|where|drop table|show tables|,|'|#|*|--|\)/i","",$sql);
$sql = trim($sql);
$sql = strip_tags($sql);
if(!$formUse || !get_magic_quotes_gpc())
$sql = addslashes($sql);
return $sql;
}
// =============================================================================
$unityHash = anti_injection_login($_POST["myform_hash"]);
$phpHash = "hashcode"; // same code in here as in your Unity game
$nick = anti_injection_login($_POST["myform_nick"]); //I use that function to protect against SQL injection
$pass = anti_injection_login_senha($_POST["myform_pass"]);
/*
you can also use this
$nick = $_POST["myform:_nick"];
$pass = $_POST["myform_pass"];
*/
if(!$nick || !$pass) {
echo "npe";//昵称或密码不能为空。
} else {
if ($unityHash != $phpHash){
echo "HASH code is diferent from your game, you infidel.";
} else {
$SQL = "SELECT * FROM scores WHERE name = '" . $nick . "'";
$result_id = @mysql_query($SQL) or die("DATABASE ERROR!");
$total = mysql_num_rows($result_id);
if($total) {
$datas = @mysql_fetch_array($result_id);
if(!strcmp($pass, $datas["password"])) {
//echo "LOGADO - PASSWORD CORRECT";
echo "success";
} else {
echo "npw"; //昵称或密码错误
}
} else {
echo "nfn";//没此用户
}
}
}
// Close mySQL Connection
mysql_close();
?>
复制代码
4,新建一个Unity 3D项目,命名为myGame;新建一个Javascript文件,命名为phpUnity(点击Project面板上的Create按钮,点击Javascript,产生一个新的javascript,按F2为其命名为phpUnity).
在phpUnity文件上编写如下代码:
private var formNick = ""; //登录名
private var formPassword = ""; //密码
var formText = ""; //根据判断显示信息
var yyyskin:GUISkin;
var URL = "http://localhost/unity_test/check_scores.php"; //提交的URL地址
var hash = "hashcode"; //change your secret code, and remember to change into the PHP file too
private var textrect = Rect (10, 150, 500, 100); //just make a GUI object rectangle
function OnGUI() {
if(yyyskin){
GUI.skin = yyyskin;
GUI.Label( Rect (10, 10, 80, 20), "姓名:"); //text with your nick
GUI.Label ( Rect (10, 30, 80, 20), "密码:" );
formNick = GUI.TextField ( Rect (90, 10, 100, 20), formNick ); //here you will insert the new value to variable formNick
formPassword = GUI.PasswordField ( Rect (90, 30, 100, 20), formPassword ,"*"[0], 25);
//same as above, but for password
if ( GUI.Button ( Rect (10, 60, 100, 20) , "登录" ) ){ //just a button
Login();
}
GUI.TextArea( textrect, formText );
}
}
function Login() {
var form = new WWWForm(); //创建一个WWWForm对象。
form.AddField( "myform_hash", hash ); //add your hash code to the field myform_hash, check that this variable name is the same as in PHP file
form.AddField( "myform_nick", formNick );
form.AddField( "myform_pass", formPassword );
var w = WWW(URL, form); //here we create a var called 'w' and we sync with our URL and the form
yield w; //we wait for the form to check the PHP file, so our game dont just hang
if (w.error != null) {
print(w.error); //if there is an error, tell us
} else {
var return_data=w.data;
if(return_data=="success"){
Application.ExternalEval("window.location.href='game.php?username="+formNick+"'");
}else if(return_data=="npe"){
print("Test ok");
formText = "用户名或密码不能为空!"; //here we return the data our PHP told us
}else if(return_data=="nfn"){
formText="用户名不存在!";
}else if(return_data=="npw"){
formText="用户名或密码不正确!";
}
w.Dispose(); //clear our form in game
}
formNick = ""; //just clean our variables
formPassword = "";
}
复制代码
5. 此代码放在主相机内的游戏对象上。
原文地址:http://unity3d8.com/content/unit ... 2%E6%A1%88%E4%BE%8B |
|