質問編集履歴

10

一部修正

2019/07/25 13:45

投稿

nosonosolife
nosonosolife

スコア42

test CHANGED
File without changes
test CHANGED
@@ -100,6 +100,8 @@
100
100
 
101
101
  ```PHP
102
102
 
103
+ <?php
104
+
103
105
  session_start();
104
106
 
105
107
  if($_SERVER["REQUEST_METHOD"] == "POST"){
@@ -120,7 +122,7 @@
120
122
 
121
123
  if(isset($_POST['Penname'])){
122
124
 
123
- $json = file_get_contents('userlog.json');
125
+ $json = file_get_contents('../data/log/userlog.json');
124
126
 
125
127
  $records = json_decode($json, true);
126
128
 
@@ -152,26 +154,28 @@
152
154
 
153
155
  }else{
154
156
 
157
+ foreach ($records as $key => $value) {
158
+
159
+ $nowtime = $Time;
160
+
161
+ $lasttime = $value['time'];
162
+
163
+ if(($nowtime - $lasttime) > 60){
164
+
165
+ unset($records[$key]);
166
+
167
+ }else{
168
+
155
- foreach ($result_index as $index) {
169
+ foreach ($result_index as $index) {
156
-
170
+
157
- $records[$index]['time'] = $Time;
171
+ $records[$index]['time'] = $Time;
172
+
173
+ }
174
+
175
+ }
158
176
 
159
177
  }
160
178
 
161
- foreach ($records as $value) {
162
-
163
- $nowtime = $Time;
164
-
165
- $lasttime = $value['time'];
166
-
167
- if(($nowtime - $lasttime) > 60){
168
-
169
- unset($value);
170
-
171
- }
172
-
173
- }
174
-
175
179
  }
176
180
 
177
181
 
@@ -188,7 +192,7 @@
188
192
 
189
193
  if($_SERVER["REQUEST_METHOD"] == "GET"){
190
194
 
191
- $json = file_get_contents('userlog.json');
195
+ $json = file_get_contents('../data/log/userlog.json');
192
196
 
193
197
  $records = json_decode($json, true);
194
198
 
@@ -206,6 +210,8 @@
206
210
 
207
211
  }
208
212
 
213
+ ?>
214
+
209
215
  ```
210
216
 
211
217
  PHPでは、リクエストがPOSTの場合は、ユーザー別にセッションIDや最終アクセス時間などをファイルに記録します。

9

一部修正

2019/07/25 13:45

投稿

nosonosolife
nosonosolife

スコア42

test CHANGED
File without changes
test CHANGED
@@ -166,7 +166,7 @@
166
166
 
167
167
  if(($nowtime - $lasttime) > 60){
168
168
 
169
- unset($records[$index]);
169
+ unset($value);
170
170
 
171
171
  }
172
172
 

8

一部修正

2019/07/25 13:25

投稿

nosonosolife
nosonosolife

スコア42

test CHANGED
File without changes
test CHANGED
@@ -132,8 +132,6 @@
132
132
 
133
133
  $Time = time();
134
134
 
135
- $_SESSION['user_name'] = $Penname;
136
-
137
135
 
138
136
 
139
137
  $result_index = array_keys(array_column($records, 'id'), $Id);

7

一部修正

2019/07/25 13:22

投稿

nosonosolife
nosonosolife

スコア42

test CHANGED
File without changes
test CHANGED
@@ -212,7 +212,7 @@
212
212
 
213
213
  PHPでは、リクエストがPOSTの場合は、ユーザー別にセッションIDや最終アクセス時間などをファイルに記録します。
214
214
 
215
- 最終アクセス時間から1分を過ぎるとファイルからデータを削除します。
215
+ 最終アクセス時間1分を過ぎても変更されない場合、ファイルからデータを削除します。
216
216
 
217
217
  リクエストがGETの場合は、現在の入室者数を返します。
218
218
 

6

一部修正

2019/07/25 12:49

投稿

nosonosolife
nosonosolife

スコア42

test CHANGED
File without changes
test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
  ==2019/07/25 追記==
28
28
 
29
- mikkameさん、mikkameさんから頂いたヒントから、自分なりに考えてコードを組んでみました。
29
+ mikkameさん、mts10806さんから頂いたヒントから、自分なりに考えてコードを組んでみました。
30
30
 
31
31
  ```php
32
32
 

5

コードを追加

2019/07/25 12:25

投稿

nosonosolife
nosonosolife

スコア42

test CHANGED
File without changes
test CHANGED
@@ -21,3 +21,199 @@
21
21
  これをなんとか解決したいのですが、どうすればいいでしょうか?
22
22
 
23
23
  ご提示よろしくお願いいたします。
24
+
25
+
26
+
27
+ ==2019/07/25 追記==
28
+
29
+ mikkameさん、mikkameさんから頂いたヒントから、自分なりに考えてコードを組んでみました。
30
+
31
+ ```php
32
+
33
+ session_id(sha1(uniqid(microtime())));
34
+
35
+ ```
36
+
37
+ まずチャットにアクセスした際に、セッションIDを作成しておきます。
38
+
39
+ ```javasctipt
40
+
41
+ function chatOnline(){
42
+
43
+ $.ajax({
44
+
45
+ type: "post",
46
+
47
+ url: "php/usercount.php",
48
+
49
+ data: { Penname: userName },
50
+
51
+ timeout: 10000, // 単位はミリ秒
52
+
53
+
54
+
55
+ success: function(result, textStatus, xhr) {
56
+
57
+ onlineTimer = setTimeout( "chatOnline()", 10000 );
58
+
59
+ },
60
+
61
+ // 通信失敗時の処理
62
+
63
+ error: function(xhr, textStatus, error) {
64
+
65
+
66
+
67
+ }
68
+
69
+ });
70
+
71
+ }
72
+
73
+ $.ajax({
74
+
75
+ url: 'php/usercount.php',
76
+
77
+ type: 'GET',
78
+
79
+ dataType: 'json',
80
+
81
+ data: {ut: (new Date().getTime())}
82
+
83
+ })
84
+
85
+ .done(function(result) {
86
+
87
+ if(result){
88
+
89
+ $("hoge").html('現在の入室者数:' + result[0].usercount + '人');
90
+
91
+ }
92
+
93
+ });
94
+
95
+ ```
96
+
97
+ javasctiptで10秒ごとにPHPにデータを送信します。
98
+
99
+
100
+
101
+ ```PHP
102
+
103
+ session_start();
104
+
105
+ if($_SERVER["REQUEST_METHOD"] == "POST"){
106
+
107
+ mb_language("uni");
108
+
109
+ mb_internal_encoding("utf-8"); //内部文字コードを変更
110
+
111
+ mb_http_input("auto");
112
+
113
+ mb_http_output("utf-8");
114
+
115
+
116
+
117
+ date_default_timezone_set('Asia/Tokyo');
118
+
119
+
120
+
121
+ if(isset($_POST['Penname'])){
122
+
123
+ $json = file_get_contents('userlog.json');
124
+
125
+ $records = json_decode($json, true);
126
+
127
+
128
+
129
+ $Id = session_id();
130
+
131
+ $Penname = htmlspecialchars($_POST["Penname"]);
132
+
133
+ $Time = time();
134
+
135
+ $_SESSION['user_name'] = $Penname;
136
+
137
+
138
+
139
+ $result_index = array_keys(array_column($records, 'id'), $Id);
140
+
141
+
142
+
143
+ if($result_index == null){
144
+
145
+ $records[] = [
146
+
147
+ 'id' => $Id,
148
+
149
+ 'time' => $Time,
150
+
151
+ 'penname' => $Penname,
152
+
153
+ ];
154
+
155
+ }else{
156
+
157
+ foreach ($result_index as $index) {
158
+
159
+ $records[$index]['time'] = $Time;
160
+
161
+ }
162
+
163
+ foreach ($records as $value) {
164
+
165
+ $nowtime = $Time;
166
+
167
+ $lasttime = $value['time'];
168
+
169
+ if(($nowtime - $lasttime) > 60){
170
+
171
+ unset($records[$index]);
172
+
173
+ }
174
+
175
+ }
176
+
177
+ }
178
+
179
+
180
+
181
+ $out_json = json_encode($records);
182
+
183
+
184
+
185
+ $result = file_put_contents("../data/log/userlog.json", $out_json, LOCK_EX);
186
+
187
+ }
188
+
189
+ }
190
+
191
+ if($_SERVER["REQUEST_METHOD"] == "GET"){
192
+
193
+ $json = file_get_contents('userlog.json');
194
+
195
+ $records = json_decode($json, true);
196
+
197
+ $Num = count($records);
198
+
199
+ $usercount[] = [
200
+
201
+ 'usercount' => $Num,
202
+
203
+ ];
204
+
205
+
206
+
207
+ echo json_encode($usercount);
208
+
209
+ }
210
+
211
+ ```
212
+
213
+ PHPでは、リクエストがPOSTの場合は、ユーザー別にセッションIDや最終アクセス時間などをファイルに記録します。
214
+
215
+ 最終アクセス時間から1分を過ぎるとファイルからデータを削除します。
216
+
217
+ リクエストがGETの場合は、現在の入室者数を返します。
218
+
219
+ 以上です。

4

ソースコード除去

2019/07/25 12:25

投稿

nosonosolife
nosonosolife

スコア42

test CHANGED
File without changes
test CHANGED
@@ -21,219 +21,3 @@
21
21
  これをなんとか解決したいのですが、どうすればいいでしょうか?
22
22
 
23
23
  ご提示よろしくお願いいたします。
24
-
25
-
26
-
27
- == 2019.07.24 追記 ==
28
-
29
- 実際のソースコードを記載します。
30
-
31
-
32
-
33
- ```JavaScript
34
-
35
- function chatOut(){
36
-
37
- if (userState) {
38
-
39
- var ua = window.navigator.userAgent.toLowerCase();
40
-
41
- if ( ua.indexOf("msie") != -1 ){
42
-
43
- $.ajax({
44
-
45
- type: "post",
46
-
47
- url: "userinout.php",
48
-
49
- data: { Name: userName , State: 'out' },
50
-
51
- timeout: 10000, // 単位はミリ秒
52
-
53
-
54
-
55
- success: function(result, textStatus, xhr) {
56
-
57
- userState = false;
58
-
59
- },
60
-
61
- // 通信失敗時の処理
62
-
63
- error: function(xhr, textStatus, error) {
64
-
65
-
66
-
67
- }
68
-
69
- });
70
-
71
- }else{
72
-
73
- $.ajax({
74
-
75
- type: "post",
76
-
77
- url: "userinout.php",
78
-
79
- data: { Name: userName , State: 'out' },
80
-
81
- timeout: 10000, // 単位はミリ秒
82
-
83
- async: false,
84
-
85
-
86
-
87
- success: function(result, textStatus, xhr) {
88
-
89
- userState = false;
90
-
91
- },
92
-
93
- // 通信失敗時の処理
94
-
95
- error: function(xhr, textStatus, error) {
96
-
97
-
98
-
99
- }
100
-
101
- });
102
-
103
- }
104
-
105
- }
106
-
107
- }
108
-
109
-
110
-
111
- var ua = navigator.userAgent;
112
-
113
- if (ua.indexOf('iPhone') > 0 || ua.indexOf('iPad') ||
114
-
115
- ua.indexOf('Android') > 0 && ua.indexOf('Mobile') > 0 ||
116
-
117
- ua.indexOf('Android') > 0 ) {
118
-
119
- $(window).bind('pagehide',function(){
120
-
121
- chatOut();
122
-
123
- });
124
-
125
- }else{
126
-
127
- $(window).on("beforeunload", function() {
128
-
129
- chatOut();
130
-
131
- });
132
-
133
- }
134
-
135
- ```
136
-
137
- ```php
138
-
139
- <?php
140
-
141
- session_start();
142
-
143
- if($_SERVER["REQUEST_METHOD"] == "POST"){
144
-
145
- mb_language("uni");
146
-
147
- mb_internal_encoding("utf-8"); //内部文字コードを変更
148
-
149
- mb_http_input("auto");
150
-
151
- mb_http_output("utf-8");
152
-
153
-
154
-
155
- date_default_timezone_set('Asia/Tokyo');
156
-
157
-
158
-
159
- if(isset($_POST['Name']) && isset($_POST['State'])){
160
-
161
- $json = file_get_contents('inoutlog.json');
162
-
163
- $records = json_decode($json, true);
164
-
165
- $State = htmlspecialchars($_POST['State']);
166
-
167
- $Num = count($records);
168
-
169
- $Num++;
170
-
171
-
172
-
173
- $Name = htmlspecialchars($_POST["Name"]);
174
-
175
- $Date = date("Y/m/d H:i:s");
176
-
177
- $_SESSION['user_name'] = $Name;
178
-
179
-
180
-
181
- $records[] = [
182
-
183
- 'num' => $Num,
184
-
185
- 'date' => $Date,
186
-
187
- 'name' => $Name,
188
-
189
- 'state' => $State,
190
-
191
- ];
192
-
193
-
194
-
195
- $out_json = json_encode($records);
196
-
197
-
198
-
199
- $result = file_put_contents("inoutlog.json", $out_json, LOCK_EX);
200
-
201
-
202
-
203
- $countjson = file_get_contents('usercount.json');
204
-
205
- $count = json_decode($countjson, true);
206
-
207
- $countNum = $count['usercount'];
208
-
209
-
210
-
211
- if($_POST['State'] == "in"){
212
-
213
- $countNum++;
214
-
215
- }else{
216
-
217
- $countNum--;
218
-
219
- }
220
-
221
-
222
-
223
- $count['usercount'] = $countNum;
224
-
225
-
226
-
227
- $out_countjson = json_encode($count);
228
-
229
-
230
-
231
- $result = file_put_contents("usercount.json", $out_countjson, LOCK_EX);
232
-
233
- }
234
-
235
- }
236
-
237
- ?>
238
-
239
- ```

3

2019/07/24 18:01

投稿

nosonosolife
nosonosolife

スコア42

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- Server-Sent EventsとJavaScriptとPHPでリアルタイムチャットを製作しているのですが、どうやって現在の入室者数をカウントするかで嵌っています。
1
+ JavaScriptとPHPでリアルタイムチャットを製作しているのですが、どうやって現在の入室者数をカウントするかで嵌っています。
2
2
 
3
3
 
4
4
 

2

2019/07/23 17:29

投稿

nosonosolife
nosonosolife

スコア42

test CHANGED
@@ -1 +1 @@
1
- ソースコードリアルタイムチャットでのスマホの入室者カウントについて
1
+ リアルタイムチャットでのスマホの入室者カウントについて
test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
  == 2019.07.24 追記 ==
28
28
 
29
- 実際のコードを記載します。
29
+ 実際のソースコードを記載します。
30
30
 
31
31
 
32
32
 

1

ソースコード追記

2019/07/23 17:04

投稿

nosonosolife
nosonosolife

スコア42

test CHANGED
@@ -1 +1 @@
1
- リアルタイムチャットでのスマホの入室者カウントについて
1
+ ソースコードリアルタイムチャットでのスマホの入室者カウントについて
test CHANGED
@@ -21,3 +21,219 @@
21
21
  これをなんとか解決したいのですが、どうすればいいでしょうか?
22
22
 
23
23
  ご提示よろしくお願いいたします。
24
+
25
+
26
+
27
+ == 2019.07.24 追記 ==
28
+
29
+ 実際のコードを記載します。
30
+
31
+
32
+
33
+ ```JavaScript
34
+
35
+ function chatOut(){
36
+
37
+ if (userState) {
38
+
39
+ var ua = window.navigator.userAgent.toLowerCase();
40
+
41
+ if ( ua.indexOf("msie") != -1 ){
42
+
43
+ $.ajax({
44
+
45
+ type: "post",
46
+
47
+ url: "userinout.php",
48
+
49
+ data: { Name: userName , State: 'out' },
50
+
51
+ timeout: 10000, // 単位はミリ秒
52
+
53
+
54
+
55
+ success: function(result, textStatus, xhr) {
56
+
57
+ userState = false;
58
+
59
+ },
60
+
61
+ // 通信失敗時の処理
62
+
63
+ error: function(xhr, textStatus, error) {
64
+
65
+
66
+
67
+ }
68
+
69
+ });
70
+
71
+ }else{
72
+
73
+ $.ajax({
74
+
75
+ type: "post",
76
+
77
+ url: "userinout.php",
78
+
79
+ data: { Name: userName , State: 'out' },
80
+
81
+ timeout: 10000, // 単位はミリ秒
82
+
83
+ async: false,
84
+
85
+
86
+
87
+ success: function(result, textStatus, xhr) {
88
+
89
+ userState = false;
90
+
91
+ },
92
+
93
+ // 通信失敗時の処理
94
+
95
+ error: function(xhr, textStatus, error) {
96
+
97
+
98
+
99
+ }
100
+
101
+ });
102
+
103
+ }
104
+
105
+ }
106
+
107
+ }
108
+
109
+
110
+
111
+ var ua = navigator.userAgent;
112
+
113
+ if (ua.indexOf('iPhone') > 0 || ua.indexOf('iPad') ||
114
+
115
+ ua.indexOf('Android') > 0 && ua.indexOf('Mobile') > 0 ||
116
+
117
+ ua.indexOf('Android') > 0 ) {
118
+
119
+ $(window).bind('pagehide',function(){
120
+
121
+ chatOut();
122
+
123
+ });
124
+
125
+ }else{
126
+
127
+ $(window).on("beforeunload", function() {
128
+
129
+ chatOut();
130
+
131
+ });
132
+
133
+ }
134
+
135
+ ```
136
+
137
+ ```php
138
+
139
+ <?php
140
+
141
+ session_start();
142
+
143
+ if($_SERVER["REQUEST_METHOD"] == "POST"){
144
+
145
+ mb_language("uni");
146
+
147
+ mb_internal_encoding("utf-8"); //内部文字コードを変更
148
+
149
+ mb_http_input("auto");
150
+
151
+ mb_http_output("utf-8");
152
+
153
+
154
+
155
+ date_default_timezone_set('Asia/Tokyo');
156
+
157
+
158
+
159
+ if(isset($_POST['Name']) && isset($_POST['State'])){
160
+
161
+ $json = file_get_contents('inoutlog.json');
162
+
163
+ $records = json_decode($json, true);
164
+
165
+ $State = htmlspecialchars($_POST['State']);
166
+
167
+ $Num = count($records);
168
+
169
+ $Num++;
170
+
171
+
172
+
173
+ $Name = htmlspecialchars($_POST["Name"]);
174
+
175
+ $Date = date("Y/m/d H:i:s");
176
+
177
+ $_SESSION['user_name'] = $Name;
178
+
179
+
180
+
181
+ $records[] = [
182
+
183
+ 'num' => $Num,
184
+
185
+ 'date' => $Date,
186
+
187
+ 'name' => $Name,
188
+
189
+ 'state' => $State,
190
+
191
+ ];
192
+
193
+
194
+
195
+ $out_json = json_encode($records);
196
+
197
+
198
+
199
+ $result = file_put_contents("inoutlog.json", $out_json, LOCK_EX);
200
+
201
+
202
+
203
+ $countjson = file_get_contents('usercount.json');
204
+
205
+ $count = json_decode($countjson, true);
206
+
207
+ $countNum = $count['usercount'];
208
+
209
+
210
+
211
+ if($_POST['State'] == "in"){
212
+
213
+ $countNum++;
214
+
215
+ }else{
216
+
217
+ $countNum--;
218
+
219
+ }
220
+
221
+
222
+
223
+ $count['usercount'] = $countNum;
224
+
225
+
226
+
227
+ $out_countjson = json_encode($count);
228
+
229
+
230
+
231
+ $result = file_put_contents("usercount.json", $out_countjson, LOCK_EX);
232
+
233
+ }
234
+
235
+ }
236
+
237
+ ?>
238
+
239
+ ```