phpでエージェントの振り分けがうまくいかない

このQ&Aのポイント
  • phpでエージェントの振り分けが上手く機能しない原因とは?
  • エージェントの振り分けがうまくいかない問題を解決する方法は?
  • 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
  • 回答数1
  • ありがとう数3

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

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

いろいろ誤差もあるとおもうのでPEARなどで処理した方がいいですよ

参考URL:
http://pear.php.net/package/Net_UserAgent_Mobile/
kurobon619
質問者

お礼

なるほどPEARですか、その方がよさそうですね。 yambejpさんありがとうございます。

関連するQ&A

  • 携帯、スマートフォンの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
  • 携帯と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・.htaccess】PCと携帯の振り分けについて

    【PHP・.htaccess】PCと携帯の振り分けについて お世話になります。 PHP・.htaccess共に初心者なのですが、 PCサイトと携帯サイトの振り分けで悩んでいるので宜しくお願い致します。 現在の構成が http://sample.com/ ←トップページ(PHPファイルのみ。各トップへ振り分け) http://sample.com/pc/ ←PCトップページ http://sample.com/mo/ ←モバイルトップページ(.htaccessでIP制限。PCからのアクセスはトップへ) ---------------------------------------------------- 振り分けPHP(http://sample.com/) <?php $agent = $_SERVER['HTTP_USER_AGENT']; if(preg_match("/^DoCoMo/i", $agent)){ header("Location: http://sample/mo/"); exit; }else if(preg_match("/^(J-PHONE|Vodafone|MOT-[CV]|SoftBank)/i", $agent)){ header("Location: http://sample/mo/"); exit; }else if(preg_match("/^KDDI-/i", $agent) || preg_match("/UP.Browser/i", $agent)){ header("Location: http://sample/mo/"); exit; }else{ header("Location: http://sample/pc/"); exit; } ---------------------------------------------------- となって居ますが、都合により http://sample.com/ ←PCトップ モバイルからのアクセスはmoに振り分け。 http://sample.com/mo/ ←モバイルトップページ。(.htaccessでIP制限。PCからのアクセスはトップへ) 上記の形に切り替えたいと思っています、 PHPを使って振り分けをしたいと思っているのですが、 上記の形を再現する事は出来ますでしょうか? また、.htaccessを利用する事により解決が出来るのであれば、 そちらもご教授いただけると幸いです。

    • 締切済み
    • 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
  • サイトが移動しました(301)が出てしまいます。

    以下のようにキャリア判別しましたが、ドコモ(f706i) でみるとサイトが移動しました(301)が出ます。 これはなんとかならないんでしょうか? <?php $agent = $_SERVER['HTTP_USER_AGENT']; if(preg_match("/^DoCoMo/i", $agent)){ header("Location: ​http://sample.com/i/index.html");​ exit; }else if(preg_match("/^(J\-PHONE|Vodafone|MOT\-[CV]|SoftBank)/i", $agent)){ header("Location: ​http://sample.com/s/index.html");​ exit; }else if(preg_match("/^KDDI\-/i", $agent) || preg_match("/UP\.Browser/i", $agent)){ header("Location: ​http://sample.com/e/index.html");​ exit; }else{ header("Location: ​http://sample.com/pc/index.html");​ exit; } ?>

    • ベストアンサー
    • PHP
  • 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
  • 携帯ユーザーのPHPによる振り分け

    WindowsXP dreamweaverCS3を使用 PHPの知識はまったくありません。 PCサイト「www.deri6mon.com」(PC用サイト)がすでにできており、 これをQRコードに変換してあります。 そのQRコードをユーザーがスマホと携帯のバーコードリーダーで変換し、 PCサイトにアクセスさせますが、携帯ユーザーは当然容量がオーバーしますので エラーでとまってしまいます。 そこで、別に携帯サイトを用意してPCサイトにアクセスしてきた携帯ユーザーを 携帯サイトに自動で振り分けたいと考えています。 PCサイトの最上部に下記を記述しました。 PHPを書き込んだファイルはhtml→phpに拡張子を変換してあります。 -----------------ここから------------------- <?php $page_m='http://www.deri6mon.com/mb/index.html'; $agent=$_SERVER['HTTP_USER_AGENT']; if (preg_match("/UP\.Browser\/[3-5]/",$agent) || preg_match("/J-PHONE\//",$agent) || preg_match("/DoCoMo\//",$agent) && !(preg_match("/(Win|Mac|Linux|FreeBSD|OS¥/2|Solaris)/i",$agent)) || preg_match("/UP.Browser/",$agent) || preg_match("/^Vodafone/",$agent) || preg_match("/^SoftBank/",$agent) || preg_match("/^MOT-/",$agent) || preg_match("/DDIPOCKET;/",$agent) || preg_match("/WILLCOM;/",$agent) || preg_match("/L-mode\/\//",$agent)) { header("Location: $page_m"); }else{ }//end if ?> -----------------ここまで---------------- しかしどうしても携帯でPCサイトが表示されてしまいます。 どこの設定(記述)をどうしたら良いのでしょうか。 因みにhttp://emu.mobile.goo.ne.jp/emu/emu.phpでPCサイトのアドレスを入れると携帯サイトのTESTが表示されるので記述は問題ないと思います。 教えてgooの過去Qを調べてみて気がついた記述が、私が使っているサーバーは 自動で挿入される広告があり「header Locationはエラーになる」というのが ありました。 これを回避するにはどうすればいいのでしょうか? PHPの知識が無いのでわかりやすく回答していただけるとたすかります。 よろしくお願いいたします。

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

    phpのユーザーエージェントでの振り分け方法 質問お願いします。 phpのユーザーエージェントでの振り分け方法を調べまして $ua = $_SERVER['HTTP_USER_AGENT']; if ((strpos($ua, 'Android') !== false) && (strpos($ua, 'Mobile') !== false) || (strpos($ua, 'iPhone') !== false) || (strpos($ua, 'Windows Phone') !== false)) { // スマートフォンからアクセスされた場合 header("Location: smartphone/index.html"); exit(); } elseif ((strpos($ua, 'Android') !== false) || (strpos($ua, 'iPad') !== false)) { // タブレットからアクセスされた場合 header("Location: tablet/index.html"); exit(); } elseif ((strpos($ua, 'DoCoMo') !== false) || (strpos($ua, 'KDDI') !== false) || (strpos($ua, 'SoftBank') !== false) || (strpos($ua, 'Vodafone') !== false) || (strpos($ua, 'J-PHONE') !== false)) { // 携帯からアクセスされた場合 header("Location: mobile/index.html"); exit(); } else { // その他(PC)からアクセスされた場合 header("Location: pc/index.html"); exit(); } とコピペで記入してスマホなどを振り分けています。 safari6とsafari7で見た時だけ header("Location: tablet/index.html"); に振り分けたいのですが、どうしたらよいでしょうか? 素人ですいません。 宜しくお願い致します。

    • ベストアンサー
    • PHP
  • Smartyを使用した、テンプレート内でのキャリア振り分け

    宜しくお願い致します。 現在モバイルサイトを構築しており、PHP+Smartyを利用しているのですが、テンプレート内で"if文"を利用した(ユーザーエージェントによる)キャリア振り分けをしたいと考えています。 ロジックとしては、PHPファイル内でユーザーエージェントを取得し、変数に適当な値を入れる。 その変数をassignし、テンプレート内ではき出す際に条件分岐。 と考えているのですが、うまくいきません。 ご教授いただけましたら幸いです。 なお、ソースは下記のようになります。 【carrier.php】※ディレクトリーの指定など一部省略 $agent = $_SERVER['HTTP_USER_AGENT']; if(preg_match("/^DoCoMo/i", $agent)){ $ca == "i"; }else if(preg_match("/^KDDI\-/i", $agent) || preg_match("/UP\.Browser/i", $agent)){ $ca == "ez"; }else if(preg_match("/^(J\-PHONE|Vodafone|MOT\-[CV]|SoftBank)/i", $agent)){ $ca == "sb"; }else { $ca == "pc"; } $tpl->assign('ca','$ca'); $tpl->display('carrier.html'); 【carrier.html】 {if $ca == "ez"}ezです{/if}

    • ベストアンサー
    • PHP
  • PCと携帯サイトの振分け後の表示アドレスについて

    こんにちは。 私はPHPを利用して、同じアドレス(http://****.com)から携帯とPCを判別して区別しています。 しかし、PCからアクセスした場合は、表示が(http://****.com)となるのですが、携帯からアクセスすると(http://****.com/mobile)のようになってしまいます。 携帯からアクセスした場合でも(http://****.com)と表示させたいのですが、どのようにすればよいのでしょうか。 以前、質問した時は、一つのPHPファイルにPCと携帯二つ分の内容を書いて、どちらかを吐き出す処理を行えばよいとアドバイスをいただきましたが、私の携帯サイトはオリジナルで作成したものではなく、起業からレンタルしているものなのです。 (http://****.com/mobile)というアドレスが与えられるだけのものです。 ですから、自分のオリジナルPCサイトの内容に取り込む事が出来ません。 以下は私の振分けのやり方です。 http://blogn.hustle-user.com/e66.html こちらのサイトを参考にPHPで作成しています。 index.php(http://****.com/でまずここにアクセスします。) <?php $title="http://****.com/mobile"; $page_e='http://****.com/mobile'; $page_j='http://****.com/mobile'; $page_i='http://****.com/mobile'; (↑これら携帯のアドレスにリダイレクトさせてアドレス表示を(http://****.com)にしたいのです。) $i=$_SERVER['HTTP_USER_AGENT'];   $pc='http://****.com/index.html'; (↑PCはこれで上手く(http://****.com)と表示されています。) require_once('data/furiwake.txt'); ?> furiwake.txt(上記のリンク先です) <?php //HDML記述のezweb端末? if (preg_match("/UP\.Browser\/[3-5]/",$i)) { header('Content-Type: text/x-hdml;charset=shift_jis'); echo('<hdml version="3.0" markable="TRUE" public="TRUE">'); echo('<nodisplay markable="TRUE" public="TRUE" title="'.$title.'">'); echo('<action type="ACCEPT" task="GO" dest="'.$page_e.'">'); echo('</nodisplay>'); echo('</hdml>'); exit(); } //SoftBank以前の端末? if (preg_match("/J-PHONE\//",$i)) { header("Location: $page_j"); exit(); } //iモード/EZウェブ次世代機/エアーH"フォンなどの携帯端末一般。 if (preg_match("/DoCoMo\//",$i) || preg_match("/Opera[s\/]/",$i) && !(preg_match("/(Win|Mac|Linux|FreeBSD|OS/2|Solaris)/i",$i)) || preg_match("/UP.Browser/",$i) || preg_match("/^Vodafone/",$i) || preg_match("/^SoftBank/",$i) || preg_match("/^MOT-/",$i) || preg_match("/DDIPOCKET;/",$i) || preg_match("/WILLCOM;/",$i) || preg_match("/L-mode\/\//",$i)) { header("Location: $page_i"); exit(); } else { require_once($pc); exit(); } ?> どなたかPHPに詳しい方、恐れ入りますが回答お願いします。

    • 締切済み
    • PHP

専門家に質問してみよう