您的位置:首页 > 编程语言 > PHP开发

游戏登陆服务器php简单实现

2016-08-13 12:25 627 查看
本案例实现一个简单的登陆服务器。

步骤
步骤一、搭建LAMP环境,也就是 linux+apache+mysql+php,如果不习惯用linux可以在window下搭建web
服务器,具体的搭建方法可以在网上搜一下,很多相关的文章,在此不赘述。
步骤二、在mysql中创建一个数据库db_account,在db_account中创建数据表tbl_account
创建数据库命令:create databases db_account;

创建表create table tbl_account(
id int not null primary key auto_increment,

username varchar(20) not null,

pwd varchar(20) not null);

步骤三、打开浏览器,输入网址,比如192.168.1.6/login.php?username=xiaoming&pwd=123,回车,如
果数据库中有这个用户名,则返回该用户名的id,如果没有,则插入用户名和密码,然后返回
id.

代码

login.php 文件

<?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))
{
//数据库中没有查到记录,说明是新用户,向数据库中加入该用户
$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文件

<?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);
}
}

从代码中您应该能看到,密码其实没有做判定,只是根据username来做判断。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  服务器 游戏 登陆