<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP | バスにっき</title>
	<atom:link href="https://takabus.com/category/program/php/feed/" rel="self" type="application/rss+xml" />
	<link>https://takabus.com</link>
	<description>バスに関するニュース・マメ知識を紹介しています</description>
	<lastBuildDate>Thu, 25 Mar 2021 00:10:00 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.3</generator>

<image>
	<url>https://takabus.com/wp-content/uploads/2022/08/cropped-takabusサイトロゴスクエア余白なし-1-32x32.png</url>
	<title>PHP | バスにっき</title>
	<link>https://takabus.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【PHP】POST・GETされたデータとヘッダーをロギングする方法</title>
		<link>https://takabus.com/201214-dump-post-data-php/</link>
					<comments>https://takabus.com/201214-dump-post-data-php/#respond</comments>
		
		<dc:creator><![CDATA[takabus]]></dc:creator>
		<pubDate>Sun, 10 Jan 2021 02:10:00 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">https://cssv.mydns.jp/wp/?p=3944</guid>

					<description><![CDATA[　デバッグするときに、POST・GETで送信したデータやリクエストヘッダーを確認したいときがあります。テキストファイルに書き出しておけると便利でしょう。 　こういうときに使えるコードを載せておきます。 サンプルコード 　 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>　デバッグするときに、POST・GETで送信したデータやリクエストヘッダーを確認したいときがあります。テキストファイルに書き出しておけると便利でしょう。</p>



<p>　こういうときに使えるコードを載せておきます。</p>



<h2 class="wp-block-heading">サンプルコード</h2>



<p>　phpファイルを作成し、以下のコードをコピー・アンド・ペーストします。Webサーバーにアップロードすれば完了です。</p>



<pre class="wp-block-code php"><code>&lt;?php
// ログ保存ファイル名
define("TXTFILE", "log.txt");
// ファイルを追記モードでオープン
$fh = fopen(TXTFILE, "a+");
//ヘッダーをすべて取得して、配列として取得
$str = print_r(getallheaders(), true);//print_r関数は、第2引数んいtrueを指定すると、内容を返すことができる
// ファイルに追記する
fputs($fh,  date("Y-m-d H:i:s") . ' ' . $str . "\n");
// WEBページにも表示しておく
echo ($str);
// POSTされたデータをすべて取得して、配列として取得
$str = print_r($_POST, true);
// ファイルに追記する
fputs($fh,  date("Y-m-d H:i:s") . ' ' . $str . "\n");
// GETされたデータをすべて取得して、配列として取得
$str = print_r($_GET, true);
// ファイルに追記する
fputs($fh,  date("Y-m-d H:i:s") . ' ' . $str . "\n");
// WEBページにも表示しておく
echo ($str);
// ファイルを閉じる
fclose($fh);
// var_dump(file_get_contents(TXTFILE));
?&gt;</code></pre>



<p>あとはそのphpにPOSTしてみると、こんな感じで、POSTされたデータとRequestヘッダーがテキストファイルに記録されます。</p>



<pre class="wp-block-code"><code>2020-12-10 12:44:24 Array
(
    &#91;Content-Type] =&gt; application/x-www-form-urlencoded
    &#91;Host] =&gt; 192.168.1.4
    &#91;Content-Length] =&gt; 13
    &#91;Cache-Control] =&gt; no-cache
)

2020-12-10 12:44:24 Array
(
    &#91;a] =&gt; 1234
    &#91;b] =&gt; 5678
)
2020-12-10 12:44:24 Array
(
)</code></pre>



<p>　ログを出力するテキストファイルは、phpを配置したディレクトリに作成されます。GETでもヘッダーは記録されますので、ブラウザでアクセスするだけでもログが追加されていきます。</p>



<p>　もちろん、ログファイルが作成されるディレクトリは、Webサーバーから書き込めるように、書き込み権限を設定しておく必要がありますので、お忘れなく。</p>



<pre class="wp-block-code"><code># chmod -R 777 test //全ユーザーからの全権限（書き込み含む）を許可</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://takabus.com/201214-dump-post-data-php/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
