PHP簡易掲示板に関してです。
「もし、名前の欄が空で渡された時、personal_nameの変数に「匿名」を入れる」
というものを作りたいのですが、ifでやろうとしても、「空」のデータをどうやって見極めるかができなかったので、質問することにしました。
実現したいこと
名前の欄に半角スペースまたは全角スペースが入力されていたか、空である場合は、変数personal_nameに「匿名」を入力する。
#コード
PHP
1<?php 2require '../recaptcha/recaptcha_vars.php'; 3// reCAPTCHA サイトキー 4$siteKey = V2_SITEKEY; 5// reCAPTCHA シークレットキー 6$secretKey = V2_SECRETKEY; 7 8$result_status = ''; // 結果を表示する文字列を初期化 9if ( isset( $_POST[ 'g-recaptcha-response' ] ) ) { 10 //API Request URL(リクエストを送る API の URL) 11 $url = 'https://www.google.com/recaptcha/api/siteverify'; 12 //パラメータを指定 13 $data = array( 14 'secret' => $secretKey, //シークレットキー 15 'response' => $_POST[ 'g-recaptcha-response' ] 16 ); 17 //POST メソッドを使用 18 $context = array( 19 'http' => array( 20 'method' => 'POST', 21 'header' => implode("\r\n", array('Content-Type: application/x-www-form-urlencoded',)), 22 'content' => http_build_query($data) 23 ) 24 ); 25 //上記パラメータを指定して file_get_contents で API Response を取得 26 $api_response = file_get_contents($url, false, stream_context_create($context)); 27 $result = json_decode( $api_response ); 28 if ( $result->success ) { 29 $result_status = '成功'; 30 // 成功した場合の処理(メールの送信など)を実行(または結果を変数に入れて、その変数を使って処理を分岐するなど) 31 } else { 32 $result_status = '失敗: '; 33 // error-codes は配列(以下は最初のエラーを取得) 34 $result_status .= $result->{'error-codes'}[ 0 ]; 35 } 36} 37?> 38<html> 39<script> 40 var onloadCallback = function() { 41 grecaptcha.render('recaptcha', { 42 'sitekey' : "<?php echo $siteKey; ?>", 43 'callback' : verifyCallback, 44 'expired-callback' : expiredCallback 45 }); 46 }; 47 var verifyCallback = function(response) { 48 document.getElementById("warning").textContent = ''; 49 document.getElementById("send").disabled = false; 50 }; 51 var expiredCallback = function() { 52 document.getElementById("warning").textContent = '投稿するにはチェックを入れてください。'; 53 document.getElementById("send").disabled = true; 54 }; 55</script> 56</head> 57<head> 58 59<link rel="icon" type="image/png" href="../icon.png"> 60<title>掲示板</title></head> 61 62<body> 63<body bgcolor="#181a19" text="#cccccc" alink="#ffffff"> 64 65 66<p><h1>掲示板</h1></p><br> 67<a href="https://kei4ban.ml/">kei4ban.ml(トップページ)</a><br> 68 69<p>テスト的な意味もありますが、ちょっと作ってみました。</p> 70削除・変更の依頼は<a href="./delete.php" target="_blank">こちら</a>から 71 72<h6>あ~疲れた</h6><br> 73 74 75 76<form method="POST" action="<?php print($_SERVER['PHP_SELF']) ?>"> 77 78<input type="text" name="personal_name"><br><br> 79 80<textarea name="contents" rows="8" cols="40" style="margin: 0px; width: 1062px; height: 108px;"></textarea><br><br> 81 <div id="recaptcha"></div> 82 <p id="warning">投稿するにはチェックを入れてください。</p> 83<input id="send" type="submit" name="btn1" class="btn btn-primary" disabled value="投稿!!"> 84 85</form> 86 87 88 89<?php 90 91 92 93if($_SERVER["REQUEST_METHOD"] == "POST"){ 94 95 writeData(); 96 97} 98 99 100 101readData(); 102 103 104 105function readData(){ 106 107 $keijban_file = 'keiji_all.txt'; 108 109 110 111 $fp = fopen($keijban_file, 'rb'); 112 113 114 115 if ($fp){ 116 117 if (flock($fp, LOCK_SH)){ 118 119 while (!feof($fp)) { 120 121 $buffer = fgets($fp); 122 123 print($buffer); 124 125 } 126 127 128 129 flock($fp, LOCK_UN); 130 131 }else{ 132 133 print('ファイルロックに失敗しました'); 134 135 print('何度も失敗する場合は知らせてください。'); 136 137 138 139 } 140 141 } 142 143 144 145 fclose($fp); 146 147} 148 149 150 151function writeData(){ 152$y = date('Y'); 153$m = date('m'); 154$d = date('d'); 155$zi = date('H'); 156$funn = date('i'); 157$byou = date('s'); 158 $personal_name = $_POST['personal_name']; 159 160 $contents = $_POST['contents']; 161 162 $contents = htmlentities($contents, ENT_QUOTES, 'UTF-8'); 163 $contents = nl2br($contents); 164 165 166 167 $data = "<hr>\r\n"; 168 169 $data = $data."<p>".$y."年".$m."月".$d."日".$zi."時".$funn."分".$byou."秒に投稿</p>\r\n"; 170 $personal_name = htmlentities($personal_name, ENT_QUOTES, 'UTF-8'); 171 $data = $data."<p>投稿者:".$personal_name."</p>\r\n"; 172 173 $data = $data."<p>内容:</p>\r\n"; 174 $data = $data."<p>".$contents."</p>\r\n"; 175 176 $keijban_file = 'keiji_all.txt'; 177 178 179 180 $fp = fopen($keijban_file, 'ab'); 181 182 183 184 if ($fp){ 185 186 if (flock($fp, LOCK_EX)){ 187 188 if (fwrite($fp, $data) === FALSE){ 189 190 print('ファイル書き込みに失敗しました'); 191 192 print('何度も失敗する場合は知らせてください。'); 193 194 195 196 } 197 198 199 200 flock($fp, LOCK_UN); 201 202 }else{ 203 204 print('ファイルロックに失敗しました'); 205 206 print('何度も失敗する場合は知らせてください。'); 207 208 209 210 } 211 212 } 213 214 215 216 fclose($fp); 217 218} 219 220 221 222?> 223 224</body> 225<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script> 226</html> 227
https://www.javadrive.jp/php/sample/keijiban/index4.htmlを元にして作りました。
>どうやって見極めるか
見極めるも何も、渡された値を確認すれば良いだけでは?
回答1件
あなたの回答
tips
プレビュー