SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I execute a PHP code when user leaves/refreshes the page

    Hello friends,

    Have a question here.

    I want to execute a particular chunk of PHP code when a user closes the browser window or he refreshes the webpage.

    How it can be done in PHP.

    Let me explain with an example.

    A random number is generated and passed it to a variable(say $x) when a user accesses the webpage for the first time
    This variable's value must be preserved till user closes the webpage or she refreshes.

    This webpage includes a form whose input is compared against the variable $x.
    This form is a self directed one - action = ""

    Form code


    PHP Code:
    <form action "" method "post">
    Number : <input type "text" name "name" />
    <
    input type "submit" />
    </
    form


    User can try to match the variable $x with "Number" say 5 number of times.

    Needs are:
    1) The variable $x must not change its value between form submissions.
    2) When page refresh/close occurs $x must be destoyed.

    How it can be done.

    Regards,
    Vinod

  2. #2
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    Bowen Mountain, NSW
    Posts
    490
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't know if all requests can be completed, however, holding a value (example, ensuring that $x = 2) for the whole time the user stays online, you need to use $_SESSION[] variable (global variables).

    You can use session_destroy() too.
    Regards,
    BJ Duncan

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    You can't execute PHP code when the user exits a page. But what you want to do, can be achieved by a combination of session variables and checking if the form has been submitted.
    Something like:
    PHP Code:
    <?php
      session_start
    ();
      if (!isset(
    $_POST['submit'])) {
        
    // if the form hasn't been submitted, initialize the session variables
        
    $_SESSION['randomnumber'] = // generate random number here;
        
    $_SESSION['try'] = 1;
      } else {
        
    // if the form has been submitted, check the number inserted by the user against the random number in the session
        
    if ($_SESSION['randomnumber'] == $_POST['name']) {
          
    // do whatever you want to do when the user has guessed the number 
        
    } else {
          
    $_SESSION['try']++;
          if (
    $_SESSION['try'] > 5) {
            
    // do whatever you want to do when the number of tries has been used
          
    }
        }
      }
    ?>
    <form action = "" method = "post">
    Number : <input type = "text" name = "name" />
    <input type = "submit" />
    </form>

  4. #4
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can not execute PHP when a visitor leaves the page, but there is something you can do.

    Setup PHP that you want executed, and then have Javascript request that PHP page using the "onunload" event of the window in the browser.


Tags for this Thread

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
  •