|
|
|
ช่วยดู nusoap ทีครับ ทำ login ผ่าน web service return array ครับ |
|
|
|
|
|
|
|
พอดีจะทำ login ผ่าน web service รับส่งข้อมูลแบบ array ลองทำดูแล้ว ไม่มีการส่งค่ามาจากทาง web service เลยครับ
รบกวนช่วยดูโค้ดให้ทีครับ
service.php
require_once("lib/nusoap.php");
require_once("configDB.php");
/**
*------------------------------ Configuration ---------------------------------------------------------
**/
//Create a new soap server
$server = new soap_server();
//Configure WSDL
$server->configureWSDL("usersLogin", "urn:server");
//Define namespace
$server->wsdl->schemaTargetNamespace = 'urn:server';
//Register argument parameters
$param = array(
"username" => "xsd:string",
"password" => "xsd:string"
);
//Add ComplexType with Array
$server->wsdl->addComplexType("ListArray",
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'xsd:string[]')
),
'xsd:string');
//Register service and method
$server->register('usersLogin',
$param,
array('return' => 'tns:ListArray'),
"urn:server",
"urn:server#loginServer"
);
/**
*------------------------------ Function ---------------------------------------------------------
**/
function usersLogin($username, $password){
$user = $username;
$pass = md5(sha1($password));
$arr = array();
$login = sprintf("SELECT user_id FROM users WHERE user_login = '%s' AND user_pass = '%s' ",$user, $pass);
$queryLogin = mysql_query($login) or die(mysql_error());
$numLogin = mysql_num_rows($queryLogin);
$resultLogin= mysql_fetch_assoc($queryLogin);
if($numLogin > 0){
$arr = array(
"status" => true,
"dbData" => array("uid" => base64_encode($resultLogin["user_id"])),
"txtResponse" => "Login Complate."
);
}else{
$arr = array(
"status" => false,
"dbData" => null,
"txtResponse" => "Incorrect username/password combination."
);
}
mysql_close($objConnect);
return $arr;
}
// Get our posted data if the service is being consumed
// otherwise leave this data blank.
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
// pass our posted data (or nothing) to the soap service
$server->service($POST_DATA);
exit();
client.php
$arr = array();
include("nusoap/lib/nusoap.php");
$client = new nusoap_client("http://localhost/cctv/nusoap/service.php?wsdl",true);
//Configured parameter
$params = array(
"username" => $username,
"password" => $password
);
//Request methoed web service
$data = $client->call("usersLogin",$params);
if($data["status"] === true){
$_SESSION["userid"] = base64_decode($data["dbData"]["uid"]);
$_SESSION["username"] = $username;
//Create cookie hash
$cookiehash = md5(sha1($username . get_real_ip()));
mysql_query(sprintf("UPDATE cctv_users SET login_session = '%s' WHERE user_id = '%s'",$cookiehash, $_SESSION["userid"]));
if($lifetime = "on")
setcookie("cookiehash",$cookiehash,time()+3600*24*365);
else
setcookie("cookiehash"); //Delete cookie
$arr = array(
"status" => $data["status"],
"status_txt" => $data["txtResponse"]
);
}else{
$arr = array(
"status" => "test1",
"status_txt" => $data["txtResponse"]
);
}
echo json_encode($data);
ช่วยดูให้ทีครับ
ขอบคุณมากครับ
Tag : PHP, MySQL
|
ประวัติการแก้ไข 2013-07-14 04:54:50 2013-07-14 04:55:32
|
|
|
|
|
Date :
2013-07-14 04:49:39 |
By :
fogza |
View :
1347 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าจะหาจริง ๆ อาจจะต้องทำการ debug ดูพวกค่านี้ครับ
Code (PHP)
function usersLogin($username, $password){
$user = $username;
$pass = md5(sha1($password));
$arr = array();
$login = sprintf("SELECT user_id FROM users WHERE user_login = '%s' AND user_pass = '%s' ",$user, $pass);
$queryLogin = mysql_query($login) or die(mysql_error());
$numLogin = mysql_num_rows($queryLogin);
$resultLogin= mysql_fetch_assoc($queryLogin);
if($numLogin > 0){
$arr = array(
"status" => true,
"dbData" => array("uid" => base64_encode($resultLogin["user_id"])),
"txtResponse" => "Login Complate."
);
}else{
$arr = array(
"status" => false,
"dbData" => null,
"txtResponse" => "Incorrect username/password combination."
);
}
mysql_close($objConnect);
return $arr;
}
ทดสอบส่งตัวแปรแบบง่าย ๆ ไปดูว่ามันสามารถทำงานได้หรือไม่
PHP and Web Service
|
|
|
|
|
Date :
2013-07-14 08:35:39 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอนนี้แก้ไขได้แล้วครับ ระบุฐานข้อมูลผิด แต่ติดปัญหาใหม่ครับ
คือ user id ผมใช้ base64_encode เข้ารหัสไว้
แล้วฝั่งรับก็เอามาถอดรหัสด้วย base64_decode จากนั้นก็ return ค่าให้ jquery แบบ json
แต่พอแสดงใน console.log ค่าของ user id ยังถูกเข้ารหัสด้วย base64 อยู่เลยครับ
ขอคำแนะนำด้วยครับ
ขอบคุณครับ
|
|
|
|
|
Date :
2013-07-15 13:58:28 |
By :
fogza |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อันที่จริงถ้า encode ก็น่าจะ decode ครบครับ
|
|
|
|
|
Date :
2013-07-15 14:45:48 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
นั่นนะสิครับ ใช้ base64_decode แต่มันไม่ถอดรหัสให้ งงเลย - -"
|
|
|
|
|
Date :
2013-07-15 15:40:24 |
By :
fogza |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|