Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Auto Redirect on key press?

  1. #1
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default Auto Redirect on key press?

    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 ) here is what I have made so far (with about 5 million errors of course)
    Code:
    <?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!!!
    Last edited by BLiZZaRD; 11-22-2005 at 09:46 AM.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    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?
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1
    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.

    PHP Code:
    /* 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

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <?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");
      }
    }
    ?>
    Code:
    gawd I feel stupid!!!
    No offense intended or anything, but I'm not surprised
    PHP is server-side. It can't handle client-side events like keypresses.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by Twey
    No offense intended or anything, but I'm not surprised
    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

    @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!
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  7. #7
    Join Date
    Dec 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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>

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That won't work, by the way. Besides, an alternative has already been given.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •