在一个星期前PHP已经完工了这个程序一直没有时间写博客。今天把这个程序放上来,在网上用PHP实现的socket也不多。所以放上自己写的程序,希望能给需要的人提供一些帮助。自学PHP程序没多久若有漏洞与不足请多多谅解!
功能:主要是提供登录功能,成绩查询功能,登出功能三种
程序主体:分三大块
•Index.php(登录界面)
•Client.php (客户界面)
+request.php(函数)
•Server.php (服务器界面)
+response.php (函数)
+conn.php(数据库连接)
用的是TCP, 由于PHP本身不支持多线程所以只实现了客户服务器一对一。服务端由一个变量记录了当前用户。
通信过程:
在客户端请求通信时现发送一个字符串至服务端,服务端有字符串得知需要提供的服务并转入相应的分支。
具体代码如下:
1 <?php 2 //index.php 3 //write for 张小豪 4 if (isset($_COOKIE["user"]) && $_COOKIE["user"]){ 5 ?> 6 <script> 7 location.href="../socket/client.php?user=<?php echo $_COOKIE["user"]; ?>"; 8 </script> 9 <?php 10 } 11 ?> 12 <html> 13 <meta charset="UTF-8"> 14 <head> 15 <title>登陆界面</title> 16 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 17 </head> 18 19 </body> 20 <SCRIPT language=javascript> 21 22 function CheckPost() 23 { 24 //var username=document.getElementById("username"); 25 //alert(username.innerHTML); 26 if(submitpage.username.value=="") 27 { 28 alert("请填写用户名!"); 29 //submit.user.focus(); 30 return false; 31 } 32 if(submitpage.password.value=="") 33 { 34 alert("请填写密码!"); 35 //submit.user.focus(); 36 return false; 37 } 38 } 39 40 </SCRIPT> 41 42 <h3><center>客户端登录</center></h3> 43 <form action = "index.php" name = "submitpage" method = "post" onsubmit="return CheckPost();"> 44 <center> 45 <p>用户名:<input type = "text" id="username" name = "username" size = "20"></p> 46 <p>密码: <input type = "password" name = "password" size = "20"></p> 47 <input type = "submit" name = "submit" value = "提交"> 48 </center> 49 </form> 50 <?php 51 set_time_limit(0); 52 53 $host = "127.0.0.1"; 54 $port = 8890; 55 if(@$_POST['submit']){ 56 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)or die("Could not create socket "); // 创建一个Socket 57 58 $connection = socket_connect($socket, $host, $port) or die("Could not connet server "); // 连接 59 60 socket_write($socket, "login ") or die("Write failed "); //通知服务器已经登陆 61 socket_write($socket, $_POST['username']) or die("Write failed "); // 将用户名发送到服务器 62 63 if ($buffer = socket_read($socket, 1024, PHP_NORMAL_READ)) { 64 //echo("Username sent was: time Password was:" . $buffer . " "); 65 } 66 socket_close($socket); 67 if(strcmp(trim($buffer),"busy")==0){ 68 ?> 69 <script> 70 alert("服务器忙 "); 71 </script> 72 <?php 73 } 74 else if(strcmp(trim($buffer),"no username")==0){ 75 ?> 76 <script> 77 alert("用户名不存在 "); 78 </script> 79 <?php 80 //echo "用户名不存在 "; 81 }else{ 82 $buffer = trim($buffer); 83 //echo $buffer."<br>"; 84 //echo md5($_POST['password'])."<br>"; 85 if(strcmp($buffer, md5($_POST['password']))==0){ 86 //echo "登录成功!"; 87 ?> 88 <script> 89 alert("登录成功!! "); 90 location.href="../socket/client.php?user=<?php echo $_POST['username']; ?>"; 91 </script> 92 <?php 93 } 94 else{ 95 ?> 96 <script> 97 alert("密码错误! "); 98 </script> 99 <?php 100 //echo "密码错误!"; 101 } 102 } 103 } 104 105 ?> 106 107 108 </body> 109 </html>
1 <?php 2 //client.php 3 //write for 张小豪 4 include("request.php"); 5 set_time_limit(0); 6 7 $host = "127.0.0.1"; 8 $port = 8890; 9 10 11 if(@$_POST['submit']){ 12 request_submit($host, $port); 13 } 14 if(@$_POST['logout']){ 15 request_logout($host, $port); 16 setcookie("user"); 17 ?> 18 <script> 19 location.href="../socket/index.php"; 20 </script> 21 <?php 22 } 23 24 if(isset($_GET['user'])) 25 setcookie("user", $_GET['user'], time()+3600); 26 else{ 27 ?> 28 <script> 29 alert("请先登录!"); 30 location.href="../socket/index.php"; 31 </script> 32 <?php 33 } 34 35 ?> 36 37 <html> 38 <head> 39 <title>socket客户端</title> 40 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 41 </head> 42 43 <body> 44 45 <?php 46 //echo $_user = $_GET['user']?$_GET['user']:$_COOKIE['user']; 47 //print_r($_COOKIE); 48 //echo "user:".$_GET['user']; 49 ?> 50 51 <SCRIPT language=javascript> 52 <!-- 53 function CheckPost() 54 { 55 //alert(submitpage.getElementById('logout').onclick); 56 if(submitpage.username.value=="" && submitpage.logout.onclick==false) 57 { 58 alert("请填写学号!"); 59 //submit.username.focus(); 60 return false; 61 } 62 } 63 --> 64 </SCRIPT> 65 66 <h3><center>socket客户端程序(q版成绩查询~.~)</center></h3> 67 <font align="left">欢迎 68 <?php 69 if(isset($_COOKIE['user']) && $_COOKIE['user']) 70 $str = $_COOKIE['user']; 71 else 72 $str = $_GET['user']; 73 echo $str; 74 ?> 75 !! 76 </font> 77 <form action = "client.php" name = "submitpage" method = "post" onsubmit="return CheckPost();"> 78 <input type = "submit" name = "logout" value = "登出"></input> 79 <br><br> 80 81 <center> 82 <p>请输入学号:<input type = "text" name = "username" size = "20"></input></p> 83 <input type = "submit" name = "submit" value = "提交"></input> 84 </center> 85 </form> 86 87 </center> 88 89 </body> 90 </html>
1 <?php 2 //request for client 3 //request.php 4 //write for 张小豪 5 function request_submit($host, $port){ 6 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)or die("Could not create socket "); // 创建一个Socket 7 $connection = socket_connect($socket, $host, $port) or die("Could not connet server "); // 连接 8 socket_write($socket, "user ") or die("Write failed "); //通知服务器已经登陆 9 if(isset($_COOKIE['user']) && $_COOKIE['user']) 10 $str = $_COOKIE['user']; 11 else 12 $str = $_GET['user']; 13 socket_write($socket, $str." ") or die("Write failed "); // 将用户名发送到服务器*/ 14 socket_write($socket, $_POST['username']) or die("Write failed "); // 将用户名发送到服务器 15 16 if ($buffer = socket_read($socket, 1024, PHP_NORMAL_READ)) { 17 ?> 18 <script> 19 var str = "The student ID: <?php echo $_POST['username']; ?> GPA was:<?php echo trim($buffer); ?> "; 20 alert(str); 21 location.href="../socket/client.php?user=<?php echo $_COOKIE['user']; ?>"; 22 </script> 23 <?php 24 } 25 socket_close($socket); 26 } 27 28 function request_logout($host, $port){ 29 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)or die("Could not create socket "); // 创建一个Socket 30 $connection = socket_connect($socket, $host, $port) or die("Could not connet server "); // 连接 31 socket_write($socket, "logout ") or die("Write failed "); //通知服务器已经登陆 32 if(isset($_COOKIE['user']) && $_COOKIE['user']) 33 $str = $_COOKIE['user']; 34 else 35 $str = $_GET['user']; 36 socket_write($socket, $str." ") or die("Write failed "); // 将用户名发送到服务器*/ 37 38 if ($buffer = socket_read($socket, 1024, PHP_NORMAL_READ)) { 39 ?> 40 <script> 41 var str = "user: <?php echo $str; ?> logout success! "; 42 alert(str); 43 </script> 44 <?php 45 } 46 socket_close($socket); 47 } 48 49 ?>
1 <?php 2 //server.php 3 //write for 张小豪 4 include ("conn.php"); 5 include ("response.php"); 6 set_time_limit(0); 7 8 $host = "127.0.0.1"; 9 $port = 8890; 10 $localuser = ""; //当前连接用户名 11 12 for($i=0; $i<5; $i++){ 13 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket "); // 创建一个Socket 14 $result = socket_bind($socket, $host, $port) or die("Could not bind to socket "); //绑定Socket到端口 15 $result = socket_listen($socket, 3) or die("Could not set up socket listener "); // 开始监听连接 16 $spawn = socket_accept($socket) or die("Could not accept incoming connection "); // 处理通信 17 18 $choose = socket_read($spawn, 1024, PHP_NORMAL_READ) or die("Could not read input ");//接受选择符 19 $choose = trim($choose); 20 echo $choose."<br>"; 21 //echo "i: ".$i." "; 22 if($choose=="user"){ 23 response_user($spawn, $localuser); 24 }else if($choose=="login"){ 25 response_login($spawn, $localuser); 26 }else if($choose=="logout"){ 27 response_logout($spawn, $localuser); 28 } 29 // 关闭sockets 30 socket_close($spawn); 31 socket_close($socket); 32 }
1 <?php 2 //response for server 3 //response.php 4 //write for 张小豪 5 function response_user($spawn, $localuser){ 6 $localuser = socket_read($spawn, 1024, PHP_NORMAL_READ) or die("Could not read input "); // 数据传送 获得客户端的输入 7 echo "localuser: ".$localuser." "; 8 $input = socket_read($spawn, 1024) or die("Could not read input "); // 数据传送 获得客户端的输入 9 $input = trim($input); 10 echo 'input:', $input, " "; 11 //从数据库中查gpa 12 $sql = "select gpa from gpalist where id = '$input'"; 13 $query = mysql_query($sql); 14 $row = mysql_fetch_array($query); 15 16 if($row['gpa']) { 17 $output = $row['gpa']. " "; //处理客户端输入并返回结果 18 }else{ 19 $output = "no imformation "; //处理客户端输入并返回结果 20 } 21 echo "output:", $output, "<br>"; 22 // 数据传送 向客户端写入返回结果 23 socket_write($spawn, $output, strlen($output)) or die("Could not write output "); 24 } 25 26 function response_login($spawn, $localuser){ 27 $input = socket_read($spawn, 1024) or die("Could not read input "); // 数据传送 获得客户端的输入 28 $input = trim($input); 29 echo 'username:', $input, " "; 30 //从数据库中查gpa 31 $sql = "select password from userlist where user = '$input'"; 32 $query = mysql_query($sql); 33 $row = mysql_fetch_array($query); 34 35 //print_r(row); 36 if($localuser!=""){ 37 $output = "busy "; 38 } 39 else{ 40 if($row['password']) { 41 $output = $row['password']. " "; //处理客户端输入并返回结果 42 }else{ 43 $output = "no username "; //处理客户端输入并返回结果 44 } 45 } 46 echo "output:", $output, "<br>"; 47 // 数据传送 向客户端写入返回结果 48 socket_write($spawn, $output, strlen($output)) or die("Could not write output "); 49 } 50 51 function response_logout($spawn, $localuser){ 52 $input = socket_read($spawn, 1024, PHP_NORMAL_READ) or die("Could not read input "); // 数据传送 获得客户端的输入 53 $input = trim($input); 54 echo 'username:', $input, "logout<br>"; 55 $output = "success! "; 56 $localuser = ""; 57 // 数据传送 向客户端写入返回结果 58 socket_write($spawn, $output, strlen($output)) or die("Could not write output "); 59 } 60 61 ?>
1 <?php 2 //连接数据库 3 4 $conn = @mysql_connect("localhost", "root", "") or die ("connect error!"); 5 mysql_select_db("socket", $conn) or die ("select error!"); 6 mysql_query("set names 'utf8'") or die ("wrong"); 7 ?>