开发者

How do i redirect different site visitors based on ip address?

I would like to redirect every user that visited my site to different url each. I'm limiting it to ten sites only though. For example;

When visitor 1 visit my website; www.youarelovely.com it would open.

When visitor 2 visit my website; www.youarelovely.com the visitor would be redirected to www.youarelovely.com/1

When visitor 3 visit my website; www.youarelovely.com the visitor would be redirected to www.youarelovely.com/2 etc... to 10.

I feel by detecting user IP address, storing it in a text file, then getting the next visitor ip address and search it in the list of visitor stored ip address, if it's there then the user would be redirected to that url, if not then the user would be directed to the next available url. How can i achieve it via pure html and javascript?

NOTE: This is for a proof of concept project that would be deployed on github pages or codepen. So basically we're not in the business of complic开发者_运维问答ating things, and it's not location based redirecting because all the visitors would be in the same location, hence why the redirecting solution need to be base on IP address only.

var defaultCountry = ‘US’;
var defaultSite = ‘https://google.com';
var otherSite = ‘https://google.co.uk';
fetch(‘https://ipapi.co/country/')
 .then(response => {
   if (response.ok) {
     return response.text();
   } else {
     throw new Error(‘HTTP Error ‘ + response.status);
   }
 })
 .then(country => {
   if (country == defaultCountry) {
     // No action needed - already on the default site
     console.log(‘Already on default site’);
   } else {
     // Redirect rest of the world
     console.log(‘Redirecting to country specific website’);
     window.location = otherSite;
   }
 })
 .catch(function(error) {
   // Network error
   // Script blocked by browser extension
   // 429 error (too many requests)
   console.log(error);
 });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜