PDA

View Full Version : Auto Redirect on key press?



BLiZZaRD
11-22-2005, 08:07 AM
Hello again! I have been working on writing a code, and all I can come up with is CRAP!

What I am trying to accomplish is something like this:

When visitor comes to mysite.com/page1.php, and hits the browser "refresh" button say, 3 times, I want to kick in an auto redirect to mysite.com/page2.php

I have tried numerous things but all I can accomplish is either a redirect upon entering page1.php, or parse errors.

If this is not possible with the "refresh" button, perhaps it is possible with a "hot key" or something similar?

Just to give some idea of what I am trying to do (and to show off my IMMENSE newbie-ness :p ) here is what I have made so far (with about 5 million errors of course)


<?php
$i=1;
on (KeyDown "CTRL" ++i);
if $i<=3
{
echo "You have " .$i. " less chances<br />"
else if $i == 3
{
$URL = "http://mysite.com/page2.php"
header ("Location: $URL");
}
}
?>


I am back to the drawing board awaiting any help!

Thanks!
gawd I feel stupid!!!

jscheuer1
11-22-2005, 09:04 AM
You could set a session cookie that, each time the page is reloaded gets read and if it is less than the desired number, set again incremented by one, when the desired number is reached, the redirect could happen. A nice touch would be to delete the cookie at that point. Cookies are a little tricky to work with but, the below link has boiler plate code for setting reading and deleting cookies.

Info on and code for Cookies (http://www.quirksmode.org/js/cookies.html)

BLiZZaRD
11-22-2005, 09:37 AM
Thanks Prof. I love the idea! I read the page, and all I can say is "HUH??!!??"

Cookies are worse for me than php, and I have been doing that for only a couple weeks.

If its not too much trouble, might I get an example of what you mean exactly?

mwinter
11-22-2005, 11:27 AM
Cookies are a little tricky to work with but, [QuirksMode] has boiler plate code for setting reading and deleting cookies.It has code for setting cookies client-side, but the OP should be doing it server-side.



/* Has a cookie already been set? If so, was
* it set for this page?
*/
if(isset($_COOKIE['uri']) && ($_SERVER['REQUEST_URI'] === $_COOKIE['uri'])) {
/* Yes, so check how many times it's been
* visited.
*
* Note that the 'visits' value will
* lag: it's set to '1' on the first visit,
* but will still be '1' on the second
* (until the else branch below).
*/
if('3' === $_COOKIE['visits']) {
/* This page has been revisited three
* times (four visits in total), so
* delete the cookies and redirect.
*/
header('HTTP/1.1 302 Found');
header('Location: http://www.example.com/');
setcookie('uri', '', 0);
setcookie('visits', '', 0);
} else {
/* Increment the visit count and
* continue.
*/
setcookie('visits', (string) $_COOKIE['visits'] + 1);
}
} else {
/* This is the first visit (or the first
* since a previous redirect), so setup
* for future encounters.
*/
setcookie('uri', $_SERVER['REQUEST_URI']);
setcookie('visits', '1');
}
This could be used in multiple places as it stores where the cookie was set. If it's different from the current location, then the cookie is reset. Be aware though, that

&#160;&#160;/index.php

and

&#160;&#160;/index.php?name=value

will be considered different for the purposes of this test, though that's probably not an issue.

Hope that helps,
Mike

Twey
11-22-2005, 03:17 PM
<?php
$i=1;
on (KeyDown "CTRL" ++i);
if $i<=3
{
echo "You have " .$i. " less chances<br />"
else if $i == 3
{
$URL = "http://mysite.com/page2.php"
header ("Location: $URL");
}
}
?>
gawd I feel stupid!!!No offense intended or anything, but I'm not surprised :p
PHP is server-side. It can't handle client-side events like keypresses. :)

BLiZZaRD
11-22-2005, 11:06 PM
No offense intended or anything, but I'm not surprised :p
PHP is server-side. It can't handle client-side events like keypresses. :)


No offense taken, lol. Running my site, forum and chat room, let alone my paying job, the flash games I build, and on and on... learning php one script at a time, one script a day, makes for some large headaches, LOL.

Thanks Twey, you always know what to say :p

@mwinter.. thanks for that! I am reading and understanding a little more now.. I will try this on a test page and tweak as needed until I get what I am after! MUCH THANKS!

nightdriver09
12-06-2005, 09:18 AM
I agree with Twey. Keypress is client-side.
you could do this
<html>
<head>
<script>
var n_times=0;
function Redirect()
{
n_times++;
if ( n_times==3 )
window.location.href = "another.site.htm.or.php";
else
alert('you have ' + 3-n_times + ' chances.' );
}
</script>
</head>
<body onkeydown="Redirect();">
</body>
</html> :)

Twey
12-06-2005, 04:58 PM
That won't work, by the way. Besides, an alternative has already been given.

Scarlet_Webz
03-28-2006, 09:55 PM
Ehm... I feel like noob now...:P

Well I have a lot of HTML experience, but after an sabattical the new thing was PHP. So I started learning that now.
However I came upon the idea of a Easter Egg on all my websites...
But I wanted to bind it to a set of keys.
Like CTRL+ALT+O or something.

And I though this code was ok but it doesnt work I believe no.
And my editor doenst give it either.

So can anyone give me a PHP script for a key-bind action?
And pleaz explain it, so i am able to edit it.

Oh, and pleaz email me the answer (aside from the forums too pleaz) cause I guess I will not be here a lot and I would like to have this fixed soon...

email: mesj.pwnage@gmail.com

Scarlet_Webz

djr33
03-29-2006, 12:29 AM
Seems like it's handled, but if you really want to control key presses, remember that it won't work with server-side, as Twey said, so you will have to use client-side. Basically, get into javascript, and learn all that.
I don't know it, so I'm not saying its easy (or hard, for that matter), but it'll do that if you want something more controlled by a keypress than what you're getting with php.

Hope it works out.

mwinter
03-29-2006, 02:26 PM
However I came upon the idea of a Easter Egg on all my websites...
But I wanted to bind it to a set of keys.In my opinion, that's a bad idea. You have no idea what functionality might be bound to a particular keystroke sequence. You would probably be better off hiding a link somewhere.


So can anyone give me a PHP script for a key-bind action?The following has been stated several times now in this thread: PHP is server-side. It cannot detect keystokes.


Oh, and pleaz email me the answer [...]Umm, no. That sort of request is rather rude. If you don't want to keep coming back to check for new posts, then I suggest that you subscribe to this thread (http://www.dynamicdrive.com/forums/subscription.php?do=addsubscription&t=6221), and get e-mail notifications from the forum itself.

Mike