来自外部网站Codeigniter的Json身份验证

时间:2016-03-09 12:25:51

标签: php json codeigniter authentication

我目前有一个Codeigniter 3框架网站(称为外部系统),我刚刚购买了一个帮助台php脚本,其中包含2个文件Json Authentication,可以帮助连接外部系统。

我的目的是当用户去帮助台并登录时,他们需要使用我当前的网站登录信息,因此他们不必再次注册(这样,帮助台和我的Codeigniter脚本的用户信息可以保持不变)。

帮助台的后端有一些需要提供的字段:url,site id,authentication key,login for login。

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重新设置为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;
  }
  ............

2 个答案:

答案 0 :(得分:0)

(注意:取决于authclass.php的样子,这可能会也可能不起作用。)

将authclass.php重命名为Auth.php(区分大小写),确保该文件顶部的类名为“Auth”,并将其放在应用程序/库中。

使用以下功能创建控制器:

function test(){
   $this->load->library('auth');
   $auth = new auth();
   /* rest of index.php code should work */
}

答案 1 :(得分:0)

将authclass转换为库是正确的。确保您需要呼叫的功能是公开的。

所以在<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class HelpdeskAuth { protected $ci; //in case you need to call CI's functions function __construct($params = array()) { parent::__construct(); $this->ci = get_instance(); } //Their code here }

index.php

他们的index()。您可以创建一个新控制器并将代码粘贴到public function index() { $this->load->library("HelpdeskAuth"); $send_array['success'] = 0; if (isset($_POST['site_id']) && isset($_POST['data'])) { if ($_POST['site_id'] == 1) { /*Set your authentication key here*/ $this->HelpdeskAuth->set('key', ''); $data = $this->HelpdeskAuth->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 $this->HelpdeskAuth->encrypt(json_encode($send_array)); } } } } } 函数中,或者在现有控制器中编写一个新函数。

您只需要确保正确调用我们创建的库

$_POST['']

您还可以将所有$this->input->post('')转换为$this->output->set_output()并使用 public int compare(String a, String b) { return (- a.compareTo(b)); } 代替最终回音,但这不是强制性的