遊戲登陸服務器php簡單實現

本案例實現一個簡單的登陸服務器。


步驟

步驟一、搭建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來做判斷。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章