スポンサーサイト

0

    一定期間更新がないため広告を表示しています


    • 2014.12.23 Tuesday
    • -
    • -
    • -
    • -
    • -
    • by スポンサードリンク

    CodeIgniterでユーザー認証

    0
      CodeIgniterのscaffoldはphpMyAdminの代替(しかも一時的)にしか使えないということがわかったのでfーー;
      django.contrib.adminみたいなcoolなものでは無かった様子。

      めげてばかりもいられないので、
      ・ユーザー認証
      ・CRUD
      の作り方、要はdjangoだとcontrib.authとかcontrib.adminがやってくれちゃうところから、まず自作することにしました。 ←django流(?)に「管理画面を作ってから公開用画面を作る」です^^


      まず、「codeigniter ユーザー認証」「codeigniter authentication」等のキーワードで片っ端からググりました。
      すると、現在はCodeIgniterには純正認証フレームワークは付いておらず、サードパーティのプラグインやライブラリの形で公開されていることがわかりました。
      #いずれ認証クラスがデフォルトで付くかもしれないけど :-)

      FreakAuth
      Rapyd
      ←プラグインですがcontrib.adminみたいなものを作ってくれるやつらしい?

      どちらもアプリをフルに上書きして使う形で、既にconfigファイルを書いてしまった気の早い私には不向きです(爆)
      あと、使い方のドキュメント&インターフェイスが英語ばかりなので、習得するのに時間が掛かる&画面の日本語化メンドクサという理由で却下。


      そこで、こちらのwikiを参考にControllerをextendしたMY_Controllerを使う方法で実装してみました。
      application/library/MY_Controller.php
      <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

      class MY_Controller extends Controller {


      function MY_Controller()
      {
      parent::Controller();
      $this->load->database();
      $this->db->query('SET NAMES utf8');

      $this->load->library('session');
      if($this->session->userdata('is_superuser')!='yes')
      {
      if($this->session->userdata('next'))
      {
      $this->session->set_userdata(array('next'=>$this->uri->uri_string()));
      }
      header("Location:".$this->config->item('base_url')."index.php/auth/login/".$this->session->userdata('next'));

      }
      }

      }

      ?>


      application/controllers/auth.php
      <?php

      class Auth extends Controller {

      function Auth()
      {
      parent::Controller();
      $this->load->library('session');
      }


      function login($next='')
      {
      if($this->session->userdata('is_supeuser')=='yes')
      {
      header("Location:".$this->config->item('base_url').'index.php/');
      }
      elseif($_SERVER['REQUEST_METHOD']=='POST')
      {
      $username = $this->input->post('username');
      $password = $this->input->post('password');
      $next = $this->input->post('next');
      if($username=='admin' && $password=='admintest') //実際に使うときはIDとパスワードはdbから取ります^^;
      {
      $this->session->set_userdata(array('is_superuser'=>'yes'));
      }
      header("Location:".$this->config->item('base_url').'index.php/'.$next);
      }
      else
      {
      print_r($this->session);
      $data['title'] = "ログインしてください";
      $data['next'] = $next;
      $this->load->helper('form');
      $this->load->view('login_form', $data);
      }
      }

      function logout()
      {
      $this->session->sess_destroy();
      header("Location:".$this->config->item('base_url')."index.php");
      }
      }
      ?>

      を用意します。


      で、アクセス制限したい場合(@login_requiredな場合。笑)は、コントローラ単位で制御します。
      例えば:
      class Welcome extends MY_Controller {

      function Welcome()
      {
      parent::MY_Controller();
      }

      function index()
      {
      echo "この部分はログインユーザーしか見られません。";
      }

      とすると、index.php/welcome/indexにアクセスすると、まずindex.php/auth/loginにぶっ飛ばされログインを要求されます。
      閲覧者が正しいIDとパスワードを入力すると、めでたくindex.php/welcome/indexが表示できるというわけ。
      用意する特別なファイルは2つだけだし、コントローラのコンストラクタを変えて歩くだけで認証ページに出来ちゃう。

      なかなか良い感じです。今後PHP4鯖の案件はcodeigniterにしようかな〜。


      スポンサーサイト

      0

        • 2014.12.23 Tuesday
        • -
        • 16:41
        • -
        • -
        • -
        • -
        • by スポンサードリンク

        コメント
        コメントする








           
        この記事のトラックバックURL
        トラックバック
        という事で、ログイン機能を実装してみよう!と、ちょこまか作業。こちらを参考にさせていただきました。 CodeIgniterでユーザー認証 CodeIgniterで管理ページを作成する KuniTsujiさんが公開...
        • されどLAMPな日々
        • 2008/07/28 1:20 AM

        PR

        calendar

        S M T W T F S
             12
        3456789
        10111213141516
        17181920212223
        24252627282930
        31      
        << March 2024 >>

        twitter

        selected entries

        categories

        archives

        recent comment

        • djangoテンプレート上でmodelのメソッドに引数を渡す方法(djangoで出勤簿アプリ試作中♪)
          GavannITサービス-なりとみ
        • 私がそれでも名古屋市に住み続ける理由を6つにまとめてみた!
          bose wireless speaker
        • FastCGIでdjango…400エラー???
          levi's
        • さくらインターネットdjangoが突然500エラー!?(Pythonバージョンアップされてた
          salomon running shoes
        • 私がそれでも名古屋市に住み続ける理由を6つにまとめてみた!
          louboutin shoes
        • FastCGIでdjango…400エラー???
          yeezy boost 350
        • さくらインターネットdjangoが突然500エラー!?(Pythonバージョンアップされてた
          jordan 11
        • Silexでエラーページをカスタマイズする方法 : Symfony Advent Calendar 2011 - day 12
          pandora jewelry
        • django対symfony 日本語メール送信(その1 symfony編)
          nike air vapormax
        • 解決!XREAでCGI版Pythonを使ってdjangoを動かす(人柱?)
          kate spade

        recent trackback

        • HTMLの表(TABLE)のセル(TD)に斜線を引くjavascriptライブラリ slash.js 作っちゃいました
          常山日記
        • django対symfony 日本語メール送信(その1 symfony編)
          CPA-LABテクニカル
        • CodeIgniterでユーザー認証
          されどLAMPな日々
        • 久々にdjangoを最新版にしたらHTMLがエスケープされちゃった!!(解決済み)
          常山日記
        • FastCGIを諦めてmod_pythonを使う。Apacheのアップグレード
          サーバー用語集
        • さくらインターネット、sqlite3でdjango@CGI版を使う際の設定メモ
          常山日記
        • さくらインターネット スタンダードプランでdjango使ってる方、DBは?
          mitszoの日記
        • python多次元リストをsort(並べ替え)する方法?
          mitszoの日記
        • フォームから送信した値とrequest.POSTの挙動($_POST@PHPとの比較)
          Humming Via Kitchen
        • 日本語テキストをtruncate@django(Python全般にも??)
          常山日記

        recommend

        links

        profile

        search this site.

        others

        mobile

        qrcode

        powered

        無料ブログ作成サービス JUGEM