headers already sentとなる原因とエラー箇所を特定する方法

このQ&Aのポイント
  • 質問文章でheaders already sentとなる原因として、セッションが開始される前に出力が行われていることが考えられます。
  • 具体的には、Plan.class.phpの122行目でセッションが開始される前に何かしらの出力が行われている可能性があります。
  • エラーの原因を特定するためには、出力箇所の近くのコードを確認し、セッション開始前の出力を見つける必要があります。
回答を見る
  • ベストアンサー

headers already sent と言われてしまう。

[portal.php] で、 <?php require ('Smarty.class.php'); require ('Plan.class.php'); session_start(); $smarty = new Smarty; $schedule = new Plan; if(empty($_SESSION["user"])){ Header("Location: login.php"); } 以下略 というコードを打って、実行したら Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Program Files\Apache Group\*******\Plan.class.php:122) in C:\Program Files\Apache Group\*******\portal.php on line 4 というエラーが出ます。 \Plan.class.php には <?php class Plan{ function __construct(){ $this->Zaikofile="C:Program Files\Apache Group\Apache2\logs\plan.sqlite"; $this->user=$_SESSION["user"]; $this->errorm=""; 以下略 ?>    ←122行目(最終行)です。 となっています。両方のプログラムとも、「<?php」の上の行には何も書いていません。 なぜ、headers already sentとなるのでしょうか? エラーの原因はどこにあるでしょうか?

  • PHP
  • 回答数1
  • ありがとう数7

質問者が選んだベストアンサー

  • ベストアンサー
  • little-m
  • ベストアンサー率44% (45/102)
回答No.1

 Plan.class.php の 122行目 ?> の後に、スペース又は改行等が入っていないでしょうか?

spindle
質問者

お礼

「えッ?」ということで調べましたら、ご明察の通りでした。無事、解決して、次に進むことがました。 本当に有難うございました。

関連するQ&A

  • unexpected T_REQUIRE とは?

    <? php require('Smarty.class.php'); require('Logging.class.php'); …・ と書いてあるlogging_gui.phpファイルをブラウザで開こうとすると、 Parse error: syntax error, unexpected T_REQUIRE in C:\Program Files\Apache Group\Apache2\**********\logging_gui.php on line 3 というエラーが出ます。 PHPマニュアルでT_REQUIREを調べてみましたが、意味が分かりません。 テンプレートはtemplates_cにコンパイルされています。 何が不具合であると言っているのでしょうか? Logging.class.phpの中身に間違いがあるということでしょうか? エラーをなくすには何をチェックすればよいのでしょうか? 超初歩的、かつ漠然とした質問かも知れませんが、よろしくお願いします。

    • 締切済み
    • PHP
  • tplを読みに行けません。

    PHP+smartyを学んでいます。 Apache2-htdocs-taro-hanako.php とファイルを置き、一方、 Apache2-php_libs-smarty の下に -libs/ (中にSmarty.class.php あり) と -templates/ (中にtest.tpl あり)を並列的におきました。 hanako.php に、 require_once "../../php_libs/smarty/libs/Smarty.class.php"; $smarty = new Smarty; $smarty->templete_dir = "../../php_libs/smarty/templates";   $smarty->compile_dir = "../../php_libs/smarty/templates_c"; $smarty->cache_dir = "../../php_libs/smarty/cache"; 以下省略・・・ と書いてブラウザで開くと、次のエラーが出て、tpl を読み込むことが出来ません。 Warning: Smarty error: unable to read resource: "test.tpl" in C:\Program Files\Apache Group\Apache2\php_libs\smarty\libs\Smarty.class.php on line 1088 どこを間違えているのでしょうか。

    • ベストアンサー
    • PHP
  • PHP SQLiteの命令文

    お尋ねします。 [Plan.class.php]で ・・・・・・・・ if($this->db = sqlite_popen($this->DBfile,0440,$sqliteerror)){ $this->qstring= "select place,title,contents from schedule where idno = {$this->idno} and hiduke = '{$this->totodate}'"; $this->qresult = sqlite_query($this->db,$this->qstring); if(0==sqlite_num_rows($this->qresult)){ $this->oplace=""; $this->ocontents=""; $this->button="登録"; }else{ ・・・・・・・・ と書いて実行すると、 Warning: sqlite_query() [function.sqlite-query]: near "and": syntax error in C:\Program Files\Apache Group\*****\Plan.class.php on line 55 ( $this->qresult = sqlite_query($this->db,$this->qstring;の行です。) Warning: sqlite_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files\Apache Group\*****\Plan.class.php on line 56 ( if(0==sqlite_num_rows($this->qresult)){ の行です。) というエラーが出ます。この文はどこか拙いでしょうか?

    • 締切済み
    • PHP
  • php session_start()

    いつもお世話になっております PHP session_start()について質問させていただきます。 ブラウザで確認すると以下のワーニングが表示されます。 Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at ファイル名) in ファイル名 過去の質問を参考にしてphpinfo()を使用してサーバの設定を確認したところ、Session Supportはenabledとなっております。 また、余分なスペースなどが無いよう、コードを1行にまとめて試してみましたが同じ結果でした。 コードは以下のとおりです。ちなみにサーバはレンタルサーバを使用しております。 ■index.php <?php  $documentroot = dirname( __FILE__) . '/';  require_once $documentroot . '../php_libs/config/config.inc.php';  require_once("Auth/Auth.php");  require_once("HTML/QuickForm.php");  require_once("HTML/QuickForm/Renderer/ArraySmarty.php");  require_once( _SMARTY_LIBS_DIR . "Smarty.class.php");  require_once( _MODULE_DIR . "build_screen.inc.php");  require_once( _MODULE_DIR . "menu.inc.php");  require_once( _MODULE_DIR . "MYDB.inc.php");  require_once( _MODULE_DIR . "sub.inc.php");  main(_MEMBER_FLG);  exit; ?> ■main関数 <?php  function main($systemflg){   // セッション名の設定   $sess_name = ($systemflg) ? _SYSTEM_SESSNAME : _MEMBER_SESSNAME;   session_name($sess_name);   // セッション開始   session_start(); ~以下省略~ どこに問題があるかお分かりの方いらっしゃいましたら、ご回答のほどよろしくお願いいたします。

    • ベストアンサー
    • PHP
  • 会員管理システムを作っているのですが、エラーが出て困っています。

    phpとsmartyで会員管理システムを構築しているのですが、以下のようなエラーが出て困っています。 エラー文 Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\Login.class.php:1) in C:\xampp\htdocs\login.php on line 5 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\Login.class.php:1) in C:\xampp\htdocs\login.php on line 5 Warning: Smarty error: unable to read resource: "login.tpl" in C:\xampp\htdocs\Smarty.class.php on line 1092 このエラー文は何と言っているのですか? また、解決方法が分かる方、ご回答お願いします。

    • 締切済み
    • PHP
  • PDOクラスを使いたいが、peclからPDOをインストールできない。

    PDOのクラスライブラリを読み込むために peclからPDOをインストールする必要があるが、インストールができない。エラーと表示される。php.iniをかなりいじり、phpinfoにもPDO,pdo_mysql,PDO_ODBC,pdo_pgsql,pdo_sqliteが表示されているのに、なぜか動かない。 ■下記はコマンドプロンプト >cd c:\ php c:\php>pecl install pdo downloading PDO-1.0.3.tgz ... Starting to download PDO-1.0.3.tgz(52,613 bytes) ..............done:52,613bytes 12 source files,building ERROR: The DSP PDO.dsp does not exist. ■動かしたいプログラムのコード(PHP辞典(山田祥寛著)から参照・引用) <?php require_once("C:\Program Files/Apache Group/Apache2/php_libs/Smarty/libs/Smarty.class.php"); $db=new PDO("sqlite:",dirname(__FILE__)."/sample.sqlite"); $rs=$db->query("SELECT * FROM books"); $o_smarty=new Smarty(); $o_smarty->caching=2; $o_smarty->template_dir="./templates"; $o_smarty->compile_dir="./templates_c"; $o_smarty->cache_dir="./cache"; $o_smarty->assign("data".$rs->fetchAll(PDO::FETCH_ASSOC)); $o_smarty->display("foreach.tpl"); print ($o_Smarty->fetch("foreach.tpl")); ?> ■そして、そのコードがブラウザから表示するエラーメッセージ Fatal error: Uncaught exception 'PDOException' with message 'safe_mode/open_basedir prohibits opening ' in C:\Program Files\Apache Group\Apache2\htdocs\dis.php:4 Stack trace: #0 C:\Program Files\Apache Group\Apache2\htdocs\dis.php(4): PDO->__construct('sqlite:', 'C:\Program File...') #1 {main} thrown in C:\Program Files\Apache Group\Apache2\htdocs\dis.php on line 4 下記のご回答をお願いします。 (1)コマンドプロンプトに表示されるエラーはどんな意味でしょうか。何がいけないとか物足りないことがありますでしょうか。 (2)そのコードのおかしいところがあったらご指摘をお願いします。 (3)コンパイルエラーメッセージを翻訳したのですが、ちょっと意味がわからないので、教えてください。 ご回答をよろしくお願いします。かなり困っています。

    • ベストアンサー
    • PHP
  • smartyのdifineについて

    表示するファイルにdefineにてパスを設定すると正常に 表示されるのですが管理が面倒なので別のファイルに defineにてパスを設定するとエラーになってしまいます。 具体的には以下のソースです。 ---Smarty/libs/path.php--- define ('SMARTY_LIBS_DIR', 'C:/Program Files/xampp/htdocs/Smarty/libs/'); ---Smarty/libs/MySmarty.class.php--- require_once dirname(__FILE__) . '/Smarty.class.php'; require_once dirname(__FILE__) . '/path.php'; ---Smarty/index.php--- require_once SMARTY_LIBS_DIR . 'MySmarty.class.php'; 調べてみたのですが表示するファイルに指定する方法しか みつからなかったのですが別ファイルから指定する事は 無理なのでしょうか?

    • 締切済み
    • PHP
  • session_startについて

    初心者です。 以下のように、ログイン時、設定画面adminpage.htmlに行くように作っています。 sakuraインターネットでは動くのですが、 もう一方の違うサーバーでは、login.phpに戻ってきてしまいます。 原因が判りません。 手がかりが欲しいのですが、 お詳しい方がおられましたら教えてください。 よろしくお願い致します。 <?php require("MySmarty.class.php"); require('Settings.class.php'); $smarty = new MySmarty(); $settings = new Settings; session_start(); $id = $_SESSION['id']; $pass = $_SESSION['pass']; //idとパスワードに不具合有れば、login.phpへ戻る。 if( ( $id == '' ) || ( !$settings->isValidUser($id,$pass) ) ){ header("Location: login.php" ); print $_SESSION["id"]; print $_SESSION["pass"]; print $id; print $pass; exit; } $settings->loadSettings(); $smarty->assign('maintitle', $settings->maintitle); $smarty->assign('subtitle', $settings->subtitle); $smarty->assign('error_message', $settings->errorm); $smarty->display('adminpage.html'); ?>

    • ベストアンサー
    • PHP
  • PEARのインストール

    初めまして。今日からphpの勉強を始めた超・初心者です。 PEARのインストールがうまくいかず、下記のエラーが出ます。他の回答も試してみましたが、うまくいきません。 Warning: require_once(PEAR/Info.php) [function.require-once]: failed to open stream: No such file or directory in C:\Program Files\Apache Group\Apache2\htdocs\phpbook\pear_info.php on line 2 Fatal error: require_once() [function.require]: Failed opening required 'PEAR/Info.php' (include_path='.;c:\php\includes;c:\php\PEAR;') in C:\Program Files\Apache Group\Apache2\htdocs\phpbook\pear_info.php on line 2 apache2.0.63 php5.1.6 よろしくお願いいたします。

    • ベストアンサー
    • PHP
  • PHP4でのSmarty派生クラスの定義について

    こんにちはよろしくお願いします。 PHP4でSmarty派生クラスを定義する方法を教えてください。 PHP5のサンプルコードで次のようなものがありました。 PHP4では,「private」が使えないらしいとか,「__construct」ではなくてクラス名がコンストラクタ名になるとかでうまくいかないようです。どのように修正すればよいのでしょうか? また,「__destruct」はどのようにすれば良いのでしょうか?コンストラクタ名とデストラクタ名が同じでよいのでしょうか? よろしくお願いします。 <?php require_once("DB.php"); require_once("Smarty/libs/Smarty.class.php"); class MySmarty extends Smarty { private $_db; public function __construct() { $this->Smarty(); $this->template_dir="../templates"; $this->compile_dir="../templates_c"; $this->_db=DB::connect("sqlite://localhost/samples.db"); } public function __destruct() { $this->_db->disconnect(); } public function getDb() {return $this->_db;} } ?>

    • ベストアンサー
    • PHP

専門家に質問してみよう