【发布时间】:2016-06-23 19:50:05
【问题描述】:
我目前有一个 Codeigniter 3 框架网站(称为外部系统),我刚刚购买了一个帮助台 php 脚本,其中包含 2 个文件 Json 身份验证,可帮助连接外部系统。
我的目的是当用户去helpdesk登录时,需要使用我当前的网站登录信息,这样就不用重新注册了(这样helpdesk的用户信息和我的Codeigniter脚本可以保持一致)。
帮助台后端有一些需要提供的字段,例如:url、站点 id、身份验证密钥、登录时创建用户。
Helpdesk 脚本有 2 个文件,需要上传到外部系统,index.php 和 Authclass.php。 Authclass.php 只是用于加密的类和函数(无需自定义)。所以我认为 Authclass.php 需要上传到 Codeigniter 应用程序中的库文件夹。 index.php的代码如下。
谁能帮我或建议将这些文件上传到codeigniter文件夹的位置以及如何修改以使其工作?
非常感谢
index.php
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Content-Type: application/json; charset=utf-8');
include('authclass.php');
$auth = new auth();
$send_array['success'] = 0;
if (isset($_POST['site_id']) && isset($_POST['data'])) {
if ($_POST['site_id'] == 1) {
/*Set your authentication key here*/
$auth->set('key', '');
$data = $auth->decrypt($_POST['data']);
$receive_array = json_decode($data, true);
if (is_array($receive_array)) {
if ($receive_array['task'] == 'authenticate') {
/*This would connect to your external database here.*/
if ($receive_array['username'] == 'username' && $receive_array['password'] == 'password') {
$send_array['success'] = 1;
$send_array['name'] = 'example name';
$send_array['email'] = 'example@example.com';
}
echo $auth->encrypt(json_encode($send_array));
}
}
}
}
?>
authclass.php
class auth {
private $config = array();
private $user = array();
function __construct() {
$this->config['key'] = '';
}
public function set($name, $value) {
$this->config[$name] = $value;
}
....
我在下面尝试过的 index.php(在控制器文件夹中)的更新被重命名为 helpdesk.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
$this->output->set_header('Cache-Control: no-cache, must-revalidate');
$this->output->set_header('Content-Type: application/json; charset=utf-8');
public function helpdesk() {
$this->load->library('auth');
$this -> new auth();
$send_array['success'] = 0;
if (isset($_POST['site_id']) && isset($_POST['data'])) {
if ($_POST['site_id'] == 1) {
/*
Set your authentication key here
*/
$this->auth->set('key', 'test1234');
$data = $this->auth->decrypt($_POST['data']);
$receive_array = json_decode($data, true);
if (is_array($receive_array)) {
if ($receive_array['task'] == 'authenticate') {
/*
This is where your auth function exists (you would connect to your external database here).
*/
if ($receive_array['username'] == ['email'] && $receive_array['password'] == ['password']) {
$send_array['success'] = 1;
$send_array['name'] = ['username'];
$send_array['username'] = ['email'];
$send_array['password'] = ['password'];
}
echo $this->auth->encrypt(json_encode($send_array));
}
}
}
}
}
?>
authclass.php被remaned到auth.php并上传到库文件夹,我已经尝试修改
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class auth {
protected $ci; //in case you need to call CI's functions
function __construct($params = array())
{
parent::__construct();
$this->ci = get_instance();
}
private $config = array();
private $user = array();
function __construct() {
$this->config['key'] = '';
}
public function set($name, $value) {
$this->config[$name] = $value;
}
............
【问题讨论】:
标签: php json codeigniter authentication