###■やりたいこと
クッキーごとに1度だけ表示するページを作っています。
wordpressの投稿テンプレートです。
###■うまくいかないこと
ファイルの読み込みがうまくいかないようで、次のようなエラーが出てしまいます。
Warning: fopen(/single-test-a.php): failed to open stream: No such file or directory in /home/XXX/example.com/public_html/wp-content/themes/simplicity2-child/single-test.php on line 23 ファイルが開けません。
###■該当のソースコード
■single.php
php
1<?php 2/* 3Template Name: singleテンプレート 4*/ 5if ( in_category('test') ) { 6get_template_part('single-test'); 7}else { 8get_template_part('single-page'); 9} 10?>
■single-test.php
php
1<?php 2 3//表示するファイルの設定 ここから▼▼▼ 4//$allow → 1回だけ許可するファイル 5//$deny → 2回目以降表示するファイル 6$allow = "/single-test-a.php"; 7$deny = "/single-test-b.php"; 8//表示するファイルの設定 ここまで▲▲▲ 9 10$buff = ""; $cookie=$_COOKIE["Visiter"]; 11if(strcmp($cookie,"") == 0){ 12$cookie = 0; 13} 14$value = $cookie + 1; $timeout = time() + 365 * 86400; 15setcookie("Visiter","$value",$timeout,'/',$_SERVER["SERVER_NAME"] . ";"); 16 17if( $cookie ) { 18$read_file = $deny; 19}else{ 20$read_file = $allow; 21} 22 23if (! ($fp = fopen ($read_file , "r" ))) { 24die("ファイルが開けません。"); 25} 26 27while ($line = fgets($fp)) { 28$buff .= $line; 29} 30fclose($fp); 31 32print $buff; 33 34?> 35
どなたか解決法を教えてくださいませ。
宜しくお願い致します。<(_ _)>
###□補足
ご回答を受けて、現在次の(その1)または(その2)のエラー状態です。
■single-test.php(その1)↓
→なんどやっても真っ白画面です。
php
1<?php 2 3if( $cookie ) { 4$read_file = $deny = get_stylesheet_directory()."//single-test-a.php"; 5}else{ 6$read_file = $allow = get_stylesheet_directory()."/single-test-b.php"; 7} 8 9?>
■single-test.php(その2)↓
→クロームだと「single-test-a」の中身だけ表示され、ファイアフォックスだと「single-test-b」の中身だけ表示されます。
php
1<?php 2 3//表示するファイルの設定 ここから▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼ 4//$allow → 1回だけ許可するファイル 5//$deny → 2回目以降表示するファイル 6$allow = get_stylesheet_directory()."/single-test-a.php"; 7$deny = get_stylesheet_directory()."/single-test-b.php"; 8//表示するファイルの設定 ここまで▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲ 9 10$buff = ""; $cookie=$_COOKIE["Visiter"]; 11if(strcmp($cookie,"") == 0){ 12$cookie = 0; 13} 14$value = $cookie + 1; $timeout = time() + 365 * 86400; 15setcookie("Visiter","$value",$timeout,'/',$_SERVER["SERVER_NAME"] . ";"); 16 17if( $cookie ) { 18$read_file = $deny; 19}else{ 20$read_file = $allow; 21} 22 23if (! ($fp = fopen ($read_file , "r" ))) { 24die("ファイルが開けません。"); 25} 26 27while ($line = fgets($fp)) { 28$buff .= $line; 29} 30fclose($fp); 31 32print $buff; 33 34?> 35
###□補足Ⅱ
■single-test.php(その3)↓
→その2と同じく、ブラウザで表示が違います。
ところで、phpやCSSが無効化されていることに気づきました。このコードにそのような無効化機能が入っているのでしょうか??
php
1<?php 2 3$buff = ""; $cookie=$_COOKIE["Visiter"]; 4if(strcmp($cookie,"") == 0){ 5$cookie = 0; 6} 7$value = $cookie + 1; $timeout = time() + 365 * 86400; 8setcookie("Visiter","$value",$timeout,'/',$_SERVER["SERVER_NAME"] . ";"); 9 10if( $cookie ) { 11$read_file = $deny = get_stylesheet_directory()."/single-test-a.php"; 12}else{ 13$read_file = $allow = get_stylesheet_directory()."/single-test-b.php"; 14} 15 16if (! ($fp = fopen ($read_file , "r" ))) { 17die("ファイルが開けません。"); 18} 19 20while ($line = fgets($fp)) { 21$buff .= $line; 22} 23fclose($fp); 24 25print $buff; 26 27?> 28
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/12/28 12:07
退会済みユーザー
2017/12/28 13:11
退会済みユーザー
2017/12/28 13:15
退会済みユーザー
2017/12/28 13:31
退会済みユーザー
2017/12/28 13:59
退会済みユーザー
2017/12/28 14:14
退会済みユーザー
2017/12/28 14:18
退会済みユーザー
2017/12/28 14:19
退会済みユーザー
2017/12/28 14:44
退会済みユーザー
2017/12/28 15:26
退会済みユーザー
2017/12/28 15:46
退会済みユーザー
2017/12/28 16:31
退会済みユーザー
2017/12/28 16:36
退会済みユーザー
2017/12/28 16:38
退会済みユーザー
2017/12/29 01:39
退会済みユーザー
2017/12/29 02:48
退会済みユーザー
2017/12/29 05:22
退会済みユーザー
2017/12/29 05:34
退会済みユーザー
2017/12/29 05:43
退会済みユーザー
2017/12/29 06:07
退会済みユーザー
2017/12/29 06:26
2017/12/29 07:23
退会済みユーザー
2017/12/29 07:37