Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Przekierowanie dla mobilnych według kraju
Forum PHP.pl > Forum > PHP
h4d3s
Witam, mam problem biggrin.gif aktualnie mam na stronie zrobione przekierowanie według kraju, czyli np klika sobie klient na link z landing pagem i przekierowuje go do danej strony, przekierowanie jest napisane w phpie i składa się z dwóch plików index.php
  1. <?php
  2. // ccr.php - country code redirect
  3. require_once('geoplugin.class.php');
  4. $geoplugin = new geoPlugin();
  5. $geoplugin->locate();
  6. $country_code = $geoplugin->countryCode;
  7. switch($country_code) {
  8. case 'US':
  9. exit;
  10. case 'CA':
  11. case 'GB':
  12. exit;
  13. case 'NL':
  14. case 'AU':
  15. exit;
  16. case 'NZ':
  17. case 'PL':
  18. exit;
  19. case 'BE':
  20. case 'FI':
  21. exit;
  22. case 'DE':
  23. case 'DK':
  24. exit;
  25. case 'IE':
  26. case 'NO':
  27. exit;
  28. case 'FR':
  29. default: // exceptions
  30. exit;
  31. }
  32. ?>

oraz geoplugin.class.php
  1. <?php
  2. /*
  3. This PHP class is free software: you can redistribute it and/or modify
  4. the code under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7.  
  8. However, the license header, copyright and author credits
  9. must not be modified in any form and always be displayed.
  10.  
  11. This class is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15.  
  16. @author geoPlugin (gp_support@geoplugin.com)
  17. @copyright Copyright geoPlugin (gp_support@geoplugin.com)
  18. $version 1.01
  19.  
  20.  
  21. This PHP class uses the PHP Webservice of http://www.geoplugin.com/ to geolocate IP addresses
  22.  
  23. Geographical location of the IP address (visitor) and locate currency (symbol, code and exchange rate) are returned.
  24.  
  25. See http://www.geoplugin.com/webservices/php for more specific details of this free service
  26.  
  27. */
  28.  
  29. class geoPlugin {
  30.  
  31. //the geoPlugin server
  32. var $host = 'http://www.geoplugin.net/php.gp?ip={IP}&base_currency={CURRENCY}';
  33.  
  34. //the default base currency
  35. var $currency = 'USD';
  36.  
  37. //initiate the geoPlugin vars
  38. var $ip = null;
  39. var $city = null;
  40. var $region = null;
  41. var $areaCode = null;
  42. var $dmaCode = null;
  43. var $countryCode = null;
  44. var $countryName = null;
  45. var $continentCode = null;
  46. var $latitude = null;
  47. var $longitude = null;
  48. var $currencyCode = null;
  49. var $currencySymbol = null;
  50. var $currencyConverter = null;
  51.  
  52. function geoPlugin() {
  53.  
  54. }
  55.  
  56. function locate($ip = null) {
  57.  
  58. global $_SERVER;
  59.  
  60. if ( is_null( $ip ) ) {
  61. $ip = $_SERVER['REMOTE_ADDR'];
  62. }
  63.  
  64. $host = str_replace( '{IP}', $ip, $this->host );
  65. $host = str_replace( '{CURRENCY}', $this->currency, $host );
  66.  
  67. $data = array();
  68.  
  69. $response = $this->fetch($host);
  70.  
  71. $data = unserialize($response);
  72.  
  73. //set the geoPlugin vars
  74. $this->ip = $ip;
  75. $this->city = $data['geoplugin_city'];
  76. $this->region = $data['geoplugin_region'];
  77. $this->areaCode = $data['geoplugin_areaCode'];
  78. $this->dmaCode = $data['geoplugin_dmaCode'];
  79. $this->countryCode = $data['geoplugin_countryCode'];
  80. $this->countryName = $data['geoplugin_countryName'];
  81. $this->continentCode = $data['geoplugin_continentCode'];
  82. $this->latitude = $data['geoplugin_latitude'];
  83. $this->longitude = $data['geoplugin_longitude'];
  84. $this->currencyCode = $data['geoplugin_currencyCode'];
  85. $this->currencySymbol = $data['geoplugin_currencySymbol'];
  86. $this->currencyConverter = $data['geoplugin_currencyConverter'];
  87.  
  88. }
  89.  
  90. function fetch($host) {
  91.  
  92. if ( function_exists('curl_init') ) {
  93.  
  94. //use cURL to fetch data
  95. $ch = curl_init();
  96. curl_setopt($ch, CURLOPT_URL, $host);
  97. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  98. curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.0');
  99. $response = curl_exec($ch);
  100. curl_close ($ch);
  101.  
  102. } else if ( ini_get('allow_url_fopen') ) {
  103.  
  104. //fall back to fopen()
  105. $response = file_get_contents($host, 'r');
  106.  
  107. } else {
  108.  
  109. trigger_error ('geoPlugin class Error: Cannot retrieve data. Either compile PHP with cURL support or enable allow_url_fopen in php.ini ', E_USER_ERROR);
  110. return;
  111.  
  112. }
  113.  
  114. return $response;
  115. }
  116.  
  117. function convert($amount, $float=2, $symbol=true) {
  118.  
  119. //easily convert amounts to geolocated currency.
  120. if ( !is_numeric($this->currencyConverter) || $this->currencyConverter == 0 ) {
  121. trigger_error('geoPlugin class Notice: currencyConverter has no value.', E_USER_NOTICE);
  122. return $amount;
  123. }
  124. if ( !is_numeric($amount) ) {
  125. trigger_error ('geoPlugin class Warning: The amount passed to geoPlugin::convert is not numeric.', E_USER_WARNING);
  126. return $amount;
  127. }
  128. if ( $symbol === true ) {
  129. return $this->currencySymbol . round( ($amount * $this->currencyConverter), $float );
  130. } else {
  131. return round( ($amount * $this->currencyConverter), $float );
  132. }
  133. }
  134.  
  135. function nearby($radius=10, $limit=null) {
  136.  
  137. if ( !is_numeric($this->latitude) || !is_numeric($this->longitude) ) {
  138. trigger_error ('geoPlugin class Warning: Incorrect latitude or longitude values.', E_USER_NOTICE);
  139. return array( array() );
  140. }
  141.  
  142. $host = "http://www.geoplugin.net/extras/nearby.gp?lat=" . $this->latitude . "&long=" . $this->longitude . "&radius={$radius}";
  143.  
  144. if ( is_numeric($limit) )
  145. $host .= "&limit={$limit}";
  146.  
  147. return unserialize( $this->fetch($host) );
  148.  
  149. }
  150.  
  151.  
  152. }
  153.  
  154. ?>

Ogólnie nie wiem jak zrobić do tego kod żeby dodatkowo wykrywał czy użytkownik jest z telefony/tabletu czy innego urządzenia... zakładając że użytkownik jest z uk i wchodzi z tabletu to przekierowuje go do odpowiedniego landing page strony .
Z góry dzięki za pomoc, nie mam pomysłu jak to zrobić, szukałem w necie przekierowania na mobilne strony ale nie wiem jak to wkomponować żeby przekierowanie było definiowane krajem.



EDIT //
Zapłacę 5 $ / paypal
SmokAnalog
Do wykrywania czy użytkownik korzysta z mobilnego urządzenia też użyj biblioteki. Znalazłem np. taką: http://mobiledetect.net/

Na jakim etapie zaawansowania jest Twoja strona? Bo jeśli na wczesnym, to zamiast tych przekierowań, lepiej zrobić obsługę języka i urządzeń mobilnych w jednym miejscu. Frameworki oferują wygodne rozwiązania językowe, a jak nie to możesz sam łatwo takowy napisać. Design dla urządzeń mobilnych lepiej jest obsłużyć w CSS, przekierowania to dzisiaj dość rzadko spotykane i niewygodne rozwiązanie.
h4d3s
Ogólnie u mnie zmiana samego języka nic nie rozwiązuje, bo potrzebuje np wyświetlić inna stronę dla UK a całkiem inna dla NL / DK czy też innych smile.gif
A mógłbyś podjąć się takiego czegoś chodzi mi o te mobilne przekierowania, za 5 $ ?smile.gif niby nic, ale dla kogoś doświadczonego to pewnie 10 minut pracy, tym bardziej że skrypt na przekierowanie jest i działa super.

Pozdrawiam.
SmokAnalog
A nie chcesz się nauczyć czegoś nowego?
h4d3s
Ok, ogólnie wymyśliłem coś takiego
  1. <?php
  2. // ccr.php - country code redirect
  3. require_once('geoplugin.class.php');
  4. $geoplugin = new geoPlugin();
  5. $geoplugin->locate();
  6. $country_code = $geoplugin->countryCode;
  7. // Include and instantiate the class.
  8. require_once 'mobile/Mobile_Detect.php';
  9. $detect = new Mobile_Detect;
  10.  
  11. // Any mobile device (phones or tablets).
  12. if ( $detect->isMobile() ) {
  13. switch($country_code) {
  14. case 'GB':
  15. header('Location: <a href="http://strona.com/include/m_uk.php&#39%3b%29;" target="_blank">http://strona.com/include/m_uk.php');</a>
  16. }
  17. }
  18. else
  19. {
  20. switch($country_code) {
  21. case 'US':
  22. header('Location: <a href="http://strona.com/include/us.php&#39%3b%29;" target="_blank">http://strona.com/include/us.php');</a>
  23. case 'CA':
  24. header('Location: <a href="http://strona.com/include/ca.php&#39%3b%29;" target="_blank">http://strona.com/include/ca.php');</a>
  25. case 'GB':
  26. header('Location: <a href="http://strona.com/include/gb.php&#39%3b%29;" target="_blank">http://strona.com/include/gb.php');</a>
  27. case 'NL':
  28. header('Location: <a href="http://strona.com/include/nl.php&#39%3b%29;" target="_blank">http://strona.com/include/nl.php');</a>
  29. case 'SE':
  30. header('Location: <a href="http://strona.com/include/se.php&#39%3b%29;" target="_blank">http://strona.com/include/se.php');</a>
  31. case 'AU':
  32. header('Location: <a href="http://strona.com/include/au.php&#39%3b%29;" target="_blank">http://strona.com/include/au.php');</a>
  33. case 'NZ':
  34. header('Location: <a href="http://strona.com/include/nz.php&#39%3b%29;" target="_blank">http://strona.com/include/nz.php');</a>
  35. case 'PL':
  36. header('Location: <a href="http://strona.com/include/pl.php&#39%3b%29;" target="_blank">http://strona.com/include/pl.php');</a>
  37. case 'BE':
  38. header('Location: <a href="http://strona.com/include/be.php&#39%3b%29;" target="_blank">http://strona.com/include/be.php');</a>
  39. case 'FI':
  40. header('Location: <a href="http://strona.com/include/fi.php&#39%3b%29;" target="_blank">http://strona.com/include/fi.php');</a>
  41. case 'DE':
  42. header('Location: <a href="http://strona.com/include/de.php&#39%3b%29;" target="_blank">http://strona.com/include/de.php');</a>
  43. case 'DK':
  44. header('Location: <a href="http://strona.com/include/dk.php&#39%3b%29;" target="_blank">http://strona.com/include/dk.php');</a>
  45. case 'IE':
  46. header('Location: <a href="http://strona.com/include/ie.php&#39%3b%29;" target="_blank">http://strona.com/include/ie.php');</a>
  47. case 'NO':
  48. header('Location: <a href="http://strona.com/include/no.php&#39%3b%29;" target="_blank">http://strona.com/include/no.php');</a>
  49. case 'FR':
  50. header('Location: <a href="http://strona.com/include/fr.php&#39%3b%29;" target="_blank">http://strona.com/include/fr.php');</a>
  51. default: // exceptions
  52. header('Location: <a href="http://strona.com/allelse.php&#39%3b%29;" target="_blank">http://strona.com/allelse.php');</a>
  53. }
  54. }
  55.  
  56. ?>

Tylko pytanie czy zadziała tak jak ma, czyli pierw wykrywa czy mobilny i potem przekierowanie ...
Pyton_000
Zamień tym:
Kod
<?php

// ccr.php - country code redirect
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
$country_code = $geoplugin->countryCode;

// Include and instantiate the class.
require_once 'mobile/Mobile_Detect.php';
$detect = new Mobile_Detect;

// Any mobile device (phones or tablets).
if ($detect->isMobile() && $country_code == 'GB')
{
    header('Location: http://strona.com/include/m_uk.php');
    exit;
}
else
{

    $countries = array(
        'US', 'CA', 'GB', 'NL', 'SE', 'AU', 'NZ', 'PL', 'BE', 'FI', 'DE', 'DK', 'IE', 'NO', 'FR'
    );

    if (in_array($country_code, $countries))
    {
        header('Location: http://strona.com/include/' . strtolower($country_code) . '.php');
        exit;
    }
    
    header('Location: http://strona.com/include/allelse.php');
    exit;
}
h4d3s
A będzie to wtedy działało ? bo ja potrzebuje żeby np osoba z UK była kierowana do danej strony, przeznaczonej właśnie dla UK ...
Pyton_000
Zmieniłem post wyżej, masz ładnie przeformatowany kod. Teraz chyba sam rozszyfrujesz co gdzie i kiedy pójdzie.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.