Home > PHP > ログイン画面を作ってみる/CakePHP

ログイン画面を作ってみる/CakePHP

  • 2007-11-24 (土)
  • PHP

ログイン画面をデバッグする(CakePHP修行 #012) | IDEA*IDEA
CakePHP Manual

などを参考にさせて頂き、自分のDBと合わせながら、
ログイン画面を作成してみました。

1ビューの部分を作成する


<form action="<?php echo $html->url('/users/login'); ?>" method="post">
<div>
    <label for="username">Username:</label>
    <?php echo $html->input('User/name', array('size' => 20)); ?>
</div>
<div>
    <label for="password">Password:</label>
    <?php echo $html->password('User/passwd', array('size' => 20)); ?>
</div>
<div>
    <?php echo $html->submit('Login'); ?>
</div>
</form>

本当はsmartyが使いたいのですが、なんか配列の部分でCakePHPは使い勝手が
悪い部分もあるということなので、とりあえずこのままでいこうと思います。
でもsmartyに慣れてるのでちょっと気持ち悪いです。

<?php echo $html->input('User/name', array('size' => 20)); ?>

この場合の「array('size' => 20)」というのはビューの部分にvalidateみたいな
ロジックを書いてるということだと思うのですが、これが当たり前のことなのかは
今後わかってくるのかな?

2、/app/controllers/users_controller.php (partial)


<pre><code>
<?php
class UsersController extends AppController
{
    function home ()
    {
	    $this->checkSession();
	    $this->set('me', $this->User->findById($this->Session->read('authenticated_id')));
    }
    
    function login()
    {
        //Don't show the error message if no data has been submitted.
        $this->set('error', false); 

// If a user has submitted form data:
if (!empty($this->data))
{
// First, let's see if there are any users in the database
// with the username supplied by the user using the form:

$someone = $this->User->findByName($this->data['User']['name']);

// At this point, $someone is full of user data, or its empty.
// Let's compare the form-submitted password with the one in
// the database.

if(!empty($someone['User']['passwd']) && $someone['User']['passwd'] == $this->data['User']['passwd'])
{
// Note: hopefully your password in the DB is hashed,
// so your comparison might look more like:
// md5($this->data['User']['password']) == ...

// This means they were the same. We can now build some basic
// session information to remember this user as 'logged-in'.

$this->Session->write('authenticated_id', $someone['User']['id']);

// Now that we have them stored in a session, forward them on
// to a landing page for the application.

$this->redirect('/users/home/');
}
// Else, they supplied incorrect data:
else
{
// Remember the $error var in the view? Let's set that to true:
$this->set('error', true);
}
}
}

function logout()
{
// Redirect users to this action if they click on a Logout button.
// All we need to do here is trash the session information:

$this->Session->delete('authenticated_id');

// And we should probably forward them somewhere, too...

$this->redirect('/');
}
}
?>


3、/app/app_controller.php (partial)


<?php
class AppController extends Controller
{
    function checkSession()
    {
        // If the session info hasn't been set...
        if (!$this->Session->check('authenticated_id'))
        {
            // Force the user to login
            $this->redirect('/users/login');
            exit();
        }
    }
}

?>


/app直下にAppControllerというクラスを作って、
checkSessionというメソッドを呼び出す。このあたりは
真似をさせて頂きました。

4、/app/views/users/home.thtml
にログイン後に飛ぶページを作ります。
とりあえずこれも百式さんの真似で、var_dumpでdumpします。


<?php
  var_dump($me);
?>


以下みたいな感じでログインページの作成はできましたー。

login0.jpg

Comment:0

Comment Form
Remember personal info


Home > > ログイン画面を作ってみる/CakePHP

Page Top