Hi, I need to block specific ip addresses and redirect to another page on my site. I have code to get the ip address, I use this on other parts of my site and it works. What I’m struggling with is to query the ip and redirect. I’ve created a collection ‘BlockedIP’ and have a field in it ‘ipAddress’. I’ve put the code in the site code so whatever page they land on it should redirect them. I’m getting no errors and the ip is being shown in the console so I guess it’s something to do with my lack of code experience that the query isn’t working! Here’s the code I’m using, any pointers or corrections are much appreciated!
You have a small bug in your code, fix it and our functionality should work.
You need to pass the discovered IP to the last section that checks it against the collection, so your last section should look like so:
Thanks Ohad for the quick correction of my reply post.
Also, is it possible to remove that request option if it isn’t appropriate now, or at least put this option as a workaround if it is still needed to be shown to users.
if (httpResponse.ok) { return httpResponse.json();
}
})
.then((json) => {
let ipaddress = json.query;
wixData.query(“BlockedIP”)//use your collection name (case-sensitive)
.eq(“ipAddress”, ipaddress)//use your collection field key (type: text)
.find()
.then( (res) => {
if (res.items.length > 0){
wixLocation.to(“/blocked”);//redirect to blocked page be sure you have such a path
}
})
})
})
import wixLocation from ‘wix-location’;
import wixData from ‘wix-data’;
import {fetch} from ‘wix-fetch’;
$w.onReady( function () {
fetch(‘https://extreme-ip-lookup.com/json’, {
method: ‘get’
})
.then((httpResponse) => {
if (httpResponse.ok) { return httpResponse.json();
}
})
.then((json) => {
let ipaddress = json.query;
wixData.query(“BlockedIP”)//use your collection name (case-sensitive)
.eq(“223.237.225.21”, ipaddress)//use your collection field key (type: text)
console.log(ipaddress)
.find()
.then( (res) => {
if (res.items.length > 0){
wixLocation.to(‘/blank-page’);//redirect to blocked page be sure you have such a path
}
})
})
})
Please check the code and understand it before using it as the original post was about querying a returned ip address against a dataset of ip addresses and not for just using a specific ip address.
You don’t need to change anything here as long as your dataset is called BlockedIP and you have the field with a key of ipAddress.
wixData.query("BlockedIP")//use your collection name (case-sensitive)
.eq("ipAddress", ipaddress)//use your collection field key (type: text)
See the Wix Data Query API and the eq. function for more info on using it.
Also, make sure that you have this page in your website so that it can go to it.
wixLocation.to('/blank-page');
//redirect to blocked page be sure you have such a path
Finally, this is an old post from last year and therefore will be CLOSED.
if you require more help then please post a new forum post.