homepage Welcome to WebmasterWorld Guest from 54.161.155.142
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Please tell me why this doesn't work
RickB




msg:4555376
 4:58 pm on Mar 15, 2013 (gmt 0)

Guys,

I need your help! Please tell me why my script won't redirect but the script I have below the line does. I think it has something to do with the variables but I would appreciate any help you guys can offer!

My script that doesn't redirect!

<script language="JavaScript" src="http://beaman-auto.com/ip2.php"></script>

<script type="text/javascript">
// IP Redirect
if (Ip=="63.243.66.174"){location.replace("http://www.example.com/security.html")
}
</script>


-----------------------------------------------------------

The Script that does redirect

<script language="JavaScript" src="http://scripts.hashemian.com/js/visitorIPHOST.js.php"></script>

<script type="text/javascript">
// IP Redirect
if (VIH_HostIP=="63.243.66.174"){location.replace("http://www.example.com/security.html")
}
</script>


------------------------------------------------------------

Here's my php code from the beaman-auto.com/ip2.php link.

<?php

function getRealIpAddr() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
//check ip from share internet
$ip=$_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
//to check ip is pass from proxy
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
?>

<?php
$ip = getRealIpAddr();
$VIH_HostIP = $ip;

$VIH_DisplayFormat = 'You are visiting from:<br>IP Address: ' . $ip . '<br>Host: ' . $VIH_HostIP . '"';
echo 'VIH_DisplayFormat = "' . $VIH_DisplayFormat . '"';
?>
<script type="text/javascript">
var Ip = "<?php echo $ip; ?>";
</script>

 

Fotiman




msg:4555409
 6:33 pm on Mar 15, 2013 (gmt 0)

ip2.php is included in your markup as though it was a script file, but you are including HTML markup in it instead.

The output of your non working version looks like this:

VIH_DisplayFormat = "You are visiting from:<br>IP Address: 0.0.0.0<br>Host: 0.0.0.0""<script language="javascript">
Ip = "0.0.0.0";
</script>


Instead, you'll need to get rid of that echo statement, as well as the opening and closing script tags so that when you include it, the output is valid JavaScript:


<?php

function getRealIpAddr() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
//check ip from share internet
$ip=$_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
//to check ip is pass from proxy
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
?>

<?php
$ip = getRealIpAddr();
$VIH_HostIP = $ip;

$VIH_DisplayFormat = 'You are visiting from:<br>IP Address: ' . $ip . '<br>Host: ' . $VIH_HostIP . '"';
//echo 'VIH_DisplayFormat = "' . $VIH_DisplayFormat . '"';
?>
var Ip = "<?php echo $ip; ?>";

RickB




msg:4555442
 8:02 pm on Mar 15, 2013 (gmt 0)

That works perfect Fotiman. Thank you so much for your help and guidance.

Is it difficult to put an array in the PHP file that includes the ip addresses I want to redirect and have the Javascript to check those and basiclly if there is a match to redirect that visitor?

I'll tell you a bit about my situation. When I run specials on my website, my competitors in my area shop the site and basically copy my specials and take content. The problem is my website provider will not allow me to add/edit any javascript on the site. I can however control the PHP file which is hosted on my server. What I would love to be able to do is submit the javascript to my website provider and as I get each competitors IP address, add it into the array in the PHP file that I can control.

Is this possible?

Fotiman




msg:4555449
 8:33 pm on Mar 15, 2013 (gmt 0)

It's not difficult to check an array of items for a particular value. The question, though, is whether or not it's worth trying to do. It's easily bypassed. All the visitor would need to do is disable JavaScript in their browser. Alternatively, there are sites online that "anonymize" your request (basically they view your site through a proxy so from your server's perspective it will not see the true IP address of the visitor). There's not much you can really do to truly prevent data that appears on the web from being copied.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved