client.php
<?php
//遍历数组元素,并将元素转码
function array_iconv($data, $in_charset='GBK', $out_charset='UTF-8'){
if (!is_array($data)){
$output = iconv($in_charset, $out_charset, $data);
}elseif(count($data) === count($data, 1)){//判断是否是二维数组
foreach($data as $key => $value){
$output[$key] = iconv($in_charset, $out_charset, $value);
}
}else{
eval_r('$output = '.iconv($in_charset, $out_charset, var_export($data, TRUE)).';');
}
return $output;
}
$arr = array("username"=>'aaa',"pwd"=>'47bce5c74f589f4867dbd57e9ca9f808','sex'=>'男');
$arr['_e_'] = 'pwd,sex,username'; //这个参数中存的是randkey里面数组value值的排序
//获得按'_e_'排序的临时数组,这是为了得到randkey
$keys = explode(",",$arr['_e_']);
$tempArr = array();
for($i=0;$i<count($arr)-1;$i++){
$tempArr["$keys[$i]"] = $arr["$keys[$i]"];
}
$str='';
foreach ($tempArr as $key=>$value){
$str .= $value;
}
$randkey = md5($str.'123abc');
$arr['randkey'] = $randkey;
$arr = array_iconv($arr);
$arr = array_map('urlencode',$arr); //可使用http_build_query()函数替代
$data = json_encode($arr);
$url = "http://localhost/zchnqi/member/receive.php?json=$data";
/****get方式传递数据******
//初始化
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//执行并获取HTML文档内容
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
//打印获得的数据
print_r($output);
*/
//***post方式传输数据******
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
//打印获得的数据
print_r($output);
$res=unserialize($output);
var_dump($res);
if ($res['stat']=='1') {
echo 'right';
}else{
echo 'error';
}
?>
server.php
<?php
require_once(dirname(__FILE__)."/config.php");
//遍历数组元素,并将元素转码
function array_iconv($data, $in_charset='GBK', $out_charset='UTF-8'){
if (!is_array($data)){
$output = iconv($in_charset, $out_charset, $data);
}elseif(count($data) === count($data, 1)){//判断是否是二维数组
foreach($data as $key => $value){
$output[$key] = iconv($in_charset, $out_charset, $value);
}
}else{
eval_r('$output = '.iconv($in_charset, $out_charset, var_export($data, TRUE)).';');
}
return $output;
}
if($_GET["json"]){ //数据传递为get方式
$arr = json_decode($_GET["json"],true);
}else{ //数据传递为post方式 或者使用$_SERVER['REQUEST_METHOD'] == 'POST',判断提交的数据是否是POST方式传来的
$arr = file_get_contents("php://input"); //若$arr=$_POST;返回null
$arr = json_decode($arr,true);
//var_dump($arr);
}
$arr = array_map('urldecode',$arr);
$arr = array_iconv($arr,'utf-8','gbk');
$str = $arr['_e_'];
//获得按'_e_'排序的临时数组,这是为了得到randkey
$keys = explode(",",$str);
$tempArr = array();
for($i=0;$i<count($arr)-2;$i++){
$tempArr["$keys[$i]"] = $arr["$keys[$i]"];
}
$str='';
foreach ($tempArr as $key=>$value){
$str .= $value;
}
$randkey = md5($str.'123abc');
if($randkey == $arr['randkey']){
$son = $dsql->GetOne("SELECT * FROM #@__member WHERE user='$arr[username]' ");
if($son[pwd] == $arr[pwd]){
//将数组序列化后输出,客户端反序列化后还原为数组;也可以使用json_encode()输出,客户端使用josn_decode()获得数组
echo serialize($son);
}else{
echo serialize('password error'); //这边序列化是为了防止客户端反序列化该字符串不标准而报错
}
}else{
echo serialize('vertiy code error');
}
?>