api 登入 zabbix 獲取sessionId
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bootstrap 例項 - 彈出框(Popover)外掛</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="http://new.imanager.com/distpicker-master/docs/css/main.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="http://new.imanager.com/imanager/_doc/loading.js"></script>
</head>
<body>
<div class="panel-body" style="padding:0px;background-image:url('../images/rack/jg.gif')" width="144" height="450">
<div class="panel panel-default" style="height:450px;">
<div class="panel-heading">查詢條件</div>
<div class="panel-body">
<form id="formSearch" class="form-horizontal">
<div class="form-group">
<div class="col-xs-4">
<button type="button" id="btn_submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>載入測試</button>
</div>
</div>
</form>
</div>
</div>
</div>
<script>
$(function () {
$("#btn_submit").on("click", function () {
$.ajax({
type: 'post',
url: 'http://xxx.zabbix/api_jsonrpc.php',
contentType: "application/json;charset=utf-8",
data: JSON.stringify({
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "admin",
"password": "admin",
},
"id":1
}),
dataType: "json",
success: function (message) {
if (message.result) {
// window.location.href = 'http://xxx.zabbix/itmm.php?action=dashboard.view&dashboardid=24&auth='+message.result.sessionid;
// window.location.href = 'http://xxx.zabbix/itmm.php?id='+message.result;
login(message.result);
}
},
error: function (message) {
alert("alert");
},
});
})
});
function login(id) {
$.ajax({
url: 'http://xxx.zabbix/index.php?session_id='+id,
contentType: "application/json;charset=utf-8",
type: 'get',
dataType: "json",
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function (message) {
if (message.result) {
window.location.href = 'http:/xxx.zabbix/host_screen.php?resourcetype=1&groupid=0&hostid=10126&sid=4476634f98505db4&screenid=0';
// window.location.href = 'http://new.imanager.com/imonitor/itmm.php?id='+message.result;
}
},
error: function (message) {
alert("alert");
},
})
}
</script>
</body>
</html>
zabbix index.php 處理 設定session cookie
$local_url = $_SERVER['QUERY_STRING'];
if (isset($_REQUEST['enter']) && $_REQUEST['enter'] == _('登入')) {/*honglimin modify */
******省略
}
else {
// 設定session id 邏輯
if($local_url){
$arr = explode('=', $local_url);
if(count($arr) == 2){
if(isset($arr[1]) && ($arr[0] == 'session_id')){
$session_id = $arr['1'];
CWebUser::setSessionCookie($session_id); # 將sessionid寫在url裡,然後從url中獲取sessionid,然後再把sessionid寫進去
echo json_encode(array('result'=>'ok')); # 302跳轉
exit;
}
}
}
// login the user from the session, if the session id is empty - login as a guest
CWebUser::checkAuthentication(CWebUser::getSessionCookie());
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結