cancel
Showing results for 
Search instead for 
Did you mean: 

Popup scripts but only to certain visitors

phoenixcomp
Newbie
Posts: 8
Registered: 28-01-2008

Popup scripts but only to certain visitors

I was wondering if anyone knew of a script which first checked the IP of the visitor and if it was a certian IP or within a range specified you could then show the popup? Big ask I know Smiley
For example: a visitor from the PlusNet IP range visited my site, it shows the popup, from anywhere else it does not.
9 REPLIES
Community Veteran
Posts: 1,576
Thanks: 3
Registered: 13-04-2007

Re: Popup scripts but only to certain visitors

You dont say what language you want this in. here are some examples in php and other. Also some examples of how to get the IP all you need to add is the popup.
Most can be found at  http://javascript.internet.com/user-details/

With SSI enabled this should work:

<!--#set var="ip" value="$REMOTE_ADDR" -->
<!--#if expr="$ip = /123.123.123.123/" -->
<script type="text/javascript">
window.alert("Welcome to the forums, Huntress.");
</script>
<!--#endif -->
<?php
if($_SERVER['REMOTE_ADDR'] == '123.123.123.123'){
echo '<script type="text/javascript">
window.alert("Welcome to the forums, Huntress.");
</script>';
}
?>

get ip
<!-- STEP ONE: Paste this code into the HEAD of your HTML document  -->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
// http://www.kdcgrohl.com
// Depending on your server set-up,
// you may need to use the ".shtml"
// extension [instead of the "html"
// or "htm"] as the script uses Server
// Side Includes. To display in the
// title bar, exclude the
//"<title></title>" code from the page.
// This part gets the IP
var ip = '<!--#echo var="REMOTE_ADDR"-->';
// This part is for an alert box
alert("Your IP address is "+ip);
// This part is for the status bar
window.defaultStatus = "Your IP address is "+ip;
// This part is for the title bar
document.write("<title>Your IP address is "+ip+"</title>");
//  End -->
</script>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>
<!-- Script Size:  1.09 KB -->

http://javascript.internet.com/user-details/
Prod_Man
Grafter
Posts: 286
Registered: 04-08-2007

Re: Popup scripts but only to certain visitors

I would say pretty much what samuria has suggested. Smiley
I would use PHP and make it much more dynamic (behind the scenes),
I would use JavaScript purely to produce the popup / display the text.
JavaScript, is fine for actually producing the Popup Message Box / Window / what ever as it's supported widely.
Use PHP to process Headers Sent by the Web Browser,
to provide information such as Language ($_SERVER["HTTP_ACCEPT_LANGUAGE"]),
IP Address ($_SERVER["REMOTE_ADDR"]) and what not.
(To check by IP range is alittle more tricky, but you can quite easily process an IP in 4 numerical parts, check each one against a range.
I can try and provide an example if you wish)
If all parameters are TRUE, provide the popup only when needed Smiley
Jim,
phoenixcomp
Newbie
Posts: 8
Registered: 28-01-2008

Re: Popup scripts but only to certain visitors

Cheers guys, worked a treat, I went for the PHP option but not on plusnets webspace. Is there a way you can use like a wildcard for the last octet? as in 123.123.123.xxx the only way I can get it to work with 2 IP's is to add the script twice.
The reason was there may be more than one or a complete range that I would want to display the pop to.(went for a html page as a popup rather than just a window.
Prod_Man
Grafter
Posts: 286
Registered: 04-08-2007

Re: Popup scripts but only to certain visitors

You'll ideally need to use a loop of checking against an Array.

$IPS = Array();
$IPS[0]="xxx.xxx.xxx.xxx";
...
etc
..
for($i=0;$i<count($IPS);$i++){
//checking code & logic
}

So that it checks IP's from a defined list..
Where the list comes from, could be database driven.. file.. how ever you wish..
Jim,
Community Veteran
Posts: 1,576
Thanks: 3
Registered: 13-04-2007

Re: Popup scripts but only to certain visitors

Depending what you are trying to do could you not just use cookies or is it for first time users?
phoenixcomp
Newbie
Posts: 8
Registered: 28-01-2008

Re: Popup scripts but only to certain visitors

No it's not for first time users, I need to display a web page but only to certain visitors from a specific IP range the code above works ok when I enter my ip and test it, but I don't know how to specift a range, ie:
123.123.0.0 or 123.123.123.0 upto the top of the range, how would I do that from
[tt]<?php
if($_SERVER['REMOTE_ADDR'] == '123.123.123.123'){
echo '<script type="text/javascript">
window.open("popup.html");
</script>';
}
?> [/tt]
Not usre if I need the subnet mask on the end? /255.255.0.0
you may have answered the question with the array but that's a bit past my abilities at the moment.

Prod_Man
Grafter
Posts: 286
Registered: 04-08-2007

Re: Popup scripts but only to certain visitors

Yeah, unfortunately to do it you will have to encounter Arrays.
along the lines of this if you plan to use 'wild cards' ...

<?php
//===========Dev-Center============
//=By J4M32 - jim@dev-center.co.uk=
//===========Dev-Center============
//Value set and checked later ...
$IPconfirm = 0;

//Explode the IP by the . character
$IPExp = explode('.',$_SERVER['REMOTE_ADDR']);

// define your IP 'Ranges' (Only by Wild cards)...
$IPList = Array();
$IPList[0] = '123.123.123.*';
$IPList[1] = '221.221.*.*';

//for every IP we've got in the list...
for($i=0;$i<count($IPList);$i++){

//explode the IP range into component parts..
$ExpIPList = explode('.',$IPList[$i]);

//for each part of the IP ...
for($j=0;$j<count($ExpIPList);$j++){

//compare them ...
//Logic handeling the '*' Wild Cards... are they the 'same'?
if(($ExpIPList[$j] == $IPExp[$j]) || $IPExp[$j] == "*"){
$IPconfirm++;
}

}

//safety against using "dirty" Arrays...
unset($ExpIPList);

}
//end of for loops

//did all 4 parts 'match'?
if($IPconfirm == 4){
echo '
<script type="text/javascript">
window.open("popup.html");
</script>';
}
?>

if you plan to define a more accurate 'value specific range',
that will require more perocessing and internal syntax to the strings,
to make life alittle bit easier for processing it and identifying...

<?php
//===========Dev-Center============
//=By J4M32 - jim@dev-center.co.uk=
//===========Dev-Center============
//Value set and checked later ...
$IPconfirm = 0;
//Used for Ranges later...
$ValueRange = Array();
//abosolute values - directly inbetween the two vlaues or numerically exlusive case?
$InclusiveCompare = 1;

//Explode the IP by the . character
$IPExp = explode('.',$_SERVER['REMOTE_ADDR']);

// define your IP 'Ranges' (Only by Wild cards)...
$IPList = Array();
$IPList[0] = '123.123.123.128-255';
$IPList[1] = '221.221.96;128-255.*';

//for every IP we've got in the list...
for($i=0;$i<count($IPList);$i++){


//explode the IP range into component parts..
$ExpIPList = explode('.',$IPList[$i]);

//for each part of the IP ...
for($j=0;$j<count($ExpIPList);$j++){

//if there are any ';' breaks ...
if(strpos(';',$ExpIPList[$j]) > 0){

//explode them out...
$ExpIPListValues = explode(';',$ExpIPList[$j]);

//check for specific value ranges..
for($k=0;$k<count($ExpIPListValues);$k++){

//are there any defined sub ranges?
if(strpos('-',$ExpIPListValues[$k]) > 0){

//split out the '-' defined ranges..
$ExpIPListSubranges = explode('-',$ExpIPListValues[$k]);

//for each range found
for($l=0;$l<count($ExpIPListSubranges);$l++){

//blah
$ValueRange[$l] = $ExpIPListSubranges[$l];
//which is the upper bound?
if($ValueRange[0] > $ValueRange[1]){
$Upper = $ValueRange[0];
$Lower = $ValueRange[1];
}else{
$Lower = $ValueRange[0];
$Upper = $ValueRange[1];
}

//if it's in-between the range accept it...
if(($IPExp[$j] > $Lower) && ($IPExp[$j] < $Higher)){
$IPconfirm++; //set the field true...
}

//do we want right up to the range defined?
if($InclusiveCompare == 1){

//is it exactly equal to the lower / higher value?
if(($IPExp[$j] == $Lower) || ($IPExp[$j] == $Higher)){
$IPconfirm++; //set the field true...
}

}

//clears up dirty values
unset($Upper);
unset($Lower);

}

}else{ //otherwise.. must be just a fixed value..

//is it equal?
if($ExpIPList[$j] == $ExpIPListValues[$k]){
$IPconfirm++; //set the field true...
}

}

}

}else{ //otherwise..

//compare them - Logic handeling the '*' Wild Cards... are they the 'same'? / are they exactly equal...
if(($ExpIPList[$j] == $IPExp[$j]) || ($IPExp[$j] == "*")){
$IPconfirm++; //set the field true...
}

}

}

unset($ExpIPList);

}
//end of loops

//did all 4 parts 'match'?
if($IPconfirm == 4){
echo '<script type="text/javascript">
window.open("popup.html");
</script>';
}
?> 

Where...
xxx = Integer - single value...
xxx = Integer;Integer;Integer - set of values...
xxx = Integer-Integer; - range of values...
*Thinking out of the box ... Regular Expressions come to mind?
not certain of how to impliment those in this particular case but, could save a few lines*
Quite a lengthy post.
Code bundles in a ZIP.
Here
Hope one of those was what you were after ...
Jim,
phoenixcomp
Newbie
Posts: 8
Registered: 28-01-2008

Re: Popup scripts but only to certain visitors

Thanks again, really useful Smiley
Prod_Man
Grafter
Posts: 286
Registered: 04-08-2007

Re: Popup scripts but only to certain visitors

Ah no problem!
sorry it's abit lengthy,
it's the easiest way I could think of doing it. Smiley
Hope it all works for what you were after Cheesy
Jim,