headerでページ遷移できないときの対象方法(PHP)

PHPでプログラミングをしているときに、Aという処理がTRUEの場合別ページに遷移させる、そういうときありますよね。別ページに遷移する処理はPHPだと「header」を利用します。

しかし正しく記載しているつもりでもページがうまく遷移せず迷宮入りすることもありますので対処方法を記載します。

headerのサンプルソース

if(hoge) {
    header("Location: https://yahoo.co.jp");
}

これはhoge処理がTRUEの場合、ヤフーページへ遷移する処理です。

エラー内容

PHPが正しく動作しない場合、必ずエラーログが出力されています。ログの内容を確認しましょう。

error_logの内容

ログには以下の通り出力されていました。

[11-Dec-2020 08:25:33 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 397
[11-Dec-2020 08:26:02 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 397
[11-Dec-2020 08:26:18 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 397
[11-Dec-2020 08:27:41 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 398
[11-Dec-2020 08:28:18 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 398
[11-Dec-2020 08:36:14 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 399
[11-Dec-2020 08:36:34 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 399
[11-Dec-2020 08:36:44 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 399
[11-Dec-2020 08:42:42 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 395
[11-Dec-2020 08:42:51 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 395
[11-Dec-2020 08:45:24 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 395
[11-Dec-2020 08:45:42 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 395
[11-Dec-2020 08:45:48 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 395
[11-Dec-2020 08:46:00 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 395
[11-Dec-2020 08:46:04 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 395
[11-Dec-2020 08:46:20 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 395
[11-Dec-2020 08:49:01 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 396
[11-Dec-2020 08:51:13 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 396
[11-Dec-2020 08:52:18 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 391
[11-Dec-2020 08:52:21 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 391
[11-Dec-2020 08:54:03 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 391
[11-Dec-2020 08:54:07 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 391
[11-Dec-2020 08:54:07 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 391
[11-Dec-2020 12:15:20 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 391
[11-Dec-2020 12:22:09 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 392
[11-Dec-2020 12:30:05 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 386
[11-Dec-2020 12:36:51 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 386
[11-Dec-2020 12:37:39 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /filepath・・・・/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:4780) in /filepath・・・・/wp-content/themes/hoge-wp/functions.php on line 390

 

同じエラーが大量に出力しているのがわかります。

対処方法

HTMLより先に処理を行う

header処理はコーディングする場所が重要です。HTMLが出力されてからではエラーとなってしまいます。そのため、header処理を記載する場合はページの最上部に記載することをお勧めします。もうページの一番上です。もちろんheader.phpよりも先に記載する必要があります。

今回error_logに出力されていた原因は記載場所でした。

WordPress固定ページにショートコードを作成し、function.phpで一生懸命headerでページ遷移を試みましたがエラーを多発するだけで全然処理してくれませんでした。

正常に動作するケース

<?php 
if(hoge) {
    header("Location: https://yahoo.co.jp");
}
get_header(); 
?>

header.phpの内容

<!DOCTYPE html>
<html>
<head>
	<!--[if lt IE 9]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width,initial-scale=1.0">
	<title><?php the_title(); ?>|<?php bloginfo(‘title’); ?></title>
	<link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri(); ?>/style.css" media="all" />
	<link href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" rel="stylesheet">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
	<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/javascript.js" charset="UTF-8"></script>
	<?php wp_head(); ?>
</head>

正常に動作しないケース

※HTMLの読み込みが先に来ているためheaderは動作しない

<?php
get_header();
if(hoge) { header("Location: https://yahoo.co.jp"); }
?>

スペルミスを確認する

コーディングに一番ありがちなスペルミスです。例えば「hedder」とか「headrer」とか他では自然に書けていたものが急にとんでもないスペルミスになることはあります。スペルをしっかり見直す、また他で正しく動作している処理をコピーして検索かけひっかることを確認する。(スペルがしっかり書かれていると正しい処理をしているプログラム処理で検索するとHITするはずです)

不要はスペースを削除する

ありがちな記載ミスとして不要な半角スペースを設置してしまうことです。

NGのケース

if(hoge) {
    header("Location : https://yahoo.co.jp");
}

これは何がいけないかわかりますか?半角スペースを「△」で表記すると以下の通りです。

if(hoge) {
    header("Location△:△https://yahoo.co.jp");
}

この通り、Locationの次にも半角スペースが混在しています。ここの半角スペースは不要となりますのでご注意ください。

まとめ

header処理が正しく動作しない場合の確認ポイントはいかがでしたでしょうか?

エラーログはもっとわかりやすく記載してくれればいいのに、という気持ちはいつもありますが、今回は私自身が困って解決にやや時間がかかったことをブログに書き留めてみました。

同じような悩みの方の解決に役立てれば幸いです!