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

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

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

こんにちは。To doリスト機能の付いたSNSサイトを作るべく、壁にぶち当たりまくっているW子です。(そんなサイトもうあるなんて言わないで。涙)

会員を募る以上、セキュリティ的に安心なログイン&新規登録画面を作りたいと思い、試行錯誤しております。

 

そもそも初心者の私が本当にセキュリティ面でしっかりしたコードを書くのは、まだ自信がない。(いつかは出来るようになるのかな。遠い目)

そんな私に今ある選択肢は、本やwebサイトを参考にする、ということ。

 

ただ購入した本に載っているとおりだとセキュリティが甘いし、ドットインストールのチュートリアルも機能的に足りないし、同じようにやってもエラーが出たり。

そんな中一番機能が理想的で、コードを公開していたのがこちら。

英語のサイトでした。。。涙。

プログラミング用語もわからないのに英語なんて。。。(*´Д`)ムリ

 

しかし、コードが公開されているのにお借りしない訳にはいかない!

自分で一から作るのはもっと(*´Д`)ムリということで、ざっくりと日本語に訳しながら、ログイン機能を作っていきたいと思います。

青字部分は私の意訳ですので、参考にしてくださる際は、怪しみながらご覧ください。本家のサイトと並行してご覧いただけると有り難いです。

 

はじめに

We all get new ideas every day. With the idea, we build more and more sites. Mostly, the sites would have a login system, right ? So, when we create new projects (sites) every time, we will have to create login systems for each one. I do the same thing and I'm tired of creating the same thing over and over again. So, I decided to create a login system that can be integrated to any PHP powered sites.

毎度毎度新しいサイト作るたびにログイン機能作るの面倒だから、どのサイトでも使えるログイン機能つくってみた。

つくってみた後公開までしてくださっているなんて、神。このログイン機能のまとめファイルをダウンロードして、自分のサイト用にカスタマイズしていきます。

 機能

  • Login & Registering ログイン&登録
  • Remember Me or not feature このIDを記憶する
  • Secure (uses PDO & hashes) PDOとハッシュを使った安全さ
  • "Forgot Password ?" feature パスワード忘れた?機能
  • Change password functionality パスワード変更
  • Custom fields for users' details storing table カスタマイズ出来るユーザー詳細入力テーブル
  • Extra Tools such as E-Mail validation and Random String generator 他にも機能あるよ。eメール承認やランダムなパスワード発生とか
  • Easily get user details 簡単にユーザーの情報ゲット
  • Update users' details easily アップデートするのも簡単
  • Auto redirection based on the login status of user 自動redirectionも
  • Show time since user joined ユーザー登録された時間も出たり
  • Protection from Brute Force Attacks ブルートフォースアッタクからも身を守れたり
  • Disable login attempts for fixed time after failed 5 login attempts 5回ログインできなかったら一定期間ログインできなくしたり
  • Configure how cookie should be created (April 6, 2015) cookieも設定おk

 盛りだくさんな機能( ゚Д゚)この機能を全部自分で考えるのは大変だから有り難い。

ダウンロード&インストール

Download & Install

You first have to download the class file and put it in the root of your project directory or wherever you like. You can download/see the class file here. Paste the code in a file called "class.logsys.php".

とりあえずファイルダウンロードして、公開ディレクトリのルートの中のどこかにいいれるか適当に入れて。ここから見れるよ。class.logys.phpにコピペして。

ダウンロードボタンからダウンロードするとZipファイルのリンクに行き、class.logys.php以外のファイルもまとめてダウンロードできます。

一応ダウンロードファイルのリンク場所貼っておきます。

Download - Subin's Blog Demos

で、hereの部分からはgithubにリンクが飛ぶのですが、そこには最新のコードが載っているのでclass.logys.phpを開いてコードをまるっと書き換える。

githubのリンク先はこちら。

logSys/class.logsys.php at master · subins2000/logSys · GitHub

私の見た段階では670段目当たりのコードが微妙に変わっているようだったので、githubのコードをコピペしました。

導入

Introduction

For this tutorial, we use the table as "users" (which is changable). You can set or view the configuration with this variable :

テーブルはusersっていう名前にするか勝手に変えて。この下の可変何か(関数?変数?メソッド?)で設定を決めたり見たり出来るよ。

\Fr\LS::$config;

The values available for configuration can be seen in this post on another section.

この結果はいろんなとこで見れるよ。(なんのこっちゃい( ;∀;)?)

The user can either use E-Mail or username for logging in. Only condition is that the email column of the users table should be "email" and not anything else. It's better if you use this Database structure :

ユーザーはログインする時eメールからでもユーザーネームからでもどっちでもログインできるよ。でもデータベースにテーブル作る時は絶対email用のコラムの名前をemailにしてね。データベースはこんな感じで。

id | username | email | password | password_salt | name | created | attempt

The columns "id", "username", "password", "password_salt" is absolutely required for the basic setup of logSys. Other columns are for optional features like "Login with E-Mail & Username", "Time Since User Joined" and "Brute Force Protection".

"id", "username", "password", "password_salt"だけは絶対入れて。他は適当に。

You can easily set up the Database table by reading the "Database Table Setup" section of this tutorial. Login by username and E-Mail can be enabled by setting "email_login" to boolean TRUE.

ま、わかんなかったら"Database Table Setup"でも読んでみて。とりあえず、booleanていう論理型使ってTRUEかFALSEどっちなの?的なコード書いてるから、その中で"email_login"がTRUEであれば、ユーザーネームとemailでログインできるし。

Return values of logSys functions are booleans. So use === operator instead of ==. logSys creates and use only 2 cookies. They are "logSyslogin" and "logSysrememberMe".

logSys関数のバリューの返り値もbooleanってことで。演算子(operator)は===を使ってね。==はだめだから。logSys関数はcookie2つしか使わないから。"logSyslogin" と "logSysrememberMe"やで。

In this post, we assume that you use "custom configuration" and not the other method to configure. Therefore there will be no including "class.logsys.php" file, but "config.php". Don't know what I'm talking about ? You haven't read this.

このブログでは設定をカスタマイズしてもらうから、最終的に今の"class.logsys.php"ファイルはなしにして、"config.php"ファイルを作ってくから。わからなかったらこれでも読んで。(わからないので、後で読みます。)

Make sure you read the next portion carefully and change the config values accordingly.

諸々気を付けてやれ。 

導入部分で既にわからなかったから、とりあえずファイルをダウンロード&解凍して、htdocs内にtesttodoとファイルの名前を変えて入れてみた。

今のところ画面はこんな感じになったよ。

f:id:hajimetenoweb:20150607101333p:plain

エラー出てんで( ゚Д゚)。なんでやねん?

ちなみにテーブルは後で出てくるからまだ作ってないで。

 

長くなってしまったので、今回はまず導入編でした。次回は設定等々に進めたら、、、いいな。