# 授權控制
授權控制就是你驗證授權的代碼了,為了方便對接,連開發文檔都不需要,就這一個SDK,塞到那都行(要放到namespace下面,具體行不行以你測試為準,反正不能放到文件最下面),遍布系統各個文件都行。真正做到了傻瓜式對接,保姆及教程。話不多說,進入正題。
SDK:
```
$RootDir = $_SERVER['DOCUMENT_ROOT'];
$firedir = "$RootDir/文件路徑";
$version = require($firedir);
$ip = $_SERVER['SERVER_ADDR'];
$domain = $_SERVER['HTTP_HOST'];
$post_string = array();
$header = array("Content-Type:multipart/x-www-form-urlencoded");
$url = 'http://你的授權站/index.php/api/index/getAuth?ip='.$ip.'&app_id='.$version['APP_ID'].'&domain='.$domain;
$ch = curl_init(); // 啟動一個CURL會話
curl_setopt($ch, CURLOPT_URL, $url); // 要訪問的地址
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 對認證證書來源的檢查 // https請求 不驗證證書和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 從證書中檢查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_POST, true); // 發送一個常規的Post請求
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); // Post提交的數據包
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // 設置超時限制防止死循環
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 獲取的信息以文件流的形式返回
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模擬的header頭
$result = curl_exec($ch);
$result = json_decode($result, true);
if($result['code'] == 0){
echo '您的網站沒有授權,請點擊<a href="你的授權站">前往授權</a>';exit;}
```
你就把這段代碼塞到你的php文件里面,放到`<?php`后面。
例如:
```
<?php
$RootDir = $_SERVER['DOCUMENT_ROOT'];
$firedir = "$RootDir/Application/Common/Conf/version.php";
$version = require($firedir);
$ip = $_SERVER['SERVER_ADDR'];
$domain = $_SERVER['HTTP_HOST'];
$post_string = array();
$header = array("Content-Type:multipart/x-www-form-urlencoded");
$url = 'http://your.domain.com/index.php/api/index/getAuth?ip='.$ip.'&app_id='.$version['APP_ID'].'&domain='.$domain;
$ch = curl_init(); // 啟動一個CURL會話
curl_setopt($ch, CURLOPT_URL, $url); // 要訪問的地址
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 對認證證書來源的檢查 // https請求 不驗證證書和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 從證書中檢查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_POST, true); // 發送一個常規的Post請求
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); // Post提交的數據包
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // 設置超時限制防止死循環
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 獲取的信息以文件流的形式返回
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模擬的header頭
$result = curl_exec($ch);
$result = json_decode($result, true);
if($result['code'] == 0){
echo '您的網站沒有授權,請點擊<a href="http://your.domain.com/">前往授權</a>';exit;}
?>
```
添加到你系統的后臺登錄頁,首頁,主頁,愛加哪加哪,加進去就行,然后別忘了,一定要加密文件。