携帯とPC振り分け

このQ&Aのポイント
  • 携帯からアクセスがあった場合に振り分けできる方法を教えてください。
  • 現在作成中のPCサイトには携帯対応のページが含まれていません。
  • PHP初心者ですが、携帯サイトのソースコードを利用することは可能でしょうか?
回答を見る
  • ベストアンサー

携帯とPC振り分け

現在PCサイトを作っています。 携帯からアクセスがあった場合に振り分け(?)したいと思っていて、現在ソースがこんなかんじです。 .htaccessに DirectoryIndex index.php index.cgi index.html index.shtml と書いてあって、 <?php $mobile = print <<<MOBILE <?xml version="1.0" encoding="Shift_JIS"?> <!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=Shift_JIS" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>携帯には対応していません(HPタイトル)</title> </head> <body> 携帯には対応していません。(HPタイトル) </body> </html> MOBILE; if(isset($_SERVER['HTTP_USER_AGENT'])){ $user_agent = $_SERVER['HTTP_USER_AGENT']; if(eregi("DoCoMo",$user_agent)){$mobile;exit;} else if(eregi("UP\.Browser",$user_agent)){$mobile;exit;} else if(eregi("J-PHONE",$user_agent)){$mobile;exit;} else if(eregi("Vodafone",$user_agent)){$mobile;exit;} else if(eregi("mobile",$user_agent)){$mobile;exit;} else if(eregi("J-EMULATOR",$user_agent)){$mobile;exit;} else{header("Location: index.html");exit;} }else{ print <<<END <html><body> HTTP_USER_AGENT Error<br /><br /> ユーザーエージェントが読み込めませんでした。<br /> </body></html> END; } ?> こんな感じです。 ですが、エラーはいてしまいます。 Warning: Cannot modify header information - headers already sent by (output started at D:\XAMPP\htdocs\index.php:16) in D:\XAMPP\htdocs\index.php on line 26 こんな感じです。 まったくのPHP初心者(HTMLはまあまあわかる)なので だいぶソースが間違っていると思います。 このPHPのソースは携帯サイトnet(http://www.keitai-site.net/)からいただきました。

noname#119508
noname#119508
  • PHP
  • 回答数1
  • ありがとう数1

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

  • ベストアンサー
  • mpx
  • ベストアンサー率71% (149/209)
回答No.1

>> $mobile = print <<<MOBILE 上記行を 下記に変更してみてください(printを削除) $mobile = <<<MOBILE >> if(eregi("DoCoMo",$user_agent)){$mobile;exit;} 上記を下記に変更(printを追加) if(eregi("DoCoMo",$user_agent)){print $mobile;exit;} 以降の5行も同じ($mobileの前にprint追加)

noname#119508
質問者

お礼

動作しました! ありがとうございました。

関連するQ&A

  • 携帯とPCの振り分け

    同じドメインにて携帯とPCの振り分けなのですが、htaccessにて振り分けたのですが、ブラウザに「振り分けがループしています」とでるのですが、 これはなぜでしょう? htaccessの中身の内容ですが、 DirectoryIndex index.php index.html index.htm これで読み込んだindex.phpの中身は <?php $agent = $_SERVER['HTTP_USER_AGENT']; if(preg_match("/^DoCoMo/i", $agent)){ header("Location: http://www.○○○○○.com/mobile/index.xhtml"); exit; }else if(preg_match("/^(J\-PHONE|Vodafone|MOT\-[CV]|SoftBank)/i", $agent)){ header("Location: http://www.○○○○○.com/mobile/index.xhtml"); exit; }else if(preg_match("/^KDDI\-/i", $agent) || preg_match("/UP\.Browser/i", $agent)){ header("Location: http://www.○○○○○.com/mobile/index.xhtml"); exit; }else{ header("Location: http://www.○○○○○.com/"); exit; } ?> サーバはlolipopでhtaccessのパーミッションは604で phpのパーミッションは700です。

    • 締切済み
    • CGI
  • PHP 携帯・PC 振り分け

    こんにちはphpの振り分けについてご教授お願いします。 http://www.abc.jp/→PCサイト(/home/abc/www/の中身) http://m.abc.jp/→mobileサイト(/home/abc/www/mobile/の中身) とした場合、PCからmobileサイトにアクセスがあった場合pcサイトに飛ばし、携帯からPCサイトにアクセスがあった場合mobileサイトに飛ばすといったようなことをしたいのですが、下記の内容でdoctype宣言をおこなった場合どのように記述したらよろしいでしょうか? 下記の記述は別ファイルに記述し、index.phpまたは他の.phpに読み込まして各キャリアをdoctype宣言で振り分けしています。 下記の記述はネット上から見つけてきたものです。 http://www.kamitani79.com/2008/10/doctypephpssi.html <?php $agent=$_SERVER['HTTP_USER_AGENT']; if(preg_match("/DoCoMo/", $agent)) { print ("<?xml version=\"1.0\" encoding=\"Shift_JIS\"?>\n"); print ("<!DOCTYPE html PUBLIC \"-//i-mode group (ja)//DTD XHTML i-XHTML(Locale/Ver.=ja/2.3) 1.0//EN\" \"i-xhtml_4ja_10.dtd\">\n"); print ("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"ja\" xml:lang=\"ja\">\n"); print ("<head>\n"); print ("<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=Shift_JIS\" />"); } elseif(preg_match("/KDDI\-/", $agent)||preg_match("/UP\.Browser/", $agent)) { print ("<?xml version=\"1.0\" encoding=\"Shift_JIS\"?>\n"); print ("<!DOCTYPE html PUBLIC \"-//OPENWAVE//DTD XHTML 1.0//EN\" \"http://www.openwave.com/DTD/xhtml-basic.dtd\">\n"); print ("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"ja\" xml:lang=\"ja\">\n"); print ("<head>\n"); print ("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Shift_JIS\" />"); } elseif(preg_match("/J\-PHONE|Vodafone|MOT\-[CV]980|SoftBank/", $agent)) { print ("<?xml version=\"1.0\" encoding=\"Shift_JIS\"?>\n"); print ("<!DOCTYPE html PUBLIC \"-//J-PHONE//DTD XHTML Basic 1.0 Plus//EN\" \"xhtml-basic10-plus.dtd\">\n"); print ("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"ja\" xml:lang=\"ja\">\n"); print ("<head>\n"); print ("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Shift_JIS\" />"); } else { print ("<?xml version=\"1.0\" encoding=\"Shift_JIS\"?>\n"); print ("<!DOCTYPE html PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.0//EN\" \"http://www.wapforum.org/DTD/xhtml-mobile10.dtd\">\n"); print ("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"ja\">\n"); print ("<head>\n"); print ("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Shift_JIS\" />"); } ?> ご教授お願いいたします。

    • ベストアンサー
    • PHP
  • 携帯で携帯サイトを閲覧すると、なぜかPCサイトが表示されます。

    携帯で携帯サイトを閲覧すると、なぜかPCサイトが表示されます。 お世話なります。素人ですので説明にいたらない点が多々あると思いますが、どうか宜しくお願いします。 タイトルが分かりずらくてすみません。どういったことかといいますと、携帯から直接アドレスをひくと自分の携帯サイトが表示されるのに、auのEZwebのau oneトップから自分の携帯サイトを検索し表示させますと、なぜか、自分のPCサイトに飛んでしまいます。 なぜでしょうか? 以下の記述のindex.phpで同じドメインで携帯へ飛ばしています。 <?php /////////////////////////////////////////▼初期処理▼ //http://から始まるURLを指定 $docomo = "http://www.abc.com/m/index.html"; //ドコモの場合 $au = "http://www.abc.com/m/index.html"; //auの場合 $softbank = "http://www.abc.com/m/index.html"; //ソフトバンクの場合 $msie = "http://www.abc.com/index.html"; //IEの場合 $Netscape = "http://www.abc.com/index.html"; //Netscapeの場合 $opera = "http://www.abc.com/index.html"; //Operaの場合 $firefox = "http://www.abc.com/index.html"; //Firefoxの場合 $pc = "http://www.abc.com/index.html"; //上記機種以外 /////////////////////////////////////////▲初期処理終端▲ /////////////////////////////////////////▼携帯判別▼ if(isset($_SERVER['HTTP_USER_AGENT'])){ $user_agent = $_SERVER['HTTP_USER_AGENT']; if(eregi("DoCoMo",$user_agent)){header("Location: $docomo");} elseif(eregi("UP\.Browser",$user_agent)){header("Location: $au");} elseif(eregi("J-PHONE",$user_agent)){header("Location: $softbank");} elseif(eregi("Vodafone",$user_agent)){header("Location: $softbank");} elseif(eregi("SoftBank",$user_agent)){header("Location: $softbank");} elseif(eregi("J-EMULATOR",$user_agent)){header("Location: $softbank");} elseif(eregi("MSIE",$user_agent)){header("Location: $msie");} elseif(eregi("Netscape",$user_agent)){header("Location: $netscape");} elseif(eregi("Opera",$user_agent)){header("Location: $opera");} elseif(eregi("Firefox",$user_agent)){header("Location: $firefox");} else{header("Location: $pc");} }else{ print <<<END <html><body> HTTP_USER_AGENT Error<br /><br /> ユーザーエージェントが読み込めませんでした。<br /> </body></html> END; } /////////////////////////////////////////▲携帯判別終端▲ ?> あと、なぜかindex.phpよりindex.htmlを先によんでしまうようなので、.htaccessで以下のように記述してindex.phpを先に読ませています。 DirectoryIndex index.php index.html 宜しくお願いします。

    • ベストアンサー
    • PHP
  • 携帯、スマートフォンのPHP振り分け

    こんなPHPを書いているのですが どうしてもParse errorが出ます。 解決方法を教えて下さい!! ■エラー内容 Parse error: syntax error, unexpected T_EXIT in /home/sites/heteml/users/web/m/index.php on line 5 ■振り分けPHP <?php $agent = $_SERVER['HTTP_USER_AGENT']; if(preg_match("/^DoCoMo/i", $agent)){ header("Location: ​http://www.hogehoge.com/m/i/index.html");​ exit; }else if(preg_match("/^(J\-PHONE|Vodafone|MOT\-[CV]|SoftBank)/i", $agent)){ header("Location: ​www.hogehoge.com/m/s/index.html");​ exit; }else if(preg_match("/^KDDI\-/i", $agent) || preg_match("/UP\.Browser/i", $agent)){ header("Location: ​www.hogehoge.com/m/e/index.html");​ exit(); }else if(preg_match("iPhoneやスマートフォン用の判定", $agent)){ header("Location: www.hogehoge.com/m/smt/index.html"); exit(); }else{ header("Location: ​www.hogehoge.com/m/pc/index.html");​ exit(); } ?> よろしくお願い致します。

    • ベストアンサー
    • PHP
  • ユーザーエージェントの振り分け

    すみません、エージェント振り分けで以下のようにPHPファイルを作りました。 そして各ホルダ i,s にindex.htmlを配置したところ Warning:Cannot modify header information headers already sent by (output started at /home/lolipop,jp-dp44253830/web/mobile/index.php:1) in/home/sites/lolipop.jp/users/lolipop.jp-dp44253830/web/mobile/index.php on line 4 index.php <?php $agent = $_SERVER['HTTP_USER_AGENT']; if(preg_match("/^DoCoMo/i", $agent)){ header("Location: http://homepege.net/mobile/i/index.html"); exit; }else if(preg_match("/^(J\-PHONE|Vodafone|MOT\-[CV]|SoftBank)/i", $agent)){ header("Location: http://homepage.net/mobile/s/index.html"); exit; 省略 } ?> お手数ですが教えてください。

    • ベストアンサー
    • PHP
  • phpでエージェントの振り分けがうまくいかない

    phpでエージェントの振り分けがうまくいかない 以下のようにmフォルダの中にindex.phpを以下の内容で設置し、同列でi,a,sフォルダを設け それぞれindex.htmlを設けました。しかし携帯でhttp://hoge.com/m で打つとサイトがみつかりませんになります。<?phpより上は空白、改行はありません。 振り分けがうまく機能しないのはなぜでしょうか? <?php $agent = $_SERVER['HTTP_USER_AGENT']; if(preg_match("/^DoCoMo/i", $agent)){ header("Location: http://hoge.com/m/i/index.html"); exit; }else if(preg_match("/^(J?-PHONE|Vodafone|MOT?-[CV]|SoftBank)/i", $agent)){ header("Location: http://hoge.com/m/s/index.html"); exit; }else if(preg_match("/^KDDI?-/i", $agent) || preg_match("/UP?.Browser/i", $agent)){ header("Location: http://hoge.com/m/a/index.html"); exit; }else{ header("Location: http://hoge.com/index.php"); exit; } ?>

    • ベストアンサー
    • PHP
  • 次のPHP BASIC認証画面で

    My Realmですよ が文字化けするのですがどうすればいいのでしょうか? このphpをFC4ーPCに配置してWindowsMeーPCのブラウザでこのphpを表示させています <?php echo' <?xml version="1.0" encoding="utf-8"?> <!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=utf-8"/> <meta http-equiv="content-style-type" content="text/css"/> <title>goo</title> </head> <body> '; if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authenticate:Basic realm="My Realmですよ"'); header('HTTP/1.0 401 Unauthorized'); echo "ユーザーがキャンセルボタンを押した時に送信されるテキスト\n"; exit; } else { echo "<p>こんにちは、{$_SERVER['PHP_AUTH_USER']} さん。</p>"; echo "<p>あなたは、{$_SERVER['PHP_AUTH_PW']} をパスワードとして入力しました。</p>"; } echo' </body> </html> '; ?>

    • 締切済み
    • PHP
  • .htaccessで携帯とPCの振り分け

     .htaccessで携帯サイトとPCサイトの振り分けで困っています。 .htaccessは以下の通りです。 <IfModule mod_rewrite.c> RewriteEngine On # SoftBank RewriteCond %{HTTP_USER_AGENT} ^J-PHONE [OR] RewriteCond %{HTTP_USER_AGENT} ^Vodafone [OR] RewriteCond %{HTTP_USER_AGENT} ^MOT- [OR] RewriteCond %{HTTP_USER_AGENT} ^SoftBank RewriteRule ^$ /mobile/[R=302,L] # DoCoMo RewriteCond %{HTTP_USER_AGENT} ^DoCoMo RewriteRule ^$ /mobile/[R=302,L] # au(HDML-Only) RewriteCond %{HTTP_USER_AGENT} ^UP.Browser RewriteRule ^$ /mobile/[R=302,L] # au(HTML-OK) RewriteCond %{HTTP_USER_AGENT} ^KDDI RewriteRule ^$ /mobile/[R=302,L] </IfModule>  これで、PCからのアクセスはどのページへも正常なのですが、携帯からのアクセスはhttp://example.comですとちゃんと携帯サイトのindex.htmlへいくのですが、http://example.com/***.htmlで携帯からアクセスするとPCサイトのhttp://example.com/***.htmlへつながってしまいます。この場合、携帯サイトのindex.htmlへ導くためにはどのようにすればよいのでしょうか。  また、欲を言えば、PCサイトのそれぞれのページにアクセスしてきた携帯をindex.htmlではなく、携帯サイトのそれぞれの該当するページへと導くためにはどのようにしたらよいのでしょうか。  まったくの初心者ですが、よろしくお願いいたします。

  • i Phone 用振り分け

    以下のように携帯キャリアごとに振り分け、3キャリア以外は PCフォルダに振り分けるようにした場合ですが、PCフォルダの方で i Phone 用に幅480pxで作成したページを配置すると PCから http://mypage.net/ でアクセスしたユーザーにも i Phone 用の幅480pxページを見せるようになってしまいますか? i Phoneやスマートフォンの対処はどうされているんでしょうか? <?php $agent = $_SERVER['HTTP_USER_AGENT']; if(preg_match("/^DoCoMo/i", $agent)){ header("Location: http://mypage.net/i/index.html"); exit; }else if(preg_match("/^(J\-PHONE|Vodafone|MOT\-[CV]|SoftBank)/i", $agent)){ header("Location: http://mypage.net/s/index.html"); exit; }else if(preg_match("/^KDDI\-/i", $agent) || preg_match("/UP\.Browser/i", $agent)){ header("Location: http://mypage.net/e/index.html"); exit; }else{ header("Location: http://mypage.net/pc/index.html"); exit; } ?>

    • ベストアンサー
    • PHP
  • 携帯サイトを作成しているのですが、DOCTYPE宣言で

    携帯サイトを作成しているのですが、DOCTYPE宣言で <!DOCTYPE HTML PUBLIC \"-//i-mode group (ja)//DTD XHTML i-XHTML(Locale/Ver.=ja/2.3) 1.0//EN\" \"i-xhtml_4ja_10.dtd\"> (ドコモ使用時) だけがページに表示されてしまいます。書き方に間違いがあるのでしょうか? 又、これは受け入れられてないのでしょうか? どなたかわかる方アドバイスよろしくおねがいします。 ↓プログラム添付 <?php $agent=$_SERVER['HTTP_USER_AGENT']; if(preg_match("/DoCoMo/", $agent)) { print("<?xml version=\"1.0\" encoding=\"Shift_JIS\"?>\n"); print ("<!DOCTYPE HTML PUBLIC \"-//i-mode group (ja)//DTD XHTML i-XHTML(Locale/Ver.=ja/2.3) 1.0//EN\" \"i-xhtml_4ja_10.dtd\">\n"); print ("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"ja\" xml:lang=\"ja\">\n"); print ("<head>\n"); print ("<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=Shift_JIS\" />"); } elseif・・・・

    • ベストアンサー
    • PHP

専門家に質問してみよう