Mark's Place

A place for Mark's murmuring, blah, blah, blah, ...

Mark's Place
Watch Timeline

How to add password protection in CodeIgniter Scaffolding

Mark Wu 發表於 2009/10/06 10:50 · 靜態連結網址

It is really bad that CodeIgniter does not support password protection in scaffolding. I just go thorugh most scaffolding related posts in CodeIgniter forum, it seems no easy and clear solution for it. So, I just come out my own based on hooks function.

Here comes the code. Just follow it, and you will get a simple password protection for scaffolding:

1. Enable hook in your CodeIgniter application

application/config.php

[...]
$config['enable_hooks'] = TRUE;
[...]

2. Add a hook setting for pre-controller

application/hooks.php

[...]
$hook['pre_controller'][] = array(
                                'class'    => 'SimpleHttpAuth',
                                'function' => 'authenticate',
                                'filename' => 'SimpleHttpAuth.php',
                                'filepath' => 'hooks/SimpleHttpAuth',
                                'params'   => array()
                                );
[...]

 

3. Put the following scripts into application/hooks/SimpleHttpAuth/SimpleHttpAuth.php

<?php
    class SimpleHttpAuth
    {
        private $user = "username";
        private $pass = "password";

        function __constructor()
        {
        }

        function authenticate()
        {
            $router =& load_class('Router');

            if($router->scaffolding_request === TRUE)
            {
                while (!$this->isAuthenticated()) {
                    header('WWW-Authenticate: Basic realm="Scaffolding"');
                    header('HTTP/1.1 401 Unauthorized');
                    die('Authorization Required');
                }
            }
        }

        function isAuthenticated() {
            if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
                $httpd_username = filter_var($_SERVER['PHP_AUTH_USER'], FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_HIGH|FILTER_FLAG_ENCODE_LOW);
                $httpd_password = filter_var($_SERVER['PHP_AUTH_PW'], FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_HIGH|FILTER_FLAG_ENCODE_LOW);
                if ($httpd_username == $this->user && $httpd_password == $this->pass) {
                    return TRUE;
                } else {
                    return FALSE;
                }
            }
            return FALSE;
        }

    }
?>

Now, you can have password protection scaffolding in CodeIgniter.

真實引用網址:

http://blog.markplace.net/trackback.php?id=404

迴響 (1) · 引用 (0) 文章分類: 可以做人幹嘛寫程式

迴響 ↓

  • 回覆: How to add password protection in CodeIgniter Scaffolding
    我愛天天問  發表於 12/10/2009, 03:15

    能看懂點,費力。