php - Nusoap set and get headers in both client and server side -
i developing simple web service using nusoap , having problem when adding authentication.
currently using setcredentials() method attach username , password request , getting them using $_server['php_auth_user'] in server side.
it works fine, don't want authenticate user every call. need maintain session , according research, after first authentication, server needs send 'session id' attached response header , client should use 'session id' in subsequent calls.
but don't know how set , headers in both request , response. help?
i'm new soap , session, if there wrong thoughts, please point out. thank much.
finally, figured out myself. used simple implementation , insecure, may beginners me.
first, observing nusoap code find easy set , soap headers both in client , server.
client:
$client->getheader(); //return array containing header elements $client->setheaders('headerstring');
server:
$server->responseheaders = 'headerstring'; //set response headers $server->requestheaders //get request headers
second, after first authentication using username/password, saved username in session , sent session id client. client sends session id @ next call instead of username/password.
server:
function someservice (){ global $server; $valid = false; $requestheaders = $server->requestheader; /*get session id request header , open existing sid*/ if(isset($requestheaders['sessiontoken'])){ $sid = $requestheaders['sessiontoken']; session_id($sid); } session_start(); /*if user not verified before, need verify it*/ if(!isset($_session['user'])){ $valid = verifyuser(); if($valid){ $_session['user'] = $_server['php_auth_user']; } } if($valid || isset($_session['user'])){ //user verified or veirified. $server->responseheaders = '<sessiontoken>'. session_id() .'</sessiontoken>'; /*** code goes here ***/ } else{ return new soap_fault(401,'', 'user not verified!'); } }
client:
require_once "nusoap.php"; $client = new nusoap_client("auth.wsdl", true); $authheaders = $client->getheader(); if(isset($authheaders['sessiontoken'])){ $header = '<sessiontoken>'. $authheaders['sessiontoken'] .'</sessiontoken>'; $client->setheaders($header); } else{ $client->setcredentials('username','password','basic'); } $result = $client->call("someservice", array());
again, above simple example , beginner. if have suggestions, please let me know
Comments
Post a Comment