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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vinodxx
Forum Newbie
Posts: 1
Joined: Tue Apr 21, 2009 6:54 am

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

Post 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
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

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

Post 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"
Post Reply