PHPスクリプトでエンコードエラー

このQ&Aのポイント
  • 貼り付けアンケートを設置した際にエンコードエラーが発生しました。
  • mb_convert_encoding関数と関連するエラーが発生しています。
  • エンコード設定を確認し、エラーの原因を特定する必要があります。
回答を見る
  • ベストアンサー

PHPスクリプトでエンコードエラー

(http://c-php.mine.nu/)で配布されている「貼り付けアンケート」を設置したところ、以下のようなエラーが出ました。 Warning: mb_convert_encoding(): Illegal character encoding specified in /home/toraya-c/public_html/harituke/harituke.php on line 94 Warning: mb_convert_encoding(): Illegal character encoding specified in /home/toraya-c/public_html/harituke/harituke.php on line 95 Warning: mb_convert_encoding(): Illegal character encoding specified in /home/toraya-c/public_html/harituke/harituke.php on line 96 Fatal error: Call to undefined function: mb_check_encoding() in /home/toraya-c/public_html/harituke/harituke.php on line 140 エンコードがうまくいってないようなのですが。。 指定の行数の記述は以下です。 $title = mb_convert_encoding($title,'EUC-JP',$en); $body = mb_convert_encoding($body,'EUC-JP',$en); $quest = mb_convert_encoding($quest,'EUC-JP',$en); if(!mb_check_encoding($body,'EUC-JP') || mb_check_encoding($body,'ASCII')){ 配布されているものを特に変えず(設定部分以外)、そのままupしたつもりですが、エラーの原因がわかりません。 当方、php初心者なので的外れな質問でしたら申し訳ございませんが、よろしくお願いします。

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

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

  • ベストアンサー
  • Suzi
  • ベストアンサー率38% (130/334)
回答No.5

それでしたら、以下のはどうでしょうか。 $gotopage = ''; $gotopage = "$thankyou[$ran]"; if(!empty($gotopage)){ print "<HTML><HEAD>"; print "<meta http-equiv =\"refresh\" content=\"0; url=" . $gotopage. "\">"; print "</HEAD></HTML>"; exit; }

mayuchinco
質問者

お礼

ちゃんと表示されるようになりました! 素晴らしいです。 勉強不足の自分に遅くまで付き合っていただいて何とお礼を申していいやら‥ 本当に助かりました。 ありがとうございました!!

その他の回答 (4)

  • Suzi
  • ベストアンサー率38% (130/334)
回答No.4

ごめんなさい、間違えました。 to_thunkyou();  ↓ if(!empty($gotopage)) to_thunkyou(); ========================== $gotopage = "$thankyou[$ran]"; if(!empty($gotopage)) to_thunkyou(); function to_thunkyou(){ global $gotopage; print "<HTML><HEAD>"; print "<meta http-equiv =\"refresh\" content=\"0; url=" . $gotopage. "\">"; print "</HEAD></HTML>"; exit; } ========================== この箇所だけですが、こちらで確認しましたが特にエラーは出ませんでした。 単にコピペした場合、そのPHPの改行コードや漢字コードの確認、そしてバックスラッシュだけ打ち直すとかしてみてはどうでしょうか。

mayuchinco
質問者

お礼

回答ありがとうございます。 確かに、サンキューページ関係の箇所だけのファイルだとちゃんと表示されるのですが、元ファイルだと全く同じ記述(、かつ階層も同じ)なのに何故かエラーが出ます。 ●エラー箇所 if(!empty($gotopage)) to_thunkyou(); ●エラー文 Fatal error: Call to undefined function: to_thunkyou() (ちなみに、サンキューページ関係の箇所だけのファイルだと if(!empty($gotopage)) to_thunkyou(); の部分は to_thunkyou(); でもエラーは出ませんでした。) 色々アドバイスを頂いたのに、解決できずに申し訳ありません。。 ただ、すごく勉強になりました。 本当にありがとうございました。

  • Suzi
  • ベストアンサー率38% (130/334)
回答No.3

>>header("Location: $thankyou[$ran]");  これ昔良くハマりました。  このスクリプトの前に print とかがあるとエラーが出ます。これはPHPのセキュリティ仕様です。 ------------------------------------------------ header("Location: $thankyou[$ran]"); exit(); ------------------------------------------------ ↑を以下のと入れ替えるとどうでしょうか。 ここのところはfunctionにして、 $gotopage = ''; $gotopage = "$thankyou[$ran]"; to_thunkyou(); function to_thunkyou(){ global $gotopage; print "<HTML><HEAD>"; print "<meta http-equiv =\"refresh\" content=\"0; url=" . $gotopage. "\">"; print "</HEAD></HTML>"; exit; }  変わらず試していませんが、済みません。

mayuchinco
質問者

お礼

回答ありがとうございます。 入れ替えてみたところ、下記のエラーが出ました。 Fatal error: Call to undefined function: to_thunkyou() in /home/toraya-c/public_html/harituke/harituke.php on line 184 184行目とは to_thunkyou(); の部分です。

  • Suzi
  • ベストアンサー率38% (130/334)
回答No.2

>140行目のエラーについてはまだよく分かりません。 ($title, 'EUC-JP', 'ASCII,JIS,UTF-8,EUC-JP,SJIS') if(!mb_check_encoding($body,'EUC-JP') || mb_check_encoding($body,'ASCII')){   ↓ if( mb_check_encoding($body,'EUC-JP') != true || mb_check_encoding($body,'ASCII') == true){  ではどうでしょうか?試していませんです。  ただ、これだと「$bodyがEUC-JP以外、または、$bodyがASCIIだったら」の意味になり結果的に if( mb_check_encoding($body,'EUC-JP') != true){ の「EUC-JP以外は」と同じになると思われますが。

mayuchinco
質問者

お礼

回答ありがとうございます。 早速書き換えてみましたが、エラーは変わらずでした。

mayuchinco
質問者

補足

すみません。 お礼欄に書いた「エラーは変わらず」というのは勘違いでした。 この補足欄で訂正させていただきます。 140行目のエラーにつきましては、回答していただいた書き換えで解決したようです。 ありがとうございます。 しかし、また新たなエラーが。。 このスクリプトはアンケートに答えたらサンキューページ(ランダム)に飛ぶという設定がしてあるのですが、今のままではアンケートに答えてもサンキューページに飛んでくれません。 以下、サンキューページ関係の箇所 //サンキューページ。半角カンマで区切るとランダムに飛ぶ define(THANKS,"thanks1.html,thanks2.html,thanks3.html"); //サンキューページをランダムに $thankyou = explode(',',THANKS); list($msec,$sec) = split(' ',microtime()); mt_srand($msec*100000); $thcount = count($thankyou) - 1; //サンキューページに飛ぶ $ran = mt_rand(0,$thcount); header("Location: $thankyou[$ran]");   ← exit(); エラーが出ているのは「←」のところです。 ちなみにエラー文は、 Warning: Cannot modify header information - headers already sent by (output started at /home/toraya-c/public_html/harituke/harituke.php:2) in /home/toraya-c/public_html/harituke/harituke.php on line 181 個人的には、サンキューページの表示はランダムでなくて1ページだけでも構わないのですが、 書き換えの方法が分からないのでそのままにしてあります。 勉強不足で大変申し訳ございませんが、 もしお分かりの方いらっしゃいましたら、ご教授願います。

  • Suzi
  • ベストアンサー率38% (130/334)
回答No.1

「$en」が良く分かりませんが、以下のではどうでしょうか。 ($title,'EUC-JP',$en);  ↓ ($title, 'EUC-JP', 'ASCII,JIS,UTF-8,EUC-JP,SJIS') 'ASCII,JIS,UTF-8,EUC-JP,SJIS' を 'auto' にすると上手く変換できない場合があります。

mayuchinco
質問者

お礼

早速の回答どうもありがとうございます! 仰るとおり、変更してみたところ94~96行目のエラーについては解決できました。 しかし、140行目のエラーについてはまだよく分かりません。 ネットで検索したりして、付け焼刃で対応しているもので‥。 ありがとうございました。

関連するQ&A

  • PHP + SQLite 文字化けについて

    SQLite と php5でデータベース管理画面を作っていますが、 日本では正常に表示されますが、 中国からアクセスすると文字化けしてしまいます。 文字はソース上で下記のように設定しています。 mb_convert_encoding($aaa,"SJIS","EUC-JP"); SQLiteが、SJISに対応していないと思い、 monospaceに変換するように下記のように変更しました。 mb_convert_encoding($aaa,"monospace","EUC-JP"); しかし、画面上で Warning: mb_convert_encoding() [function.mb-convert-encoding]: Unknown encoding "monospace" in C:\Apache Group\Apache2\htdocs\aaa\aaa.php on line XXX と出ます。 なにか解決策をご存知の方いらっしゃいましたら お教えいただけると幸いです。 どうぞよろしくお願いいたします。 なお、ページ自体の文字設定は、 <meta http-equiv="Content-Type" content="text/html; charset=S-JIS">となっているところ、 ソース変更にともないこちらも、charset=monospace">と変更しました。

    • 締切済み
    • PHP
  • PHPエラーについて

    PHPのエラーについてご質問させていただきます。 【エラー詳細】 Warning: Missing argument 2 for wpdb::prepare(), called in /home/・・・・・/public_html/wordpress/wp-content/plugins/category-icons/category_icons.php on line 1338 and defined in /home/・・・・・/public_html/wordpress/wp-includes/wp-db.php on line 990 調査結果下記の記事と同じ症状な気がします http://dwi.blog101.fc2.com/blog-entry-205.html category icons というプラグインのエラーで新しいVerもでていないようです。 http://www.category-icons.com/ どのようにすればエラーが解消され正常に動作するのかが全く分からず質問させていただきました。 どうぞよろしくお願いいたします。

    • 締切済み
    • PHP
  • phpとpostgreの接続にエラー解決方法について

    始めまして、PHPの勉強始まったばかりのものです。以下のエラーを解決するにはどうすればよいでしょうか? Warning: pg_connect() [function.pg-connect]: Unable to connect to PostgreSQL server: FATAL: database "touroku" does not exist in /home/k0410user/public_html/111/inc/include/init.inc on line 2 Warning: pg_query(): supplied argument is not a valid PostgreSQL link resource in /home/k0410user/public_html/111/inc/include/init.inc on line 6 Warning: pg_num_rows(): supplied argument is not a valid PostgreSQL result resource in /home/k0410user/public_html/111/inc/include/init.inc on line 8 Warning: pg_close(): supplied argument is not a valid PostgreSQL link resource in /home/k0410user/public_html/111/inc/include/init.inc on line 13

    • 締切済み
    • PHP
  • PHPのエラー「Unexpected character ...」「syntax error, unexpected ...」について

    PHP Warning: Unexpected character in input: ''' (ASCII=39) state=1 in /home/***/***.php on line 25 PHP Parse error: syntax error, unexpected '<' in /home/***/***.php on line 26 という2行のエラーが表示されました。 まったく明快な答えは探せませんでした。 もしも少しでも手がかりになるような情報がありましたら教えて下さい。 お願いします。

    • 締切済み
    • PHP
  • 次のエラーの説明をお願いします。

    エラーがでて次の掲示板がひらけません。 http://www.inter-edu.com/forum/ という受験掲示板ですが,開こうとすると次のエラーがでてひらけません。どなたか原因を教えていただけませんか? Warning: mysql_connect(): Too many connections in /home/homepage/public_html/forum/include/db/mysql.php on line 3025 Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/homepage/public_html/forum/include/db/mysql.php on line 3026 Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/homepage/public_html/forum/include/db/mysql.php on line 902 Warning: mysql_errno(): supplied argument is not a valid MySQL-Link resource in /home/homepage/public_html/forum/include/db/mysql.php on line 904 Too many connections: select * from phorum_settings

  • PHPエラーコードについて

    このようなエラーの場合どうすればいいのでしょうか?どなたか宜しくお願いいたします。 Warning: main(pager/pager.php) [function.main]: failed to open stream: No such file or directory in /home/○○○○/public_html/server/app/○○○○/○○○○.php on line 10 Fatal error: main() [function.require]: Failed opening required 'pager/pager.php' (include_path='../../pear:../../smarty:.:/usr/local/lib/php') in /home/○○○○/public_html/server/app/○○○○/○○○○.php on line 10

    • 締切済み
    • PHP
  • 多次元配列をエンコードする関数を作るには?

    多次元配列でも文字コードのエンコードができる関数を 作ろうとしていますが、エラーで躓いています。 自分自身の関数を再帰で呼び出しているので、array_map関数上? 引数が1つしか持てないので、$valueを array($ary, "SJIS", "EUC-JP")として渡しています。 これを実行すると、以下のエラーになります。 何かよい方法はありますか? PHP Notice: Undefined offset: 0 in C:\test.php on line 10 function mb_convert_encoding_deep($value) { //var_dump($value); if( is_array($value) ){ if( is_array($value[0]) ){ array_map(__FUNCTION__, $value); }else{ $value[0] = mb_convert($value[0], $value[1], $value[2]); } }else{ die("ここは通過しないと思いますが自信なし"); } return $value; } $ary = mb_convert_encoding_deep( array($ary, "SJIS", "EUC-JP") ); array_mapの用法でも間違っているのでしょうか?

    • ベストアンサー
    • PHP
  • PHP+Postgres 「髙」が文字化け

    DB上に格納した文字列を取得し、PHPで出力すると、 「髙」(はしごだか)等、一部の文字が「□・」のような見た目に化けてしまいます。 環境は以下です。 Linux(CentOS 5) PHP 5.1.6 Apache 2.2.3 PostgreSQL 8.4.3 文字コードは以下です。 Postgres:EUC-JP PHPソース:EUC php.ini の[mbstring]はコメントのまま変更していません。 文字コードの変換を試してみたのですが、「髙」としては出力できませんでした。 Windows上では「CP51932」に変換すると正常に出力できましたが、Linux上ではやはり駄目でした。 mb_language("uni"); mb_internal_encoding("euc-jp"); mb_http_input("auto"); mb_http_output("euc-jp"); $str = "髙橋"; print(mb_convert_encoding($str, "EUCJP-win")); print(mb_convert_encoding($str, "EUCJP-win","EUC-JP")); print(mb_convert_encoding($str, "SJIS")); print(mb_convert_encoding($str, "SJIS","EUC-JP")); print(mb_convert_encoding($str, "SJIS-win")); print(mb_convert_encoding($str, "SJIS-win","EUC-JP")); print(mb_convert_encoding($str, "UTF-8")); print(mb_convert_encoding($str, "UTF-8","EUC-JP")); print(mb_convert_encoding($str, "EUC","SJIS")); print(mb_convert_encoding($str, "Unicode")); print(mb_convert_encoding($str, "Unicode","EUC-JP")); print(mb_convert_encoding($str, "UTF-8", "sjis-win")); print(mb_convert_encoding($str, "CP51932")); print(mb_convert_encoding($str, "MS932")); print(mb_convert_encoding($str, "MS932","EUC-JP")); print(mb_convert_encoding($str, "UTF-8","EUCJP-win")); print(mb_convert_encoding(mb_convert_encoding($str, "EUCJP-win" ), "UTF-8","EUCJP-win")); print(mb_convert_encoding(mb_convert_encoding($str, "EUCJP-win","EUC-JP"), "UTF-8","EUCJP-win")); print(mb_convert_encoding(mb_convert_encoding($str, "SJIS-win" ), "UTF-8","SJIS-win")); print(mb_convert_encoding(mb_convert_encoding($str, "SJIS-win","EUC-JP"), "UTF-8","SJIS-win")); print(mb_convert_encoding(mb_convert_encoding($str, "SJIS-win" ), "EUCJP-win","SJIS-win")); print(mb_convert_encoding(mb_convert_encoding($str, "SJIS-win","EUC-JP"), "EUCJP-win","SJIS-win")); http://blog.livedoor.jp/loopus/archives/50160285.html 上記サイトによると、解決法は「EUC-JPで出力しないこと」という事でしたが、 文字コードを変換して出力できるような事を書いているサイトもあり、結論が出せずにいます。 ソースを書き換えるのは避けたいのですが、いい方法がありましたらご教示ください。

    • ベストアンサー
    • PHP
  • mb_convert_encodingについて

    Fatal error: Call to undefined function: mb_convert_encoding() in c:\apache\htdocs\test\03-03\common.php on line 27 上記のエラーがでます。 mb_convert_encoding[文字エンコーディングを変換する] を利用するにはphp.iniで設定の変更などありますか?

    • ベストアンサー
    • PHP
  • PHPエラーについて教えてください

    PHPエラーについて教えてください GD関数のエラーだと思います。 サーバを移動したら以下のようなエラーが出ました。 どのようなエラーになるのでしょうか。 Warning: imagesx(): supplied argument is not a valid Image resource in /home/sites/heteml/xxx/xxx.php on line 99 Warning: imagesy(): supplied argument is not a valid Image resource in /home/sites/heteml/xxx/xxx.php on line 99 Warning: imagecopyresized(): supplied argument is not a valid Image resource in /home/sites/heteml/xxx/xxx.php on line 99 Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/sites/heteml/xxx/xxx.php on line 100

    • ベストアンサー
    • PHP

専門家に質問してみよう