前提・実現したいこと
Codeigntierで作られた既存のシステムをローカルで動くようにしたい
現在の状況としまして、Amazon Linux上で動いているサービスの追加開発をすることになっており、ローカル開発環境を立ち上げたのですが、セッションを使用した部分が動きません。
ローカル環境
Docker for Macを使用して開発環境を構築しています。
・nginx 1.14
・php-fpm 7.21
・MySQL 5.7
をdocker-composeを使って管理しています。
わかっていること
■動作確認済みの物
・通常のPHPセッションは動作している(nginx上のお話で、codeigniterシステム上で書くと動作しない)
・ブラウザのCookieも保存されている
・同じソースコードで本番環境ならば動作する
・nginxの /tmp
に ci_session****
ファイルは存在する
動作したコード
<?php $_SESSION["_test"]++; echo $_SESSION["_test"]; ?>
こちらを実行すると更新ごとに数字が増えていく
動作しないソースコード
counter.php
1class Counter extends CI_Controller{ 2function index() 3{ 4$this->load->library('session'); 5 6if(! $this->session->userdata('count')){ 7$this->session->set_userdata('count',1); 8}else{ 9$count=$this->session->userdata('count'); 10$count++; 11$this->session->set_userdata('count',$count); 12} 13echo 'アクセス回数は'.$this->session->userdata('count').'です<br>'; 14echo 'セッションID:'.$this->session->userdata('session_id').'<br>'; 15echo 'IPアドレス:'.$this->session->userdata('ip_address').'<br>'; 16echo 'ユーザーエージェント:'.$this->session->userdata('user_agent').'<br>'; 17echo '最終アクセス:'.$this->session->userdata('last_activity'); 18} 19}
こちらのコードを実行してもアクセス回数の表示が1のままです。
試したこと
ブラウザ切り替え(chrome,safari,firefox)
MAMPでの開発環境立ち上げ
どちらも同じ結果だったため、php-fpmか nginxの設定が怪しいのですが、codeigniterを利用するにあたり特殊な設定が必要なのでしょうか?
/etc/nginx/conf.d/default.conf
server { listen 80; index index.php index.html; server_name localhost; include mime.types; root /var/www/html; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ .php$ { root /var/www/html; fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass php:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param CI_ENV test; } location ~* .(ico|css|js|gif|jpe?g|png)(?[0-9]+)?$ { root /var/www/html; expires 0; access_log off; } }
php.ini(省略)
[Session] ; Handler used to store/retrieve data. ; http://php.net/session.save-handler session.save_handler = files ; Argument passed to save_handler. In the case of files, this is the path ; where data files are stored. Note: Windows users have to change this ; variable in order to use PHP's session functions. ; ; The path can be defined as: ; ; session.save_path = "N;/path" ; ; where N is an integer. Instead of storing all the session files in ; /path, what this will do is use subdirectories N-levels deep, and ; store the session data in those directories. This is useful if ; your OS has problems with many files in one directory, and is ; a more efficient layout for servers that handle many sessions. ; ; NOTE 1: PHP will not create this directory structure automatically. ; You can use the script in the ext/session dir for that purpose. ; NOTE 2: See the section on garbage collection below if you choose to ; use subdirectories for session storage ; ; The file storage module creates files using mode 600 by default. ; You can change that by using ; ; session.save_path = "N;MODE;/path" ; ; where MODE is the octal representation of the mode. Note that this ; does not overwrite the process's umask. ; http://php.net/session.save-path ; RPM note : session directory must be owned by process owner ; for mod_php, see /etc/httpd/conf.d/php.conf ; for php-fpm, see /etc/php-fpm.d/*conf session.save_path = "/tmp" ; Whether to use strict session mode. ; Strict session mode does not accept uninitialized session ID and regenerate ; session ID if browser sends uninitialized session ID. Strict mode protects ; applications from session fixation via session adoption vulnerability. It is ; disabled by default for maximum compatibility, but enabling it is encouraged. ; https://wiki.php.net/rfc/strict_sessions session.use_strict_mode = 0 ; Whether to use cookies. ; http://php.net/session.use-cookies session.use_cookies = 1 ; http://php.net/session.cookie-secure ;session.cookie_secure = ; This option forces PHP to fetch and use a cookie for storing and maintaining ; the session id. We encourage this operation as it's very helpful in combating ; session hijacking when not specifying and managing your own session id. It is ; not the be-all and end-all of session hijacking defense, but it's a good start. ; http://php.net/session.use-only-cookies session.use_only_cookies = 1 ; Name of the session (used as cookie name). ; http://php.net/session.name session.name = PHPSESSID ; Initialize session on request startup. ; http://php.net/session.auto-start session.auto_start = 0 ; Lifetime in seconds of cookie or, if 0, until browser is restarted. ; http://php.net/session.cookie-lifetime session.cookie_lifetime = 360
はっきりした原因がわからずつまずいているため、よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー