久しぶりに書いてみる。
header('Location: ~~') って書いて実行して、ローカルのアパッチでは問題なくページが遷移できたのに、サーバにアップロードして実行したらページが遷移できなかった、なぜだろう? というお話。
理由を考えてみた。
以下のプログラムをローカル環境、サーバ環境でそれぞれ実行してみた。
■----transition1.php-----■
<?php
echo 'HAHAHA!!';
header('Location: index.php');
?>
■------------------------■
結果、ローカル環境では index.php に遷移し、サーバ環境では遷移せず、『HAHAHA!!』という文字列が出力されました。
というワケで、ローカル、サーバで動きが異なったのは、単純に設定か何かの問題だろうということで無視。
とにかく、やっぱり何かを出力しちゃってるっぽいぞと。
いったいそれはなんなんだと。
で、問題のプログラムを詳しく見ていったら、はい! わかった! 解決!
唐突に問題!
さて、今回の問題は、以下 2 つのプログラムと同様の問題により、header("Location: ~~") が機能しませんでした。
いったいドコが悪いのでしょう?
答えは土曜日くらいに!
解答がなくても土曜日くらいに!
■----function.php--------■
<?php
function process($var){
return $var * 2;
}
?>
■------------------------■
■----transition2.php-----■
<?php
require_once('function.php');
header('Location: index.php');
?>
■------------------------■
header('Location: ~~') って書いて実行して、ローカルのアパッチでは問題なくページが遷移できたのに、サーバにアップロードして実行したらページが遷移できなかった、なぜだろう? というお話。
理由を考えてみた。
- header() の前に何か出力してない?
ははは。まさかそんなワケないっしょ。却下。 - サーバの該当のページに、Basic 認証をかけているから?
Basic 認証をかけているページからかけていないページへ、その逆など、いろいろ試してみたけど特に関係ないみたい。 - 本当に、header() の前に何か出力してないのかい?
・・・やっぱこれかな? よし、調べてみよう。
以下のプログラムをローカル環境、サーバ環境でそれぞれ実行してみた。
■----transition1.php-----■
<?php
echo 'HAHAHA!!';
header('Location: index.php');
?>
■------------------------■
結果、ローカル環境では index.php に遷移し、サーバ環境では遷移せず、『HAHAHA!!』という文字列が出力されました。
というワケで、ローカル、サーバで動きが異なったのは、単純に設定か何かの問題だろうということで無視。
とにかく、やっぱり何かを出力しちゃってるっぽいぞと。
いったいそれはなんなんだと。
で、問題のプログラムを詳しく見ていったら、はい! わかった! 解決!
唐突に問題!
さて、今回の問題は、以下 2 つのプログラムと同様の問題により、header("Location: ~~") が機能しませんでした。
いったいドコが悪いのでしょう?
答えは土曜日くらいに!
解答がなくても土曜日くらいに!
■----function.php--------■
<?php
function process($var){
return $var * 2;
}
?>
■------------------------■
■----transition2.php-----■
<?php
require_once('function.php');
header('Location: index.php');
?>
■------------------------■
とういワケで答えです!
問題の部分は、こちら!
■----function.php--------■
<?php
function process($var){
return $var * 2;
}
?> // この行で PHP 終わり
// ←この部分の改行が、HTML として出力されてしまっている
■------------------------■
すなわち、transition2.php 内で、require_once で function.php を読み込んだ際、改行が出力されたおかげで、header('Location: ~~') が機能しなかったというコトですね! はい!
というか、規約的には PHP 単独のファイル(HTML と混在しないファイル)の場合、?> っていう閉じタグは書かないことが推奨(むしろ書くなと)されてるみたいですね。知りませんでした。
参考URL↓
http://framework.zend.com/manual/ja/coding-standard.php-file-formatting.html
おーそういえばそんな規約読んだことあった!
でも閉じタグ書かないと Notice かなんか出なかったっけ?
僕の勘違いかな。
PEAR のコーディング規約
http://pear.plus-server.net/standards.html
この中のファイルフォーマットって中に
http://pear.plus-server.net/standards.file.html
>>>
2) PHP 終了タグ (?> ) の後には ラインフィードを 1 つ 置くべきです。 つまり、ファイルの最後にカーソルを移動させると、 PHP 終了タグの 1 つ 下の行に カーソルがあるようにすべきと言うことです。
<<<
あと昨日言ったのはこれね。
http://pear.plus-server.net/standards.including.html
>>>
注意 include_once() および require_once() は命令であり、関数ではありません。 読み込むファイル名の前後にカッコは付けません。
<<<
それから
header('Location: index.php');
ってなってるけど、これは RFC 的にだめだよー。
http://www.studyinghttp.net/rfc_ja/rfc2616#Sec14.30
Location ヘッダの値は絶対URLしかだめなの。http から書かないとだめだよ。
フラグメント識別子もだめね。
http://d.hatena.ne.jp/maru_cc/20080207/1202386973
でもたいていのブラウザでうごいちゃうんだけど。
閉じタグ書かなきゃ Notice、確かに自分もそうだと思ってましたけど、試してみたら、Notice 出ませんでした。
あれ? 夢でも見たかな?
規約に関しては、知らないことだらけですね~、ほとんど読んだことないから。
指摘ありがとうございます!
If you're in not good state and have got no money to move out from that, you would require to take the loans. Because that would aid you for sure. I take college loan every single year and feel myself good because of that.
Easily, the publish is really the greatest on this laudable topic. I concur with your conclusions and will thirstily look forward to your future updates. Saying thanks will not just be sufficient, for the fantastic lucidity in your writing. I will instantly grab your rss feed to stay privy of any updates. Solid work and much success in your business enterprise!
Usually I do not write on blogs, but I would like to say that this article really convinced me to do so! Congratulations, pretty nice submit.
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
it Is easily my favourite, lovely post really will be coming back to this time and time again. Thanks!