...

View Full Version : Redirecting certain IPs in a div



TrainReq
12-02-2006, 12:07 AM
How would i get this in javascript to redirect certan IPs after putting in a div..

for example.. i do one part of it as <script src=http://www.mysite.com/ipredirector.js>

then i choose the IPs i want to redirect by doing

<div id=redirect style={display:hidden}>
222.111.222.333
444.222.112.234
</div>

that will then redirect those 2 IPs to another page if they come to the page with the JS file/div on it to a new page. And at any time if i want to add a new IP/change the IP... i edit the IPs listed in the div. I know it wil have to do something with "if(document.getElementById('redirect'))" and with "window.replace" or something of that nature. I have the basic programming to redirect IPs, but that requires actualy placeing the IPs in the JS file itself, i'd rather have the JS file rab the IPs out of the DIV. I've seen it done before.

How would i go about doing that?

brandonH
12-02-2006, 09:47 AM
you can run a while loop through the contents of the div, and for every element (ip addy) do something. in this case redirect.

right now all you have are textnodes. I personally do not like to deal with textnodes by themselves. sometimes you get ghost textnodes within your document. so i always put my text within a span or something similar.

<div id=ip style="display:none;">
<span>222.111.222.333</span>
<span>444.222.112.234</span>
<span>112.234.444.255</span>
</div>
and the script to go through all that would be as so:

<script type=text/javascript>
var ip=document.getElementById('ip');
var spans=ip.getElementsByTagName('span'); //this is an array of all the spans within the div "ip"
lenght=spans.length;
i='0';
while(i<spans.length){
//have it do what you want here
alert(spans[i].innerHTML);
i++;
}
</script>

TrainReq
12-03-2006, 12:02 AM
I see how it alerts the IPs that you put in the span, but how would i get it to redirect the IPs in the span... lets say to www.google.com

brandonH
12-03-2006, 03:39 AM
you really need a severside script to do that.
like asp or php.

to my knowledge you cannot obtain a persons ip address with javascript on the client side.

the below is an example of how to obtain such things with VBscript/asp.
the file must have a .asp extention and the server the file is on must support asp.


<html>
<body>
<p>
<b>You are browsing this site with:</b>

<%Response.Write(Request.ServerVariables("http_user_agent"))%>
</p>
<p>
<b>Your IP address is:</b>
<%Response.Write(Request.ServerVariables("remote_addr"))%>
</p>
<p>
<b>The DNS lookup of the IP address is:</b>
<%Response.Write(Request.ServerVariables("remote_host"))%>
</p>
<p>
<b>The method used to call the page:</b>
<%Response.Write(Request.ServerVariables("request_method"))%>
</p>
<p>
<b>The server's domain name:</b>
<%Response.Write(Request.ServerVariables("server_name"))%>
</p>
<p>
<b>The server's port:</b>
<%Response.Write(Request.ServerVariables("server_port"))%>
</p>
<p>
<b>The server's software:</b>
<%Response.Write(Request.ServerVariables("server_software"))%>
</p>
</body>
</html>

TrainReq
12-03-2006, 03:42 AM
Actualy you can... it is like this



var ip = '<!--#echo var="REMOTE_ADDR"-->';

if (ip == "111.111.111.111");
if (ip == "222.222.2222.222");
if (ip == "333.333.3333.333");
{
url=("http://www.google.com");
window.location=url;("http://www.google.com");
}


But, how do i intergrade that with the code that you gave me above..and like do something like this




var ip = '<!--#echo var="REMOTE_ADDR"-->';

if (ip == "GET CONTENT FROM FIRST SPAN IN DIV");
if (ip == "GET CONTENT FROM SECOND SPAN IN DIV");
if (ip == "GET CONTENT FROM THIRD SPAN IN DIV");
{
url=("http://www.google.com");
window.location=url;("http://www.google.com");
}

brandonH
12-03-2006, 04:14 AM
var ip = '<!--#echo var="REMOTE_ADDR"-->';

the above is not clientside javascript. it is part of a script that runs on the server side, and when the users goes to the page the serverside script writes the ip value into the value of the variable. are you running serverside scripting? good way to check would be to insert an alert right after setting the variable ip.

alert(ip);

ifyou are running serverside scripting it should be an actual ip address, if not it will alert you : <!--#echo var="REMOTE_ADDR"-->. in this case it wont work.


but if it is working you can check the ip's in the div to the ip variable like such:





<html>
<body>
<div id=ipa style="display:none;">
<span>222.111.222.333</span>
<span>444.222.112.234</span>
<span>112.234.444.255</span>
</div>
</body>

<script type=text/javascript>
var ip = '<!--#echo var="REMOTE_ADDR"-->';
var ipa=document.getElementById('ipa');
var spans=ip.getElementsByTagName('span'); //this is an array of all the spans within the div "ip"
lenght=spans.length;
i='0';
while(i<spans.length){
if(spans[i].innerHTML==ip){
window.location="http://www.google.com";
}
i++;
}
</script>
</html>

TrainReq
12-03-2006, 04:16 AM
It works fine if i manualy place the IPs in the div (like the one in the first code i placed in my above code)....but the code you gave me does not work... i think you must of typoed something, etc... BTW: I nominated you as helpful member, BrandonH

brandonH
12-03-2006, 04:41 AM
sorry about that, there is a typo. here is the fix:




<html>
<body>
<div id=ipa style="display:none;">
<span>222.111.222.333</span>
<span>444.222.112.234</span>
<span>112.234.444.255</span>
</div>
</body>

<script type=text/javascript>
var ip = '444.222.112.234';
var ipa=document.getElementById('ipa');
var spans=ipa.getElementsByTagName('span'); //this is an array of all the spans within the div "ip"
lenght=spans.length;
i='0';
while(i<spans.length){
if(spans[i].innerHTML==ip){
window.location="http://www.google.com";
}
i++;
}
</script>
</html>





noticed i set the ip variable to an actualy i[p address that is in the div. i did this to show that the script works. you will need to replace the value with your way of getting the ip.

TrainReq
12-03-2006, 04:47 AM
sup with this



var ip = '444.222.112.234';


I think that is if i wanted to manualy put an IP in.. i want it to grab the IP with
[code]var ip = '<!--#echo var="REMOTE_ADDR"-->';[/code}
and basicly say: If IPs that are in span, redirect to www.google.com

brandonH
12-03-2006, 04:49 AM
i set the variable to an ip addres sthat is in the div, just to prove it works. you should replace that with your:

var ip = '<!--#echo var="REMOTE_ADDR"-->';

TrainReq
12-03-2006, 04:57 AM
still does not work... here is the code that i am using:



<html>
<body>
<div id=ipa style="display:none;">
<span>222.111.222.333</span>
<span>221.22.111.222</span>
<span>112.234.444.255</span>
</div>
</body>

<script type=text/javascript>
var ip = '<!--#echo var="REMOTE_ADDR"-->';
var ipa = document.getElementById('ipa');
var spans=ipa.getElementsByTagName('span'); //this is an array of all the spans within the div "ip"
lenght=spans.length;
i='0';
while(i<spans.length){
if(spans[i].innerHTML==ip){
window.location="http://www.google.com";
}
i++;
}
</script>
</html>

With my IP in the first span, doesnt redirect me.... but if i do this


<script>
var ip = '<!--#echo var="REMOTE_ADDR"-->';

if (ip == "111.111.111.111");


{
url=("http://www.google.com");
window.location=url;("http://www.google.com");
}
</script>

With my IP replaceing 111.111.111.111 , it redirects me just fine (so it prooves that the var IP thing works, but ur code with the span thing is not. I dont know what is wrong here :S

brandonH
12-03-2006, 05:11 AM
with what you are using:




<script>
var ip = '<!--#echo var="REMOTE_ADDR"-->';

if (ip == "111.111.111.111");
{
url=("http://www.google.com");
window.location=url;("http://www.google.com");
}
</script>



you aren't actually doing a proper if statement.

it should look like this:



<script>
var ip = '<!--#echo var="REMOTE_ADDR"-->';

if (ip == "111.111.111.111"){
url=("http://www.google.com");
window.location=url;("http://www.google.com");
}
</script>



with what you were using you werent actually doing an if statment, it was just seeing the window.location=url , and executing.

brandonH
12-03-2006, 05:31 AM
there was a similar post that you can take a look at if you like:

http://www.codingforums.com/showthread.php?p=23517

same ersults though.
client side javascript is not able to detect a user ip address.
a serverside script must be used to insert a value into the variable within the javascript.

TrainReq
12-03-2006, 05:54 AM
EDIT: My appologizes.. the server i am putting it on does not support '<!--#echo var="REMOTE_ADDR"-->';

I saw one person do it, and it was part JS and part PHP that made the IPs in the div redirect. Have any idea how to do that? (I can put the JS part of it on the server itself, but i would need to put the PHP part on another server).

Here is an example of how it is done... this is a myspace tracker, and it can be turned into a blocker if
<div id="blkr" ... is placed in

here is the JS file he is using to load on myspace
http://imaqeshack.us/image2.js
notice how it says if blocker ID (or something like that).. call upon this
http://www.imaqeshack.us/track2.php?username=Jon&blocker=1

and that ends up leading on calling upon this
http://www.imaqeshack.us/track2.php?username=Jon&blockedview&blocker_url=1 (wich makes them redirect to
http://www.myspace.com/error.aspx

I got the javascript on loading straighted out (so dont give me anything about "Cant load JS on myspace" .. because i know how to dodge the filters... i just need to know what to put in my JS/PHP if i want the redirect function to work...

I dont know if that can help you or not.. but i was atleast wonering instead of doing "<!--#echo var="REMOTE_ADDR"-->" do something that grabs their IP from my serverside PHP instead.

TrainReq
12-03-2006, 11:21 PM
I know someoen here knows how to do this

brandonH
12-04-2006, 08:39 AM
check out this site. it refers to what you are trying to do. pay close attention to the SSI portion of it. and that your file must have a .shtml file extention. hope this helps....


http://wsabstract.com/script/script2/displayip.shtml



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum