#016 posted on 2017.06.01[ update: 2017.07.11 ]

ユーザーエージェントでPC、SP振り分け

PHP

<?php

$ua = $_SERVER['HTTP_USER_AGENT'];

if((strpos($ua,'iPhone') !== false) || (strpos($ua,'iPod') !== false) || (strpos($ua,'Android')!==false)) {

  SP処理;

}else{

  PC処理;

}

?>

 

JavaScript

<script type="text/javascript">

if ((navigator.userAgent.indexOf('iPhone') > 0 && navigator.userAgent.indexOf('iPad') == -1) || navigator.userAgent.indexOf('iPod') > 0 || navigator.userAgent.indexOf('Android') > 0) {

  SP処理;

}else{

  PC処理;

}

</script>

 

.htaccess

RewriteEngine On

 

# Set enviroment value by user-agent

SetEnvIf User-Agent "iPhone" UA=sp

SetEnvIf User-Agent "iPod" UA=sp

SetEnvIf User-Agent "Android" UA=sp

 

# Redirect if smart phone

RewriteCond %{REQUEST_URI} !^/sp.*

RewriteCond %{ENV:UA} ^sp$

RewriteRule ^(.*)$ /sp/ [R,L]

 

 

http://html5-css3.jp/smartphone/pc-iphone-android-php-javascript-htaccess.html

SHARE THIS ON...

- 人気記事 -