Page 1 of 1

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

Posted: Tue Apr 21, 2009 7:17 am
by vinodxx
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

Code: Select all

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

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

Posted: Tue Apr 21, 2009 8:08 am
by it2051229
you cannot execute a PHP page when the browser refreshes or closes the browser... but regarding your problem the hint that you can make use is either a "session" or a "cookie"