1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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

Discussion in 'PHP' started by vinodxx, Apr 21, 2009.

  1. #1
    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 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


    1. <form action = "" method = "post">
       2. Number : <input type = "text" name = "name" />
       3. <input type = "submit" />
       4. </form>
    PHP:


    User can try to match the variable $x with "Number" say 10 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
     
    vinodxx, Apr 21, 2009 IP
  2. Grobbulus

    Grobbulus Peon

    Messages:
    557
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here is a link that might help you. I'm not entirely sure what exactly you want the page to do but I'm sure you would be able to handle it with sessions or directly with a database.


    http://www.php.net/manual/en/book.session.php
     
    Grobbulus, Apr 21, 2009 IP
  3. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #3
    Variables are always "destroyed" when you refresh/close the page.
    The easiest way would be to use PHP session : http://lv.php.net/session
     
    ActiveFrost, Apr 21, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That's unfortunate, since there's no way to guarantee you will get to run your code when the user closes the browser window or navigates away to a new site.
     
    SmallPotatoes, Apr 21, 2009 IP
  5. vinodxx

    vinodxx Guest

    Messages:
    292
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    When you refresh a page, all variables are not destroyed.
    This I verified with PHP.
     
    vinodxx, Apr 21, 2009 IP
  6. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #6
    Don't mind to share it with us ?
     
    ActiveFrost, Apr 21, 2009 IP
  7. SiteTalkZone

    SiteTalkZone Peon

    Messages:
    243
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No, not all of them, most of them. Superglobal arrays will stay obviously.

    You can use xajax libary to register the PHP functions then use Javascript to calle them when ever the hell you want. xajaxproject.org
     
    SiteTalkZone, Apr 22, 2009 IP
  8. buldozerceto

    buldozerceto Active Member

    Messages:
    1,137
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    88
    #8
    use the onunload or onbeforeunload javascript to call PHP page/code.
     
    buldozerceto, Apr 22, 2009 IP