質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

2回答

1893閲覧

ログイン機能が作れない。セッションの中身が消えてしまう。

kamomesaaaaan

総合スコア0

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

1クリップ

投稿2020/10/20 13:23

編集2020/10/20 16:07

前提・実現したいこと

ini

1 2[Session] 3; Handler used to store/retrieve data. 4session.save_handler = files 5 6; Argument passed to save_handler. In the case of files, this is the path 7; where data files are stored. Note: Windows users have to change this 8; variable in order to use PHP's session functions. 9; 10; As of PHP 4.0.1, you can define the path as: 11; 12; session.save_path = "N;/path" 13; 14; where N is an integer. Instead of storing all the session files in 15; /path, what this will do is use subdirectories N-levels deep, and 16; store the session data in those directories. This is useful if you 17; or your OS have problems with lots of files in one directory, and is 18; a more efficient layout for servers that handle lots of sessions. 19; 20; NOTE 1: PHP will not create this directory structure automatically. 21; You can use the script in the ext/session dir for that purpose. 22; NOTE 2: See the section on garbage collection below if you choose to 23; use subdirectories for session storage 24; 25; The file storage module creates files using mode 600 by default. 26; You can change that by using 27; 28; session.save_path = "N;MODE;/path" 29; 30; where MODE is the octal representation of the mode. Note that this 31; does not overwrite the process's umask. 32;session.save_path = "/tmp" 33 34; Whether to use cookies. 35session.use_cookies = 1 36 37;session.cookie_secure = 38 39; This option enables administrators to make their users invulnerable to 40; attacks which involve passing session ids in URLs; defaults to 0. 41; session.use_only_cookies = 1 42 43; Name of the session (used as cookie name). 44session.name = PHPSESSID 45 46; Initialize session on request startup. 47session.auto_start = 1 48 49; Lifetime in seconds of cookie or, if 0, until browser is restarted. 50session.cookie_lifetime = 0 51 52; The path for which the cookie is valid. 53session.cookie_path = / 54 55; The domain for which the cookie is valid. 56session.cookie_domain = 57 58; Whether or not to add the httpOnly flag to the cookie, which makes it inaccessible to browser scripting languages such as JavaScript. 59session.cookie_httponly = 60 61; Handler used to serialize data. php is the standard serializer of PHP. 62session.serialize_handler = php 63 64; Define the probability that the 'garbage collection' process is started 65; on every session initialization. 66; The probability is calculated by using gc_probability/gc_divisor, 67; e.g. 1/100 means there is a 1% chance that the GC process starts 68; on each request. 69 70session.gc_probability = 1 71session.gc_divisor = 1000 72 73; After this number of seconds, stored data will be seen as 'garbage' and 74; cleaned up by the garbage collection process. 75session.gc_maxlifetime = 1440 76 77; NOTE: If you are using the subdirectory option for storing session files 78; (see session.save_path above), then garbage collection does *not* 79; happen automatically. You will need to do your own garbage 80; collection through a shell script, cron entry, or some other method. 81; For example, the following script would is the equivalent of 82; setting session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes): 83; cd /path/to/sessions; find -cmin +24 | xargs rm 84 85; PHP 4.2 and less have an undocumented feature/bug that allows you to 86; to initialize a session variable in the global scope, albeit register_globals 87; is disabled. PHP 4.3 and later will warn you, if this feature is used. 88; You can disable the feature and the warning separately. At this time, 89; the warning is only displayed, if bug_compat_42 is enabled. 90 91session.bug_compat_42 = 0 92session.bug_compat_warn = 1 93 94; Check HTTP Referer to invalidate externally stored URLs containing ids. 95; HTTP_REFERER has to contain this substring for the session to be 96; considered as valid. 97session.referer_check = 98 99; How many bytes to read from the file. 100session.entropy_length = 0 101 102; Specified here to create the session id. 103session.entropy_file = 104 105;session.entropy_length = 16 106 107;session.entropy_file = /dev/urandom 108 109; Set to {nocache,private,public,} to determine HTTP caching aspects 110; or leave this empty to avoid sending anti-caching headers. 111session.cache_limiter = nocache 112 113; Document expires after n minutes. 114session.cache_expire = 180 115 116; trans sid support is disabled by default. 117; Use of trans sid may risk your users security. 118; Use this option with caution. 119; - User may send URL contains active session ID 120; to other person via. email/irc/etc. 121; - URL that contains active session ID may be stored 122; in publically accessible computer. 123; - User may access your site with the same session ID 124; always using URL stored in browser's history or bookmarks. 125session.use_trans_sid = 0 126 127; Select a hash function 128; 0: MD5 (128 bits) 129; 1: SHA-1 (160 bits) 130session.hash_function = 0 131 132; Define how many bits are stored in each character when converting 133; the binary hash data to something readable. 134; 135; 4 bits: 0-9, a-f 136; 5 bits: 0-9, a-v 137; 6 bits: 0-9, a-z, A-Z, "-", "," 138session.hash_bits_per_character = 5 139 140; The URL rewriter will look for URLs in a defined set of HTML tags. 141; form/fieldset are special; if you include them here, the rewriter will 142; add a hidden <input> field with the info which is otherwise appended 143; to URLs. If you want XHTML conformity, remove the form entry. 144; Note that all valid entries require a "=", even if no value follows. 145url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry" 146session.save_path="C:\MAMP\bin\php\sessions\" 147 148

試したこと

上記のコーディング

補足情報(FW/ツールのバージョンなど)

MAMP PHP 7.4.1
ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hentaiman

2020/10/20 13:59

ソースよりもiniに問題がありそうなのでphpinfoを晒してごらんなさい
kamomesaaaaan

2020/10/20 14:58 編集

すみません。文字数制限に引っ掛かかり、iniファイルの投稿ができませんでした。
hentaiman

2020/10/20 15:01

じゃあiniが怪しいって言うのがヒントだと思って、iniを起点に自分で調査してみてください。ノーヒントから調べるより楽でしょう。 予想が外れてたら失礼。
kamomesaaaaan

2020/10/20 15:15

ありがとうございます! 挑戦してみます!
kamomesaaaaan

2020/10/20 16:08

すみません。ちょっと自分には難しかったです... SESSIONの記述箇所を張りましたのでご助力いただければ幸いです。
kai0310

2020/10/20 16:23

> すみません。ちょっと自分には難しかったです... 具体的に何を調べたのかが分かりません。
hentaiman

2020/10/20 17:21

iniが怪しいのにphpinfoを載せろって言ったのは、webサーバー通った後のphpの設定が見たい為です。phpinfo(をwebで表示した結果)を貼ってください。※もちろん、ini以外では絶対に何も変更して無い&把握していない個所で変更されてしまっている可能性も100%無いと言い切れるのならiniでも構いませんが
guest

回答2

0

;session.cookie_secure =

有効な場合、httpsの通信でしかセッションは使えない

;session.save_path = "/tmp"

指定したディレクトリへの書き込み権限が無ければセッションの保存はできない

とりあえず引っ掛かりそうな点だけですが、これらの値はiniファイルではなくブラウザからphpinfo()で確認しましょう。

投稿2020/10/20 17:35

hentaiman

総合スコア6415

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

全体のデバッグをするのはteratailの趣旨に沿ったものでは無いと思うので、私ならこうしてデバッグを進めるという観点で回答します。

そもそもセッション機能が動いているか確認

ごく簡単なコードでセッション機能が動作しているかを確認してみて、ダメならブラウザ設定、iniやファイルフォーマット、その他出力等に問題が無いかを確認する

こんなコードを書いて、アクセスするごとに$_SESSION['test']の値が増えればセッションは正常に動いている。
増えなければ何らかの問題でセッションが正常に動作していないので、その原因を探る

PHP

1<?php 2//エラー表示は必ず行う。出来ればiniで設定した方が良い 3ini_set('display_errors',1); 4ini_set('error_reporting',E_ALL); 5 6session_start(); 7if(isset($_SESSION['test']) === false){ 8 $_SESSION['test'] = 0; 9} 10$_SESSION['test']++; 11var_dump($_SESSION); 12

セッションが正常に動いていたら

各ファイルの頭に

PHP

1<?php 2//エラー表示は必ず行う。出来ればiniで設定した方が良い 3ini_set('display_errors',1); 4ini_set('error_reporting',E_ALL); 5 6session_start(); 7var_dump($_SESSION);//出来ればxdebugでブレークポイントを仕掛けて確認する `php xdebug ブレークポイント`辺りで調べてください。 8

として、$_SESSIONの中身がどのタイミングでクリアされてしまっている or 想定外の状態になっているかを確認する。
問題が発生するタイミングが確認出来たら、そのファイルに何か問題がある可能性が高いので、ファイルの文字コードやBOMの有無、<?phpより前に改行やHTMLコードが挟まっていないか等を確認する。

投稿2020/10/20 16:48

tanat

総合スコア18713

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問