0

Possible Duplicate:
how can I make visitor counter in php?

I want to insert when page open/close or leave but not onunload .. the below code help me to insert when open the page , but not when close or leave

session_start();
if (!isset($_SESSION["visits"]))
    $_SESSION["visits"] = 0;
if ($_SESSION["visits"] > 1){
    echo 'visit='.$_SESSION["visits"];
    echo "You hit the refresh button!";}
else{
    mysql_query(
        "INSERT INTO najd_visit( visit_userId, visit_staticId, visit_page,
            visit_enterTime)VALUES ('$userId', '$Sid', '$title', '$date') ");
    $_SESSION["visits"] = $_SESSION["visits"] + 1;
    echo 'visit='.$_SESSION["visits"];
    echo "This is my site";
}
Community
  • 1
  • 1
beginner php
  • 27
  • 1
  • 9
  • This is the 3rd time you have asked this question in some form. http://stackoverflow.com/questions/10784163/how-unset-session-when-close-the-web-page-in-php , http://stackoverflow.com/questions/10862686/how-can-i-make-visitor-counter-in-php – Jeremy Harris Jun 02 '12 at 14:27
  • yes , I am new of using this website ... still try how to use it – beginner php Jun 02 '12 at 14:31
  • Only ask one question and make edits to it if you need to rephrase your wording. Also, please read the FAQ: http://stackoverflow.com/faq – Jeremy Harris Jun 02 '12 at 14:37

2 Answers2

0

You cant run something via PHP when someone "leaves" the page - because the browser nevers sends anything to your server to trigger the script.

You only possible option is to use javascript and run it as they leave - but its going to be temperamental and unreliable at best.

Laurence
  • 58,936
  • 21
  • 171
  • 212
0

You can use Javascript and Ajax. Javascript: Intercept page exit event Ajax - http://www.w3schools.com/ajax/default.asp

Edit: I don't think that you can do it without unload, php is a server-side language, not user-side.

Community
  • 1
  • 1
Yehonatan
  • 3,168
  • 7
  • 31
  • 39