php做游戏登录服务器,游戏登陆服务器php简单实现
本案例實(shí)現(xiàn)一個(gè)簡(jiǎn)單的登陸服務(wù)器。
步驟
步驟一、搭建LAMP環(huán)境,也就是 linux+apache+mysql+php,如果不習(xí)慣用linux可以在window下搭建web
服務(wù)器,具體的搭建方法可以在網(wǎng)上搜一下,很多相關(guān)的文章,在此不贅述。
步驟二、在mysql中創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)db_account,在db_account中創(chuàng)建數(shù)據(jù)表tbl_account
創(chuàng)建數(shù)據(jù)庫(kù)命令:create databases db_account;
創(chuàng)建表create table tbl_account(
id int not null primary key auto_increment,
username varchar(20) not null,
pwd varchar(20) not null);
步驟三、打開(kāi)瀏覽器,輸入網(wǎng)址,比如192.168.1.6/login.php?username=xiaoming&pwd=123,回車(chē),如
果數(shù)據(jù)庫(kù)中有這個(gè)用戶名,則返回該用戶名的id,如果沒(méi)有,則插入用戶名和密碼,然后返回
id.
代碼
login.php 文件
require_once(‘db_conn.php‘);
$db = new DBConnection();
$conn = $db->connect("localhost","root","12345678",‘db_account‘);
if(!$conn)
{
die(‘Could not connect: ‘);
}
else
{
$username = $_GET["username"];
$password = $_GET["pwd"];
if($username == ‘‘||$password==‘‘)
{
echo ‘please input username and password‘;
exit;
}
$result = mysql_query("select id from tbl_account where username=‘$username‘");
if(0 == mysql_num_rows($result))
{
//數(shù)據(jù)庫(kù)中沒(méi)有查到記錄,說(shuō)明是新用戶,向數(shù)據(jù)庫(kù)中加入該用戶
$ret = mysql_query("insert into tbl_account(username,pwd)value(‘$username‘, ‘$password‘)");
if(!$ret)
{
echo "Insert fail".mysql_error();
}
else
{
$result = mysql_query("select id from tbl_account where username=‘$username‘");
$row = mysql_fetch_assoc($result);
echo ‘{"response":"new user","id":‘ . $row[‘id‘] . ‘}‘;
}
}
else
{
//老用戶,返回id
$row = mysql_fetch_assoc($result);
echo ‘{"response":"welcome","id":‘ . $row[‘id‘] . ‘}‘;
}
}
db_con.php文件
class DBConnection
{
function connect($server,$username,$pwd,$db_name)
{
$conn = mysql_connect($server,$username,$pwd);
if(!$conn)
{
die(‘Could not connect: ‘.mysql_error());
}
else
{
mysql_query("SET NAMES UTF8");
mysql_query("set character_set_client=utf8");
mysql_query("set character_set_results=utf8");
mysql_select_db($db_name,$conn);
}
return $conn;
}
function close($conn)
{
mysql_close($conn);
}
}
從代碼中您應(yīng)該能看到,密碼其實(shí)沒(méi)有做判定,只是根據(jù)username來(lái)做判斷。
原文:http://chenshuhb.blog.51cto.com/6087203/1837539
總結(jié)
以上是生活随笔為你收集整理的php做游戏登录服务器,游戏登陆服务器php简单实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 河南大学计算机与信息工程学院张磊,张磊-
- 下一篇: php项目中sql,php – 大括号{