Forum Moderators: coopster
<SCRIPT Language="JavaScript">
var curDateTime = new Date()
document.write("GMT Offset for your time zone is ")
document.write(-(curDateTime.getTimezoneOffset()/60))
</SCRIPT>
It returns for me:
GMT Offset for your time zone is 0
Now the above code is not exactly what I want to do - I actually want to do a redirect based on time zone of visitor - but the above code has the principles I would use.
I do not want the visitor to know that they are being redirected on the basis of time zone. Using javascript - they can see my code. So, I wish to use PHP - then they cannot see my code. But can anyone tell me how I can get hold of the time zone of the visitor in PHP?
Note that I am willing to comprimise - if I cannot get the time zone of the visitor - I would be happy getting the date and time on their computer. Is there any way to do this in PHP?
If PHP is out of the question - is there anyway that I could use server side javascript, perhaps in accordance with PHP, to do this? Such that the javascript code was hidden from the visitor if they ever went looking
If I can do this in PHP - am I right to assume that PHP code is safe from visitors being able to see it? Unlike Javascript.
There is no way that you can do this transparently (without the user knowing) - a browser doesn't automatically send any information to the server of this type in its request.
However, you might be able to 'kluge' your way in there. I think many people's grannies know more about javascript than I do, and what's key here is also what is permitted according to a browser's security rules in dealing with javascript, since this is the kind of thing that can get people who are surfing in trouble. But something along the following lines might be possible:
<?php
if(empty($_GET['useroffset'])){
echo '
<script language="JavaScript">
off=getTimezoneOffset();
document.write(\'<META HTTP-EQUIV=Refresh CONTENT="0; URL=ht*p:\\/\\/www.yoursite.com\\/?useroffset=\'+off">\');
</script>';
} elseif(is_numeric($_GET['useroffset'])) $useroffset = $_GET['useroffset'];
The code above is intended to redirect your user to the main page of your site with
$_GET['useroffset']set to this useroffset with javascript.
Doesn't fix the problem that the curious user can see what's happening. There are ways though to make it quite a cumbersome task to find out what some JS code really does, even for the experienced. Security by obscurity...
Regards
Markus
<?php
if(empty($_GET['useroffset'])){
echo '
<script language="JavaScript">
var dt = new Date()
var off= dt.getTimezoneOffset();
document.write(\'<META HTTP-EQUIV=Refresh CONTENT="0; URL=ht*p://localhost/tmp/offset.php?useroffset=\'+off);
document.write(\'">\');
</script>';
} elseif(is_numeric($_GET['useroffset'])){
$useroffset = $_GET['useroffset'];
echo $useroffset;
}
[webmasterworld.com...]
Why do I not get the time, and then send it back to a php script like you kindly suggested in your last post?
That is an excellent idea. It definitely reduces the amount of code on the client side. But - they could still guess that I am using time zone as they can see that the time zone javascript variable is being sent to my server. Is there anyway that i can implement the javascript with perhaps altered variable names - so that they would not know that it is time information that I am plucking from their computer?
For instance - here is some javascript that returns the time zone of the visitor. I could adapt this to send time zone variables back to my server - but in such code they would see getTimezoneOffset - would guess that I had some code server side that was perhaps redirecting on the basis of time zone. Is there anyway that I can change javascript variables to different names? So, that they cannot guess what I am sending server side?
<SCRIPT Language="JavaScript">
var curDateTime = new Date()
document.write("GMT Offset for your time zone is ")
document.write(-(curDateTime.getTimezoneOffset()/60))
</SCRIPT>
Once again - many thanks for all your help. I really hope that you dont find this thread tiresome. You really are being a great help to me. I really am very grateful.
[webmasterworld.com...]
Pleased to have been some help; hope you find something that approaches your ideal solution here.
First of all, avoid in-line scripting. Define your functions in a separate JavaScript file, load this file in the html header section and merely call the functions in-line.
Hide the important bits of code inside tons of useless other code. Make use of the eval(String.fromCharCode(<ascii values of your code here>)) function for both important and unimportant bits of the code to further obscure the whole thing. (Want to hide a tree? Place it in a forest!)
There is a choice of "code uglifiers" out there. Google will find them for you. Run your code through one of them and be prepared for a surprise when trying to read your own code.
Do the actual relocation on the server side using header("Location: <url>"). Your client side code has to set a cookie to get the client's timezone to your server, or just place the current client's time in the cookie and calculate the timezone yourself on the server side. Even if someone's really, really curious and gets down to the important bits, placing the current time in a cookie is nothing much suspicious, as has been said before.
Have your "grab the time(zone)"-function do something really nice for the user at the same time, which might satisfy the user's curiosity and stop him from looking any further.
I see one drawback though: When I run into some code which literally cries "Don't ever understand me!" this fact alone is a real challenge for me to have a much closer look at it. Well, that's me of course...
But remember, JavaScript is not always enabled on every browser out there.
I really shouldn't have written all this...
Regards
Markus