View Full Version : How do I redirect according to the time of day ?
gamer
12-29-2002, 12:36 PM
I really need a script to redirect visitors according to the time of day, or put some kind of timed access on my links to stop people clogging up my bandwidth when i'm playing on-line. I have searched everywhere and cannot find anything except clocks, redirects on mouse over, etc etc. I found a nice "day of the month redirect" so I assume it is possible.
I am a complete novice but learn very quickly.
I use Xitami webserver as my website is on my own pc,
www.gamer.sexidude.com
I dont want people to download during the evening, so if I can redirect to another page hosted by my isp then I can remove links on that page and solve the problem.
Any ideas would be much appreciated.
<script>
//get Time of the day (of the client)
time = new Date();
hours = time.getHours();
//set time-margin
starthour = 16;
endhour = 24;
//check what time is it
if(hours >= starthour && hours < endhour)
{
document.write('<a href="redirect_link">redirect_link</a>');
}else
{
document.write('<a href="normal_link">normal_link</a>');
}
</script>
This works - almost.
JavaScript shows the time of the client and not the server time. So it is different in all parts of the world.
For the server time you have to use PHP with almost the same script. If your server supports php.
Saludo, piz
gamer
12-29-2002, 01:51 PM
Thanks Piz, I think this will do nicely as I only need to restrict access for about 5 hours, it doesnt really matter about client timezone as most visitors are from the central european area the time difference isn't very big.
Unless someone can explain how I use server time, I will use this for now.
Thanks again. Piz
gamer
12-29-2002, 02:12 PM
Silly questions, but do I put it in the head or body of page, also My page uses 2 frames, so does it work from index.htm or the other 2 pages.
I think i'm missing something.
duniyadnd
12-29-2002, 04:58 PM
on IE 5+ you can put it on either, but i think its safer for this script to go after the body tag, because I'm not certain if document.write works above within the head tags or not on all browsers.
Duniyadnd
gamer
12-29-2002, 05:14 PM
I dont know what I'm doing wrong but I cannot get this to work, my page just loads up normally.
I've tried other pieces of code and splicing it all together but nothing works
gamer
12-29-2002, 05:18 PM
I have now got it to display "redirect_link"
What I need is for it to happen by itself, not display a link.
Any more ideas, thanks for the help so far.
gamer
12-29-2002, 05:26 PM
Thanks but I now have it working a little bit, I used the simple window.location"xxxxxxxxxxx"
But I now have the problem of the other frame which does not change.
I cant get the code to work at all in the index.htm as this quickly loads the framesets
ok, what would be the best way of redirecting?
It is possible to load in in the index.htm.
And with the window.location you can redirect ist.
If you want to start it from one of the frames and you want to change both frames:
parent.frames["framename or index"].location.href = ...
two times, to change both frames.
If you want to redirect to a document that should appear in the parent window:
parent.location.href = ...
Saludo
piz
gamer
12-29-2002, 05:59 PM
I would prefer to start it from the index.htm, but when I try it is ignored, Should I place it in HEAD, Frameset, or at the bottom in body,
also, I now have this:
<script type="text/javascript">
<!--
time = new Date();
hours = time.getHours();
starthour = 16
endhour = 24
if(hours >= starthour && hours < endhour)
{
location.href = "http://www.apdc74.dsl.pipex.com/"
}
else
This works but only changes the frame it is in.
Please let me know where to put it in my index.htm as I think this will solve my problem..
ok, you have to put it in the head.
if this doesn't work (but it should work), leave ist as you have it at replace location.href with parent.location.href.
This should work, too
gamer
12-29-2002, 06:05 PM
It now works a treat, I managed to get in inside the head statements on the index.htm page and it works very quickly.
Thnx again
gamer
12-29-2002, 06:49 PM
Anybody know how to alter this "simply please" to use the server side time instead of the client side. Not really important but could prove usefull to me.
<script type="text/javascript">
<!--
time = new Date();
hours = time.getHours();
starthour = 18
endhour = 24
if(hours >= starthour && hours < endhour)
{
location.href = "http://www.apdc74.dsl.pipex.com/"
}
else
{
}
//-->
</SCRIPT>
Does your server supports PHP?
JavaScript is a Client side language. It is interpreted by the browser.
What you could do is following, but this is... argh ;-)
You search a site where there is an element which alway contains the hour of your region (done with any server side technology). You include this site in an invisible frame in your site and you check the content of the element which contains the time...
I don't know any other way now.
But i'll post if i get an idea ;-)
whammy
12-29-2002, 07:06 PM
If you were using a common server-side language like PHP or ASP, I could give you the answer on that, but you might need to read up on:
http://www.xitami.com/developer.htm
gamer
12-30-2002, 12:21 AM
This is working a treat, however most of our clan members are central / east european therefore +1hr.
I tried to alter the endhour = 02
it didnt work anymore
I'm not thick, I just dont know the syntax etc, I grew up on C.E.S.I.L. and Z80 machine code.
endhour = 24 is great for my timezone.
How do I go beyond 00:00
Thnx
I tried it and it works.
You don't have to use 02, just use 2.
But it works anyway. Which Browser are you using?
Post your script, perhaps there is an error.
gamer
12-30-2002, 12:42 AM
maybe I treated it as 24 hr clock, i'll try this:
<script type="text/javascript">
<!--
time = new Date();
hours = time.getHours();
starthour = 18
endhour = 2
if(hours >= starthour && hours < endhour)
{
location.href = "http://www.apdc74.dsl.pipex.com/"
}
else
{
}
//-->
</SCRIPT>
thnx mate
aaah ok ;-)
at this hours i'm a little bit slower...
If you want to do it this way you have to turn round endhour and starthour.
you took the interval between 18 and 2. Why don't you take it another way round?
the interval between 2 and 18?
then you redirect in the else-part and not in the main part of the if-part.
;-)
ok?
gamer
12-30-2002, 01:22 AM
I'll look at this tomorrow when I finish work, and get my friends to set there clocks to help me, it wa working great until my clan member from oslo tried it, he still got access to my old page,
I guess it comes down to using server side time ref instead of client.
Thnx and goodnight.
Ken
gamer
12-30-2002, 04:25 PM
Piz, I turned it around and now it works fine, thank you for your time.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.