Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass varibles with javascript maybe php?? 1

Status
Not open for further replies.

techexpressinc

Programmer
Oct 28, 2008
67
US
:chomp: I want to capture the user's ip address and do a different web page based on the ip address.
The code below gets the ip address but i am having difficultly in using it an "if else " statement later.

I am not sure how to proceed??



<script language="JavaScript">


VIH_BackColor = "palegreen";
VIH_ForeColor = "navy";
VIH_FontPix = "16";
VIH_DisplayFormat = "You are visiting from:<br>IP Address: %%IP%%<br>Host: %%HOST%%";
VIH_DisplayOnPage = "yes";

var ipaddr = %%IP%%;
document.write('You are at '+ipaddr+' .');

</script>
<script language="JavaScript" src="
</script>


This is "like" the if statement I want to use, but i want to use the capture var ipaddr. Which I cannot catch. Can someone help??

<script type="text/javascript">
//If the time is less than 10,
//you will get a "Good morning" greeting.
//Otherwise you will get a "Good day" greeting.var d = new Date();

var d = new Date();
var time = d.getHours();

if (time < 12)
{
window.location="http:://;
}
else
{
window.location=";
}
</script>

Thank you for any help.

Russ @ scaninc.org
 
Here is more code, it is always going to the good ip url.
I never get the ip to display it part two of code.


<script language="JavaScript">


VIH_BackColor = "palegreen";
VIH_ForeColor = "navy";
VIH_FontPix = "16";
VIH_DisplayFormat = "You are visiting from:<br>IP Address: %%IP%%<br>Host: %%HOST%%";
VIH_DisplayOnPage = "yes";

var ipaddr = '%%IP%%';
document.write('You are at '+ipaddr+' .');


if (ipaddr = '63.131.12.999')
{
window.location="videoindexgoodip.htm";
;
}
else
{
window.location="videoindexbadip.htm";
;
}


</script>
<script language="JavaScript" src="
 
P.S. Don't forget about the 'Thank xxx for this valuable post!' link at the bottom of each post... it's a quick way of marking a post as helpful, and I see you've not used it yet in any of your posts yet.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
I still do not have it working correctly yet.

<script language="JavaScript">

VIH_BackColor = "palegreen";
VIH_ForeColor = "navy";
VIH_FontPix = "16";
VIH_DisplayFormat = "You are visiting from:<br>IP Address: %%IP%%<br>Host: %%HOST%%";
VIH_DisplayOnPage = "yes";

var ipaddr = '%%IP%%';
document.write('You are at '+ipaddr+' .');


if (ipaddr == '63.131.12.999')
{
window.location="videoindexgoodip.htm";
};
else
{
window.location="videoindexbadip.htm";
;
}

</script>
<script language="JavaScript" src="

You can see the code work getting the ip at:

And not work on the redirecting from using the ip at:

Thanks for any good insight.
Russ
 
Neither of those URLs are working for me, so I can't tell you anything by looking at them.

However, I know that the trailing semicolon after your 'if' block (right before the 'else') if not correct - in fact, if you look in Firebug (a great debugging aid for Firefox), it shows you immediately that that is a syntax error.

Why you felt the need to move it between posts of the code is beyond me, though.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
I fixed the syntax error and it is still not working. The problem appears to be getting the ip address varible to the if statement.

This is what i am testing with and failing now.


<script language="JavaScript">

VIH_BackColor = "palegreen";
VIH_ForeColor = "navy";
VIH_FontPix = "16";
VIH_DisplayFormat = "You are visiting from:<br>IP Address: %%IP%%<br>Host: %%HOST%%";
VIH_DisplayOnPage = "yes";

var ipaddr = '%%IP%%';
document.write('You are at '+ipaddr+' .');



if (VIH_HostIP == '63.131.12.170')
{
window.location="http:://;
}
else
{
window.location=";
}
</script>



The end product will be one page to reference the ip and redirect. The reason i listed two urls is to show that i am getting a url with the first one. And in the second one when i am using it to redirect, i do not have it.

You should be able to see it working getting the url.
And failing on the redirect.

This gets the url and show that part works = "

This gets the url and redirects: it flashes through the redirect:
"
Thanks for following this ugly.
Russ
 
It's funny - I had always assumed that '%%IP%%' was some sort of server-side thing that would be parsed out by your page, but it's clearly nothing of the sort, as when I view your source, I see that my IP address is apparently '%%IP%%'.

Why you still have this confusing, non-working code still left lying around is beyond me - I suggest you remove it ASAP.

If I look at the other single script you include, I can see that there is a variable, 'VIH_HostIP', which is populated correctly with my IP address (I guess you have figured this much out because in your code above, you are testing 'VIH_HostIP' instead of 'ipaddr').

Your remaining problem is very evident with the single JavaScript error thrown - by both IE and Fx - which I'm surprised you haven't seen: 'VIH_HostIP is undefined'.

This is probably due to two things:

1: An extra closing <script> tag. Validate your page!

2: You are including the external script AFTER attempting to use variables it contains.

I'm sure you can work out the rest!

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
if (ipaddr == '63.131.12.999')
{
window.location="videoindexgoodip.htm";
}; // get rid of this semi-colon
else
{
window.location="videoindexbadip.htm";
;
}

%%IP%% is just a place holder tag. You need to replace it with something. Normally a bit of REGEX would do the job.
 
I think I should just copy the script over. Now that script is PHP correct?
"src="Then the varible might be more easily accessed right?
Let me know your thoughts. Thank you


I would just do this in my html code:
<?php
//your php code here
?>

Below is the php code for getting the ip.
/*
Author: Robert Hashemian

You can use this code in any manner so long as the author's
name, Web address and this disclaimer is kept intact.
********************************************************
Usage Sample:

<script language="JavaScript">
VIH_BackColor = "palegreen";
VIH_ForeColor = "navy";
VIH_FontPix = "16";
VIH_DisplayFormat = "You are visiting from:<br>IP Address: %%IP%%<br>Host: %%HOST%%";
VIH_DisplayOnPage = "yes";
</script>
<script language="JavaScript" src="*/

if (typeof(VIH_BackColor)=="undefined")
VIH_BackColor = "white";
if (typeof(VIH_ForeColor)=="undefined")
VIH_ForeColor= "black";
if (typeof(VIH_FontPix)=="undefined")
VIH_FontPix = "16";
if (typeof(VIH_DisplayFormat)=="undefined")
VIH_DisplayFormat = "You are visiting from:<br>IP Address: %%IP%%<br>Host: %%HOST%%";
if (typeof(VIH_DisplayOnPage)=="undefined" || VIH_DisplayOnPage.toString().toLowerCase()!="no")
VIH_DisplayOnPage = "yes";

VIH_HostIP = "63.131.12.170";
VIH_HostName = "static-63-131-12-170.ind.choiceone.net";

if (VIH_DisplayOnPage=="yes") {
VIH_DisplayFormat = VIH_DisplayFormat.replace(/%%IP%%/g, VIH_HostIP);
VIH_DisplayFormat = VIH_DisplayFormat.replace(/%%HOST%%/g, VIH_HostName);
document.write("<table border='0' cellspacing='0' cellpadding='1' style='background-color:" + VIH_BackColor + "; color:" + VIH_ForeColor + "; font-size:" + VIH_FontPix + "px'><tr><td>" + VIH_DisplayFormat + "</td></tr></table>");
}
 
Name your file something.php
Code:
[COLOR=blue]
<?php
start_session();
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;
}
?>
[/color]
<script language="JavaScript">
VIH_BackColor = [COLOR=brown]"palegreen"[/color];
VIH_ForeColor = [COLOR=brown]"navy"[/color];
VIH_FontPix = [COLOR=brown]"16"[/color];
[COLOR=blue]
<?php
$ip = getRealIpAddr();
$VIH_HostIP = [COLOR=brown]"63.131.12.170"[/color];
[COLOR=green]// you can do it this way, but it's complicated[/color]
$VIH_DisplayFormat = "[COLOR=brown]You are visiting from:<br>IP Address: %%IP%%<br>Host: %%HOST%%"[/color];
$search = array ( [COLOR=brown]'/(*)%%IP%%(*)/i')[/color];
$replace = array ($1,$ip,$2);
$VIH_DisplayFormat = preg_replace($search, $replace, $VIH_DisplayFormat);
$search = array ( [COLOR=brown]'/(*)%%HOST%%(*)/i')[/color];
$replace = array ($1,$VIH_HostIP,$2);
$VIH_DisplayFormat = preg_replace($search, $replace, $VIH_DisplayFormat);
[COLOR=green]// even easier would be to not do the REGEX at all[/color]
$VIH_DisplayFormat = [COLOR=brown]'You are visiting from:<br>IP Address: '[/color] . $ip . [COLOR=brown]'<br>Host: '[/color] . $VIH_HostIP . [COLOR=brown]'"'[/color];
echo [COLOR=brown]'VIH_DisplayFormat = "'[/color] . $VIH_DisplayFormat . '"';
?>
[/color]
VIH_DisplayOnPage = "yes";
</script>
/*
 you can't put Javascript into a php file, you can add a php file as a Javascript script
 the way to include a php script is
<?php
 include(" script.php");
?>
<script language="JavaScript" src="[URL unfurl="true"]http://www.hashemian.com/js/visitorIP.js.php"></script>[/URL]
*/

if (typeof(VIH_BackColor)=="undefined")
  VIH_BackColor = "white";
if (typeof(VIH_ForeColor)=="undefined")
  VIH_ForeColor= "black";
if (typeof(VIH_FontPix)=="undefined")
  VIH_FontPix = "16";
if (typeof(VIH_DisplayFormat)=="undefined")
[COLOR=blue]
<?php
  echo [COLOR=brown]'VIH_DisplayFormat = "'[/color] . $VIH_DisplayFormat . [COLOR=brown]'"'[/color];
?>
[/color]
if (typeof(VIH_DisplayOnPage)=="undefined" || VIH_DisplayOnPage.toString().toLowerCase()!="no")
  VIH_DisplayOnPage = "yes";

VIH_HostIP = "63.131.12.170";
VIH_HostName = "static-63-131-12-170.ind.choiceone.net";

if (VIH_DisplayOnPage=="yes") {
  VIH_DisplayFormat = VIH_DisplayFormat.replace(/%%IP%%/g, VIH_HostIP);
  VIH_DisplayFormat = VIH_DisplayFormat.replace(/%%HOST%%/g, VIH_HostName);
  document.write("<table border='0' cellspacing='0' cellpadding='1' style='background-color:" + VIH_BackColor + "; color:" + VIH_ForeColor + "; font-size:" + VIH_FontPix + "px'><tr><td>" + VIH_DisplayFormat + "</td></tr></table>");
}
</script>

The above will work as long has your server recognises PHP.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top