【问题标题】:Json Authentication from external website Codeigniter来自外部网站 Codeigniter 的 Json 身份验证
【发布时间】: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


    【解决方案1】:

    (注意:取决于 authclass.php 的样子,这可能有效,也可能无效。)

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

    创建一个具有以下功能的控制器:

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

    【讨论】:

    • 非常感谢您的帮助,它仍然无法正常工作。我已经更新了我尝试过的内容。而 Auth.php 只是使用 base64 和 rindale 加密数据的功能。再次感谢
    • 这是一个很长的镜头,但我注意到库类和控制器具有相同的名称(Auth)。尝试给控制器一个不同的名称(和相应的文件名)。
    • 无论如何,查看库 auth 类代码会有所帮助。
    • 我按照 Adrien 的说法进行了检查和修复,但它不起作用,所以我只更新了库中的 authclass 编码的一部分,因为我担心在共享其他作者的编码时它可能会违反。再次感谢您的帮助。
    • 您好,我已经更新了我尝试过的内容,请帮我看看。非常感谢,我真的很感激
    【解决方案2】:

    您将他们的 authclass 转换为库是正确的。确保您需要调用的函数是公开的。

    所以在application/libraries

    <?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-&gt;input-&gt;post('') 并使用$this-&gt;output-&gt;set_output() 代替最终的回显,但这不是强制性的

    【讨论】:

    • 非常感谢您的帮助。我只是按照你所说的做了,但是没有用。我检查了一切。您是否认为我在上面设置的 receive_array 和 send_array 设置错误?而且我只是更新了库中的部分 authclass 代码,因为我担心在共享其他作者的编码时它可能会违反。谢谢
    • 你不应该再有`$auth = new auth();`,这是$this-&gt;load-&gt;library() 所做的。您应该使用$this-&gt;auth instead()$auth。除此之外你还有什么错误吗?
    • 我删除了它并使用了 $this->auth.再次感谢您
    • 您好,我已经更新了我尝试过的内容,请帮我看看。非常感谢,我真的很感激
    • 嗨阿德里安,没有错误信息。在我按上述方式编码并上传到我的 codeigniter 网站后。我去了帮助台网站并尝试使用我的codeigniter网站的用户信息登录。它只是说登录失败。无论如何我可以检查我的网站是否真的将信息传递给帮助台登录表单。再次感谢您
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多