<?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>Serendip &#187; php</title>
	<atom:link href="http://www.serendip.ws/archives/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.serendip.ws</link>
	<description>Webデザイン・プログラミング</description>
	<lastBuildDate>Thu, 02 Sep 2010 07:00:52 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Python の if __name__ == &#8216;__main__&#8217;: を Perl, Ruby, PHP で行う</title>
		<link>http://www.serendip.ws/archives/4360</link>
		<comments>http://www.serendip.ws/archives/4360#comments</comments>
		<pubDate>Fri, 19 Feb 2010 13:37:02 +0000</pubDate>
		<dc:creator>iNo</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.serendip.ws/?p=4360</guid>
		<description><![CDATA[以下の Python コードの if 文の本体は、直接スクリプトとして呼び出された場合のみ実行され、ライブラリとして読み込まれた場合は実行されない。 if __name__ == '__main__': # do something 同様のコードを Perl, Ruby, PHP で書く方法を調べてみた。 Perl の場合 if ($0 eq __FILE__) { # do something } Ruby の場合 if $0 == __FILE__ # do something end PHP の場合 if (basename(__FILE__) == basename($_SERVER['PHP_SELF'])) { // do something }]]></description>
			<content:encoded><![CDATA[<p>以下の Python コードの <code>if</code> 文の本体は、直接スクリプトとして呼び出された場合のみ実行され、ライブラリとして読み込まれた場合は実行されない。</p>
<pre><span class="Statement">if</span> __name__ == '<span class="Constant">__main__</span>':
    <span class="Comment"># do something</span>
</pre>
<p>同様のコードを Perl, Ruby, PHP で書く方法を調べてみた。</p>
<h3>Perl の場合</h3>
<pre><span class="Statement">if</span> (<span class="Special">$0</span> <span class="Statement">eq</span> __FILE__) {
    <span class="Comment"># do something</span>
}
</pre>
<h3>Ruby の場合</h3>
<pre><span class="Statement">if</span> <span class="Special">$0</span> == <span class="Constant">__FILE__</span>
  <span class="Comment"># do something</span>
<span class="Statement">end</span>
</pre>
<h3>PHP の場合</h3>
<pre><span class="Statement">if</span> <span class="Special">(</span><span class="Identifier">basename</span><span class="Special">(</span><span class="Constant">__FILE__</span><span class="Special">)</span> <span class="Statement">==</span> <span class="Identifier">basename</span><span class="Special">(</span><span class="Statement">$</span><span class="Identifier">_SERVER</span><span class="Special">[</span>'<span class="Constant">PHP_SELF</span>'<span class="Special">]))</span> <span class="Special">{</span>
    <span class="Comment">// do something</span>
<span class="Special">}</span>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.serendip.ws/archives/4360/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP5.3 でタイムゾーンの設定に関する警告が出る</title>
		<link>http://www.serendip.ws/archives/3229</link>
		<comments>http://www.serendip.ws/archives/3229#comments</comments>
		<pubDate>Thu, 08 Oct 2009 12:45:49 +0000</pubDate>
		<dc:creator>iNo</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.serendip.ws/?p=3229</guid>
		<description><![CDATA[Snow Leopard の MacPorts でインストールした PHP が Version 5.3.0 となり、以下のような警告が出るようになった。 Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled [...]]]></description>
			<content:encoded><![CDATA[<p>Snow Leopard の MacPorts でインストールした PHP が Version 5.3.0 となり、以下のような警告が出るようになった。</p>
<pre>Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Tokyo' for 'JST/9.0/no DST' instead in <var>/Users/hoge/public_html/hoge.php</var> on line <var>10</var>
</pre>
<p>php.ini にタイムゾーンを Asia/Tokyo として設定し、Apache2 を再起動すると警告が出なくなる。</p>
<pre>$ sudo vi /opt/local/etc/php5/php.ini
</pre>
<pre><span class="Special">[Date]</span>
<span class="Comment">; Defines the default timezone used by the date functions</span>
<span class="Comment">; http://php.net/date.timezone</span>
<span class="Type">date.timezone =</span> &quot;Asia/Tokyo&quot;
</pre>
<pre>$ sudo /opt/local/apache2/bin/apachectl restart
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.serendip.ws/archives/3229/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP で $_GET $_POST で Notice: Undefined index: が出る</title>
		<link>http://www.serendip.ws/archives/3217</link>
		<comments>http://www.serendip.ws/archives/3217#comments</comments>
		<pubDate>Wed, 07 Oct 2009 12:32:43 +0000</pubDate>
		<dc:creator>iNo</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.serendip.ws/?p=3217</guid>
		<description><![CDATA[以下のような記述が .php 内にあった場合、Notice: Undefined index: id in /Users/foo/public_html/hoge.php on line n と表示される。 $id = $_GET['id']; これは、外部からの汚染された引数が渡されたことが原因なので、htmlspecialchars を使ってサニタイズすると警告が表示されなくなる。 $id = htmlspecialchars(@$_GET['id']);]]></description>
			<content:encoded><![CDATA[<p>以下のような記述が .php 内にあった場合、<code>Notice: Undefined index: <var>id</var> in <var>/Users/foo/public_html/hoge.php</var> on line <var>n</var></code> と表示される。</p>
<pre><span class="Statement">$</span><span class="Identifier">id</span> <span class="Statement">=</span> <span class="Statement">$</span><span class="Identifier">_GET</span><span class="Special">[</span>'<span class="Constant">id</span>'<span class="Special">]</span>;
</pre>
<p>これは、外部からの汚染された引数が渡されたことが原因なので、<code>htmlspecialchars</code> を使ってサニタイズすると警告が表示されなくなる。</p>
<pre><span class="Statement">$</span><span class="Identifier">id</span> <span class="Statement">=</span> <span class="Identifier">htmlspecialchars</span><span class="Special">(</span>@<span class="Statement">$</span><span class="Identifier">_GET</span><span class="Special">[</span>'<span class="Constant">id</span>'<span class="Special">])</span>;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.serendip.ws/archives/3217/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP を CGI モードで動かす</title>
		<link>http://www.serendip.ws/archives/2997</link>
		<comments>http://www.serendip.ws/archives/2997#comments</comments>
		<pubDate>Wed, 09 Sep 2009 12:52:55 +0000</pubDate>
		<dc:creator>iNo</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.serendip.ws/?p=2997</guid>
		<description><![CDATA[例えば、hoge.php ファイルを CGI モードで動作させるには、.htaccess ファイルに以下の記述を追加する。 &#60;Files hoge.php&#62; AddHandler application/x-httpd-phpcgi .php &#60;/Files&#62;]]></description>
			<content:encoded><![CDATA[<p>例えば、hoge.php ファイルを CGI モードで動作させるには、.htaccess ファイルに以下の記述を追加する。</p>
<pre><span class="Statement">&lt;Files</span><span class="Constant"> hoge.php</span><span class="Statement">&gt;</span>
<span class="Identifier">AddHandler</span> application/x-httpd-phpcgi .php
<span class="Statement">&lt;/Files&gt;</span>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.serendip.ws/archives/2997/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OSX に PECL::imagick をインストール</title>
		<link>http://www.serendip.ws/archives/296</link>
		<comments>http://www.serendip.ws/archives/296#comments</comments>
		<pubDate>Mon, 20 Oct 2008 01:15:27 +0000</pubDate>
		<dc:creator>iNo</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.serendip.ws/?p=296</guid>
		<description><![CDATA[Mac OSX に ImageMagick を PHP から利用する PECL::imagick をインストールした際のメモ。 $ sudo pecl install imagick 上記のインストール方法では、MagickWand-config, Wand-config が見つからないというエラーが出てインストール出来なかったのでソースからコンパイルしてインストールした。 $ pecl bundle imagick $ cd imagick $ phpize $ ./configure --with-imagick=/opt/local $ make $ sudo cp modules/imagick.so /usr/lib/php/extensions/no-debug-non-zts-20060613/ php.ini に次の設定を書き込む。 $ sudo vi /opt/local/etc/php.ini extension=imagick.so apache を再起]]></description>
			<content:encoded><![CDATA[<p>Mac OSX に ImageMagick を PHP から利用する <a href="http://pecl.php.net/package/imagick" class="out">PECL::imagick</a> をインストールした際のメモ。</p>
<pre><code>$ sudo pecl install imagick
</code></pre>
<p>上記のインストール方法では、<code>MagickWand-config</code>, <code>Wand-config</code> が見つからないというエラーが出てインストール出来なかったのでソースからコンパイルしてインストールした。</p>
<pre><code>$ pecl bundle imagick
$ cd imagick
$ phpize
$ ./configure --with-imagick=/opt/local
$ make
$ sudo cp modules/imagick.so /usr/lib/php/extensions/no-debug-non-zts-20060613/
</code></pre>
<p><code>php.ini</code> に次の設定を書き込む。</p>
<pre><code>$ sudo vi /opt/local/etc/php.ini

extension=imagick.so
</code></pre>
<p>apache を再起動する。</p>
<pre><code>$ sudo /opt/local/apache2/bin/apachectl restart
</code></pre>
<p><code>phpinfo()</code> に <code>imagick</code> の項目が表示されていればインストール完了!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.serendip.ws/archives/296/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHPで指定ディレクトリ下のファイルを更新日時順に取得する</title>
		<link>http://www.serendip.ws/archives/60</link>
		<comments>http://www.serendip.ws/archives/60#comments</comments>
		<pubDate>Wed, 11 Jun 2008 06:15:54 +0000</pubDate>
		<dc:creator>iNo</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.serendip.ws/?p=60</guid>
		<description><![CDATA[function get_file_list($dir='.') { $temp_file = array(); if ($h_dir = opendir($dir)) { while (false !== ($filename = readdir($h_dir))) { if ($filename != '.' &#038;&#038; $filename != '..') { if (is_file($dir . '/' . $filename)) { $temp_file[$filename] = filectime($filename); } } } } else { exit("ERROR : cannot open directory $dir. "); } arsort($temp_file); return $temp_file; } [...]]]></description>
			<content:encoded><![CDATA[<pre><code>function get_file_list($dir='.') {
    $temp_file = array();
    if ($h_dir = opendir($dir)) {
        while (false !== ($filename = readdir($h_dir))) {
            if ($filename != '.' &#038;&#038; $filename != '..') {
                if (is_file($dir . '/' . $filename)) {
                    $temp_file[$filename] = filectime($filename);
                }
            }
        }
    } else {
        exit("ERROR : cannot open directory $dir. ");
    }
    arsort($temp_file);
    return $temp_file;
}

print_r(get_file_list());
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.serendip.ws/archives/60/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHPでCookieを使ったアクセスカウンタ</title>
		<link>http://www.serendip.ws/archives/56</link>
		<comments>http://www.serendip.ws/archives/56#comments</comments>
		<pubDate>Thu, 15 May 2008 12:27:11 +0000</pubDate>
		<dc:creator>iNo</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.serendip.ws/?p=56</guid>
		<description><![CDATA[Cookieを使ってセッション管理をするアクセスカウンタを作ってみた。 単純にカウントの合計を表示するだけのもの。 &#60;?php $logfile = &#34;php_counter.log&#34;; if (!file_exists($logfile)) { touch($logfile); } $file = fopen($logfile, &#34;r+&#34;) or exit(&#34;FILE OPEN ERROR...&#34;); flock($file, LOCK_EX); if (isset($_COOKIE[&#34;php_counter&#34;]) == false) { $Count = fgets($file, 32) + 1; setcookie(&#34;php_counter&#34;, 1); } else { $Count = fgets($file, 32); } rewind($file); fwrite($file, $Count); flock($file, LOCK_UN); fclose($file); ?&#62; ------カウンタ表示部 &#60;?php echo &#34;&#60;div&#62;$Count&#60;/div&#62;&#34; ?&#62;]]></description>
			<content:encoded><![CDATA[<p>Cookieを使ってセッション管理をするアクセスカウンタを作ってみた。<br />
単純にカウントの合計を表示するだけのもの。</p>
<pre><code>&lt;?php

$logfile = &quot;php_counter.log&quot;;

if (!file_exists($logfile)) {
    touch($logfile);
}
$file = fopen($logfile, &quot;r+&quot;) or exit(&quot;FILE OPEN ERROR...&quot;);
flock($file, LOCK_EX);
if (isset($_COOKIE[&quot;php_counter&quot;]) == false) {
    $Count = fgets($file, 32) + 1;
    setcookie(&quot;php_counter&quot;, 1);
} else {
    $Count = fgets($file, 32);
}
rewind($file);
fwrite($file, $Count);
flock($file, LOCK_UN);
fclose($file);

?&gt;
------カウンタ表示部
&lt;?php
echo &quot;&lt;div&gt;$Count&lt;/div&gt;&quot;
?&gt;
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.serendip.ws/archives/56/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
