php.iniファイルの確認と修正

広告

PHPに関する設定は php.ini ファイルを使って設定しますが、現在 php.ini ファイルで設定されている内容を確認し、一部の設定については修正を行っておきます。設定を行う対象の php.ini ファイルの場所については前のページをご参照下さい。

ファイルの修正も行いますので心配であれば修正前のファイルをコピーしバックアップを取っておいて下さい。

p2-1

それでは順に設定項目を確認していきます。

1.セーフモード
2.マジッククオート
3.インクルードパス
4.拡張モジュール
5.mbstring
6.php.iniの変更内容を反映

「safe_mode」で検索して下さい。

; Safe Mode
; http://php.net/safe-mode
safe_mode=Off

PHPではセーフモードと呼ばれる設定があります。セーフモードは共有サーバなどの環境でPHPを動作させる時にセキュリティを高めるために使用されるものです。セーフモードをオンにすることでいくつかの関数の利用に制限がかかります。

デフォルトではOffになっていますが、セーフモードをオンにしなければならない時は「Off」を「On」に変更して下さい。今回は変更はせず「Off」にしてあります。

「magic_quotes_gpc」で検索して下さい。

; Magic quotes are a preprocessing feature of PHP where PHP will attempt to
; escape any character sequences in GET, POST, COOKIE and ENV data which might
; otherwise corrupt data being placed in resources such as databases before
; making that data available to you. Because of character encoding issues and
; non-standard SQL implementations across many databases, it's not currently
; possible for this feature to be 100% accurate. PHP's default behavior is to
; enable the feature. We strongly recommend you use the escaping mechanisms
; designed specifically for the database your using instead of relying on this
; feature. Also note, this feature has been deprecated as of PHP 5.3.0 and is
; scheduled for removal in PHP 6.
; Default Value: On
; Development Value: Off
; Production Value: Off
; http://php.net/magic-quotes-gpc
magic_quotes_gpc=Off

; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc.
; http://php.net/magic-quotes-runtime
magic_quotes_runtime=Off

; Use Sybase-style magic quotes (escape ' with '' instead of \').
; http://php.net/magic-quotes-sybase
magic_quotes_sybase=Off

マジッククオートとは自動的にエスケープ処理を行うかどうかの設定です。有効にすることでエスケープが必要な時に自動でエスケープ処理を行ってくれますが、機能的に問題がある部分があり自動でエスケープされると問題となることがあります。その為、マジッククオートは無効にしておき、エスケープ処理はプログラムの中で明示的に記述することが推奨されています。

デフォルトで全て「Off」となっていましたので特に変更はしていません。

「include_path」で検索して下さい。

; UNIX: "/path1:/path2"
;include_path = ".:/php/includes"
;
; Windows: "\path1;\path2"
include_path=".;C:\xampp\php\PEAR"
;
; PHP's default setting for include_path is ".;/path/to/php/pear"
; http://php.net/include-path

インクルードパスとはPHPが記述されたファイルから外部のファイルをインクルードする時に、インクルードするファイルを設置するディレクトリを設定します。ここで設定されたディレクトリを基点として外部のファイルを検索します。

インクルードパスには複数のパスを記述でき、セミコロン(;)で区切って続けて記述できます。デフォルトの設定ではではカレントディレクトリを表す「.」とXAMPPをインストールしたディレクトリに合わせて「C:\xampp\php\PEAR」の2つのパスが自動的に設定されています。

実際にこのディレクトリを見てみます。

p2-2

デフォルトで多くのファイルが既に格納されていることが確認できます。

「extension_dir」で検索して下さい。

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir="C:\xampp\php\ext"

拡張モジュールのディレクトリにはPHPの起動時に読み込まれる拡張モジュールを設置するディレクトリを設定します。デフォルトの設定ではXAMPPをインストールしたディレクトリに合わせて「C:\xampp\php\ext」となっています。実際にこのディレクトリを見てみます。

p2-3

多くのモジュールが配置されていることが確認できます。

そして実際にどのモジュールをPHPに読み込むのかについて「extension」で指定します。下記は php.ini ファイルに記述されている「extension」の一部です。

extension=php_bz2.dll
extension=php_curl.dll
extension=php_mbstring.dll
extension=php_exif.dll
;extension=php_fileinfo.dll
extension=php_gd2.dll
extension=php_gettext.dll
;extension=php_gmp.dll
;extension=php_intl.dll
;extension=php_imap.dll

読み込むモジュールを「extension=モジュール名」と記述します。行の先頭に「;」が付いている行はコメントとして扱われますので「;」が付いているものは現在有効になっていないものです。今後必要になったモジュールを読み込むには先頭の「;」を削除することでモジュールが読み込まれます。

日本語などのマルチバイト文字を扱うにはmbstringの設定が必要です。まずはmbstring用の拡張モジュールを有効にします。次の行を検索して下さい。

extension=php_mbstring.dll

デフォルトの設定でmbstring拡張モジュールを読み込むようになっています。もし先頭に「;」が付いていた場合は削除して下さい。

続いて設定を行います。「[mbstring]」で検索して下さい。

[mbstring]
; language for internal character representation.
; http://php.net/mbstring.language
;mbstring.language = Japanese

; internal/script encoding.
; Some encoding cannot work as internal encoding.
; (e.g. SJIS, BIG5, ISO-2022-*)
; http://php.net/mbstring.internal-encoding
;mbstring.internal_encoding = EUC-JP

; http input encoding.
; http://php.net/mbstring.http-input
;mbstring.http_input = auto

; http output encoding. mb_output_handler must be
; registered as output buffer to function
; http://php.net/mbstring.http-output
;mbstring.http_output = SJIS

; enable automatic encoding translation according to
; mbstring.internal_encoding setting. Input chars are
; converted to internal encoding by setting this to On.
; Note: Do _not_ use automatic encoding translation for
;       portable libs/applications.
; http://php.net/mbstring.encoding-translation
;mbstring.encoding_translation = Off

; automatic encoding detection order.
; auto means
; http://php.net/mbstring.detect-order
;mbstring.detect_order = auto

; substitute_character used when character cannot be converted
; one from another
; http://php.net/mbstring.substitute-character
;mbstring.substitute_character = none;

; overload(replace) single byte functions by mbstring functions.
; mail(), ereg(), etc are overloaded by mb_send_mail(), mb_ereg(),
; etc. Possible values are 0,1,2,4 or combination of them.
; For example, 7 for overload everything.
; 0: No overload
; 1: Overload mail() function
; 2: Overload str*() functions
; 4: Overload ereg*() functions
; http://php.net/mbstring.func-overload
;mbstring.func_overload = 0

; enable strict encoding detection.
;mbstring.strict_detection = Off

; This directive specifies the regex pattern of content types for which mb_output_handler()
; is activated.
; Default: mbstring.http_output_conv_mimetype=^(text/|application/xhtml\+xml)
;mbstring.http_output_conv_mimetype=

; Allows to set script encoding. Only affects if PHP is compiled with --enable-zend-multibyte
; Default: ""
;mbstring.script_encoding=

各項目の先頭の「;」を削除した上で順に設定を変更していきます。設定方法はご利用になっている環境によって異なりますので何が正解というものはありません。下記では私が設定している例をご紹介します。

「mbstring.internal_encoding」はマルチバイト文字列関数(mbstring)のデフォルトエンコードとなります。変換元の文字コードが指定されなかった場合、ここで指定した文字コードが使用されます。基本的には文字コードを指定しますので設定は不要ですが、ここでは基本の文字コードとして使用予定のUTF-8を指定しておきます。

; language for internal character representation.
; http://php.net/mbstring.language
mbstring.language = Japanese

; internal/script encoding.
; Some encoding cannot work as internal encoding.
; (e.g. SJIS, BIG5, ISO-2022-*)
; http://php.net/mbstring.internal-encoding
mbstring.internal_encoding = UTF-8

「mbstring.http_input」と「mbstring.http_output」はHTTP通信の時のインプットとアウトプットの文字コード変換に関するパラメータです。自動で変換しないように「mbstring.encoding_translation」は「Off」を指定しています。

; http input encoding.
; http://php.net/mbstring.http-input
mbstring.http_input = pass

; http output encoding. mb_output_handler must be
; registered as output buffer to function
; http://php.net/mbstring.http-output
mbstring.http_output = pass

; enable automatic encoding translation according to
; mbstring.internal_encoding setting. Input chars are
; converted to internal encoding by setting this to On.
; Note: Do _not_ use automatic encoding translation for
;       portable libs/applications.
; http://php.net/mbstring.encoding-translation
mbstring.encoding_translation = Off

「mbstring.detect_order」は文字コードの自動判別を行う時にどの文字コードから順に確認していくのかを指定します。「auto」に設定するとどの文字コードから判別するか分かりませんので明示的に指定してあります。

; automatic encoding detection order.
; auto means
; http://php.net/mbstring.detect-order
mbstring.detect_order = UTF-8,SJIS,EUC-JP,JIS,ASCII

「mbstring.substitute_character」は無効な文字があった場合に代わりに表示する文字を指定します。今回はデフォルトの値のまま無効な文字は何も表示しない設定の「none」としておきます。

; substitute_character used when character cannot be converted
; one from another
; http://php.net/mbstring.substitute-character
mbstring.substitute_character = none

「mbstring.func_overload」はシングルバイト対応の関数をマルチバイト対応の関数でオーバーロードするどうかの設定です。自動でオーバーロードされると予期せぬ不具合が発生することもありますのでオーバーロードしない「0」としておきます。

; overload(replace) single byte functions by mbstring functions.
; mail(), ereg(), etc are overloaded by mb_send_mail(), mb_ereg(),
; etc. Possible values are 0,1,2,4 or combination of them.
; For example, 7 for overload everything.
; 0: No overload
; 1: Overload mail() function
; 2: Overload str*() functions
; 4: Overload ereg*() functions
; http://php.net/mbstring.func-overload
mbstring.func_overload = 0

「mbstring.strict_detection」は文字コードの自動判別を厳密に行うかどうかの設定です。それぞれメリットデメリットがありますが今回はOffのままにしてあります。

; enable strict encoding detect
mbstring.strict_detection = Off

「mbstring.http_output_conv_mimetype」と「mbstring.script_encoding」についてはよく分かっていないので、今回はコメントのままとしておきます。

; This directive specifies the regex pattern of content types for which mb_output_handler()
; is activated.
; Default: mbstring.http_output_conv_mimetype=^(text/|application/xhtml\+xml)
;mbstring.http_output_conv_mimetype=

; Allows to set script encoding. Only affects if PHP is compiled with --enable-zend-multibyte
; Default: ""
;mbstring.script_encoding=

mbstring に関する設定は以上です。

php.iniファイルの必要最小限の設定変更は以上となります。PHPからMySQLなどのデータベースを使用する場合はMySQLに関するモジュールの読み込みや設定などが必要となりますが、それは別のページで解説します。

なお変更したphp.iniを有効とするにはApacheを再起動する必要があります。再起動後にphpinfo関数で出力される内容を確認して下さい。(確認方法は「phpinfoによる設定の確認とphp.iniファイルの位置」を参照して下さい)。

p2-4

「mbstring」のブロックを見て下さい。設定した内容が反映されていれば設定変更は完了です。

( Written by Tatsuo Ikura )