ど素人によるwebサービス開発日記

ウェブの構造もわかっていない超初心者ですが、Webサービスを開発中です。調べたこと、学んだことをまとめていきます。

セキュリティを考慮したログイン機能をPHPで作る方法その2-設定編(PHP Secure, Advanced Login Systemというサイトの翻訳)

セキュリティを考慮したログイン機能をPHPで作るべく、このPHP Secure, Advanced Login System - Subin's Blogブログに載っているチュートリアルに沿って進んでいます。

前回はまずファイルをダウンロードしましたが、このままではまだ使えません。

今回は設定をしていきたいとおもいます。

設定の種類

Setting up

You have to configure logSys first to continue. You have two options to change configurations. See about it here on the "configuration" section.

設定をするには2つの方法があるよ。このページ見てみて。

 とのことだったので、リンク先The Francium Project - Subin's Blogに行ってみると、

Configuration 設定

1. Custom Config

For this, you can make an array into the variable "config". You do this by making another file like "config.php" and including it instead of the class file.

Then, you include the above file in pages instead of "class.logsys.php" file. I recommend this custom config, because you can then easily update the class when a new version come out.

"config.php"を作って、そこからカスタマイズする方法。で、"class.logsys.php"を読み込ませる。これだと新しいバージョンが出た時も簡単にアップデート出来るよ。

2. Change Default Config 

You can directly change the values of the "\Fr\LS::$default_config" in the class file, so you won't need to make another file for custom configuration. But beware, when you're updating to a newer version of the class, be sure to reupdate the default configuration again.

デフォルトのファイルをいじることもできるけど、バージョンアップの時面倒だからやめとけば?

 とのこと。config.phpファイルはダウンロードしたファイル一式の中に入ってたから、これを作ってカスタマイズしろってことね。

設定内容

<?
include "class.logsys.php";
/**
* This configuration is for Subin's Blog Demos page
* running on RedHat's OpenShift server
*/
\Fr\LS::$config = array(
"info" => array(
"company" => "Subin's Blog Demos",
"email" => "mail@subinsb.com"
),
"db" => array(
"host" => "localhost",
"port" => 3306,
"username" => "username",
"password" => "password",
"name" => "database_name",
"table" => "logSysUsers"
),
"keys" => array(
"cookie" => "myCookieKey",
"salt" => "MySaltKey"
),

 config.phpを開いて、この青字部分を自分で変更しろって。

Make sure the secure keys for both hashing passwords and cookies are different and secure. Don't let anyone know them.

パスワードとクッキーは違うやつで、難しいやつにしとこ。あ、内緒ね。

データベースでテーブルの設定

テーブルをまだ作っていなかったので、新しくデータベースを作ってusersというテーブルを作成します。

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(10) NOT NULL,
  `email` tinytext NOT NULL,
  `password` varchar(64) NOT NULL,
  `password_salt` varchar(20) NOT NULL,
  `name` varchar(30) NOT NULL,
  `created` datetime NOT NULL,
  `attempt` varchar(15) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

https://subinsb.com/php-logsys

サイトの教えどおりにコピペして作ることにしました。自分で好きなもの付け加えたりしてもいいらしい。

そして、パスワードをリセットする時のトークン用テーブルも作成するらしい。

CREATE TABLE IF NOT EXISTS `resetTokens` (
 `token` varchar(40) NOT NULL COMMENT 'The Unique Token Generated',
 `uid` int(11) NOT NULL COMMENT 'The User Id',
 `requested` varchar(20) NOT NULL COMMENT 'The Date when token was created'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

https://subinsb.com/php-logsys

The reset password token saving table should be called "resetTokens". It is not changeable. If you really want to change it, find the SQL queries in the class file that uses the "resetTokens" table and replace it.

リセットトークンのテーブルは絶対"resetTokens"で、変えちゃだめ。もし本気で変えたかったら、SQLクエリーをファイルから探して勝手にして。

二つのテーブルを言われるがままに作成しました。

そして、テーブルが出来たところで上のconfig.phpのデータベース名とテーブル名も変更しましょう。

その他のページの設定

Initialize

Redirection based on the login status is needed for a login system. You should call the \Fr\LS::init(); at the start of every page on your site to redirect according to the login status. and continue with your HTML code. 

ログイン機能はredirectさせる設定をしなくてはいけないので、あなたのサイトの全てのページの最初の部分で、\Fr\LS::init();を呼び出してあげて。その後HTML続けていいから。

When a user not logged in visits a page that is not in the "pages"->"no_login" array, the user gets redirected to the login page mentioned in "pages"->"login_page" config value. If the user is logged in and is on a page mentioned in the "pages"->"no_login" array, he/she will be redirected to the "pages"->"home_page" URL.

もしユーザーがログインしないでページに来たら、home_pageURLに強制送還されるようになるよ。

 コードはこちら。このコードを全てのページの一番最初の部分に張り付ける。

<?php
require "class.logsys.php";
\Fr\LS::init();
?>
<html>

 

https://subinsb.com/php-logsys

画面上でコードが丸見えな件とエラーの件

先ほどまでこの画面のように、画面上でコードが丸見えの状態だったのですが、なんとなくconfig.phpないのすべての=>の記号を=に変えたら、消えてくれました。

=>は連想配列用のダブルアロー演算子というものらしく、絶対意味があるはずなので、本当は他にベストな解決方法があるんだと思います。良い子は真似しないでください。

 

f:id:hajimetenoweb:20150607101333p:plain

あとindex.phpのparrとかidとか知らないけど?ってお知らせが出ていたので、

<center><h1><?php echo $parr[$id]['title'];?></h1></center>を

<center><h1>To Do List</h1></center>と勝手にシンプル化してみました。

 

ということで、現在の画面はこちら。

 

 

f:id:hajimetenoweb:20150607131923p:plain

自分のブックマークを世間様に晒して少し恥ずかしいです。