Cookieの設定エラーメッセージとは?

このQ&Aのポイント
  • PHPでCookieにデータを設定しているのですが、エラーメッセージが出て正常に設定できません。
  • セットしたCookieの値がうまく保存されず、エラーメッセージが表示されます。
  • PHPファイルの出力が既に始まっているため、ヘッダ情報を変更できないというエラーです。
回答を見る
  • ベストアンサー

Cookieの設定について

PHPでCookieにデータを設定しているのですが以下のエラーメッセージが出て 正常に設定できません何か悪いのでしょうか? よろしくお願いします。 ○エラーメッセージ Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\index.php:59) in C:\xampp\htdocs\index.php on line 340 Cokkie NG ○ソースの関連部分 index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <title>SAMPLE </title> <meta http-equiv="Pragma" content="No-Cashe" /> <meta http-equiv="Cache-Control" content="No-Cache" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <link type="text/css" rel="stylesheet" href="search.css" /> <?php $r = setcookie('win_width','800',time()+3600,"/"); if ($r) { echo'<p>Cokkie OK</p>'; } else { echo'<p>Cokkie NG</p>'; } ?> </body> <HEAD> <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"> </HEAD> </html>

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

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

  • ベストアンサー
  • yambejp
  • ベストアンサー率51% (3827/7415)
回答No.1

>正常に設定できません何か悪いのでしょうか? マニュアル見ればわかりますがクッキーを設定できるのはあらゆる出力の前です。 (httpヘッダを利用しているから?) 文字だけでなく改行一つでも、極端にいえばBOM一つでもはいっても 仕様上エラーになります。 出力後クッキーを設定したいなら、javascriptでやってください。

参考URL:
http://www.php.net/manual/ja/function.setcookie.php
tatotu
質問者

お礼

ありがとうございます。 そうですね、出力前ですね。 javascript でやってみます。

関連するQ&A

  • 初歩的な質問で恐縮ですが、教えてください。

    すみませんが、教えてください。 入力フォームでデータが送れなくて、困っています。 下のコードに問題点はあるのでしょうか? 自分では、分からないので、よろしくお願いいたします。 test1.php------------------- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> </head> <body> <form method="post" enctype="multipart/form-data" action="./test2.php"> <input type="text" name="fmTitle" ><br /> <input type="submit" value="次へ" > </form> </body> test2.php-------------------------- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> </head> <body> <?= $fmTitle . "<br />" ?> </body>

    • ベストアンサー
    • PHP
  • 下記のタグの中で必要ないのはありますか?

    (1) <?xml version="1.0" encoding="UTF-8"?> (2) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> (3) <html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja"> (4) <head> (5) <title>●●●●●●●●</title> (6) <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> (7) <meta http-equiv="Content-Style-Type" content="text/css" /> (8) <meta name="robots" content="INDEX,FOLLOW"> (9) <meta name="description" content="●●●●●●●●"> (10) <meta name="keywords" content="●●●●●●●●"> (11) <meta name="content="●●●●●●●●" /> (12) <meta http-equiv="Content-Script-Type" content="text/javascript"> (13) <meta http-equiv="Content-Style-Type" content="text/css"> (14) <link href="css/A1.css" rel="stylesheet" type="text/css" /> (15) <META http-equiv="Content-Type" content="text/html; charset=Shift_JIS">

  • 携帯サイトでソフトバンクだけ接続されない

    携帯サイトを作っています。各キャリアのエミュレーター、firefoxのアドオン「FireMobieSimulator」では問題なく表示されるのですが、ソフトバンクの実機ではなぜか接続されません。機種によっては「お客様の端末からは接続できません」というようなエラーメッセージが出ます。 最初は、絵文字変換スプリクトのせいかと思い、それを削除した状態でアップロードしてもつながりません。 ソースは下記のようになっています。 何か問題がありましたら、教えていただければと思います。 <?xml version="1.0" encoding="Shift_JIS"?> <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja"> <head> <meta http-equiv="Content-Type" content="application/xhtml+xml ; charset=Shift_JIS" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="pragma" content"no-cache" /> <meta http-equiv="cache-control" content="no-cache" /> <meta http-equiv="expires" content="-1" /> <meta name='robots' content="all"> <meta name='description' content=""> <meta name="keywords" content=""> <style type="text/css"> <!-- a, a:link {color:#000099;} a:visited {color:#000099;} a:active {color:#ffffff;} a:hover {color:#663399;} --> </style> <title>test</title> </head> <body> </body> </html>

    • ベストアンサー
    • HTML
  • Shift_JISのことろをすべてsjisに

    しても大丈夫でしょうか? たとえば以下のように書いて大丈夫でしょうか? 本来どのように書くべきでしょうか? <?xml version="1.0" encoding="sjis"?>のsjisと <meta http-equiv="content-type" content="application/xhtml+xml; charset=sjis"/> のsjisの2箇所です。 <?xml version="1.0" encoding="sjis"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja"> <head> <meta http-equiv="content-type" content="application/xhtml+xml; charset=sjis"/> <meta http-equiv="content-style-type" content="text/css"/> <meta http-equiv="content-script-type" content="text/javascript"/> <title>sjis</title> </head> <body> <p>これは文字化けのテストです</p> </body> </html>

    • ベストアンサー
    • HTML
  • 画面(ウィンドウ)/画像の拡大縮小について

    PHP/JavaScript初心者です。 PHPとJavaScriptと以下のようなことをしてます、 画面(ウィンドウ)上に画像が表示し 「拡大」ボタンを押したら ウィンドウ/画像を拡大して表示 「リセット」ボタンを押したら ウィンドウ/画像を元のサイズに戻してして表示 をしたいのですが、どうすればいいですか? 関連ありそうな、ソースを以下に書きます。 dspimg.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <title>文献表示</title> <meta http-equiv="Pragma" content="No-Cashe" /> <meta http-equiv="Cache-Control" content="No-Cache" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <script type="text/javascript"> <!-- //画面/画像を拡大/縮小 function win_zoom(w,h){ window.resizeTo(w,h) location.reload(); } //--> </script> </head> <body> echo '<img id="dspimg" src="ppp.jpeg" alt="ppp.jpeg" width=600 height=400 " />'; <?php //拡大 ?> <INPUT type="button" Value="拡大" onclick="win_zoom(800,700)" > echo '<div align="center">'; <?php //リセット ?> <INPUT type="button" Value="リセット" onclick="win_reset(600,500)" > </div> </body> </html>

  • phpで数字の値を画像で表示したいのですが

    お世話になります。phpを触り出している初心者です。携帯に対応させたいのですが、phpだけでいっぱいなので助けてください. 数字が0だったら非表示 2だったら2の画像・・・というようなものを作りたいのですが imgタグの埋め込み方・記述方法がよくわかりません。 ↓数字を文字で表示する方法はこれで一応やってみました。 <?php $number1 = 0; $number2 = 2; $number3 = 3; $number4 = 4; if ($number1 == true) { echo"<p>0は非表示です。"; } if ($number2 == true) { echo"<p>1の画像です。"; } if ($number3 == true) { echo"<p>2の画像です。"; } if ($number4 == true) { echo"<p>3の画像です。"; } ?> しかし画像がうまくいかずIE6では一種類の画像だけ表示されて 他の新しいブラウザでは文字化けしてる状態です。 それぞれ違う画像を当てはめる場合 どのように数値を増やして記述したらよいのでしょう? 色々試していますがこの記述も正しいのがわかりません。 <?php $gazou = imagecreatefromgif("img/01.gif"); imagegif($gazou); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja"> <head> <meta http-equiv="content-Type" content="application/xhtml+xml; charset=shift_jis" /> <meta http-equiv="content-style-type" content="text/css" /> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" content="no-cache" /> <meta http-equiv="expires" content="0" /> <title>数字で分岐して画像を表示</title> </head> <body> <div style="text-align:center;"> <?php $number1 = 0; $number2 = 1; $number3 = 2; $number4 = 3; if ($number1 == true) { echo'<img src="gazou.php" />'; } if ($number2 == true) { echo'<img src="gazou.php" />'; } if ($number3 == true) { echo'<img src="gazou.php" />'; } if ($number4 == true) { echo'<img src="gazou.php" />'; } ?> </div> </body> </html>

    • ベストアンサー
    • PHP
  • SafariでPHPソースがそのまま表示される

    Safariでindex.phpを開くとソースコードがそのまま表示されてしまいます。 拡張子をphpとしているだけでソース内にはphpスクリプトは記述しておりません。 なぜでしょうか? ※ファイル名をindex.htmlに変えると正しく表示されます。 Windows 7 Home Premium 64bit Safari5.1.5 ソースコードのエンコーディング:utf-8 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="content-style-type" content="text/css" /> <meta http-equiv="content-script-type" content="text/javascript" /> <meta name="keywords" content="" /> <meta name="description" content="" /> <title>テストtitle> </head> <body> <h1>テストページ</h1> </body> </html>

  • .phpがブラウザで表示されない

    携帯サイトを作成したのですが、PCのブラウザで確認できないので少し不安です。 広告をいれたいため拡張子は.phpで作りました。 携帯では問題なく確認できるのですが、PCでアドレスを入力して確認しようとすると「このファイルを保存しますか、または開くためのプログラムをオンラインで検索しますか?」というポップアップウインドウが表示されて、PCでは確認することができない状態です。 タグは以下に記載します。 どなたか解決法がわかるかた教えて頂けないでしょうか? <?php header('Content-Type: application/xhtml+xml; charset=Shift_JIS'); echo '<?xml version="1.0" encoding="Shift_JIS"?>'."\n" ?> <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd" > <html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja"> <head> <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=Shift_JIS" /> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" content="no-cache" /> <meta http-equiv="expires" content="-1" /> <title>趣味のページ</title> </head>

    • ベストアンサー
    • PHP
  • FirefoxでCSSが読み込めない。

    HPを作成中です。IEやSleipnirでは思った通りに表示されるのですが、Firefoxではスタイルシートが読み込まれていないようです。 どの部分が悪いのでしょうか。ご教示のほどをよろしくお願いいたします。 以下冒頭部分のソースです。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "​http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">​ <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=shift_jis"> <meta http-equiv="Content-Style-type" content="text/css"> <link rel="stylesheet" href="index.css" type="text/css"> <title></title> </head> <body>

    • ベストアンサー
    • HTML
  • headerの添削お願いします。

    自分なりに書いてみたのですがおかしいところがあったら教えてください。 また、採点してくれるサイトがあったと思うのですがご存知の方教えてください。 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml" xml:lang="ja" lang="ja"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <meta http-equiv="content-style-type" content="text/css" /> <meta http-equiv="content-script-type" content="javascript" /> <meta name="keywords" content="content" /> <link rel="shortcut icon" href="/favicon.ico" /> <link rel="stylesheet" href="/css/main.css" type="text/css" /> <title>0725</title> </head>