phpのリダイレクトではまったこと。

Twitter ツイート Hatena Bookmark ブックマーク

phpを初めて使ってからもう6年経つ僕ですが、リダイレクト処理で勘違していたみたいです。
リダイレクトのヘッダを生成した瞬間僕は、phpの処理がとまると勘違いしておりました。

こんな感じに

1
2
3
4
5
6
7
8
<?php

// ...処理いろいろ
if ( $isRedirect ) {
  header('Location: index.php');
}
// ...処理いろいろ2
?>

僕はheader関数はかれたすぐにプログラムの実行が止まってリダイレクトかかるとおもったんですが、どうやらちがったみたいですね。。。

1
2
3
4
5
6
7
8
<?php

// ...処理いろいろ
if ( $isRedirect ) {
  header('Location: index.php');
}
error_log("location test");
?>

こんな風に書くとリダイレクト時にもログが出力されてしまいます。。。
なんで、ちゃんとexitを書いておくべきだと。。。

1
2
3
4
5
6
7
8
<?php
// ...処理いろいろ
if ( $isRedirect ) {
  header('Location: index.php');
  exit;
}
error_log("location test");
?>

こんなつまらない事で金曜日あたりに2時間ぐらいはまってましたw

comments powered by Disqus
Built with Hugo
テーマ StackJimmy によって設計されています。