Pentru a apela o functie Ajax cand pagina e inchisa, aplicati evenimentul beforeunload la obiectul "window", care sa apeleze functia ajax cand evenimentul este emis.
- Cererea ajax trebuie facuta in mod sincron (synchronous).
<script>
// sends data to a php file, via POST, when page is closed
function pageClosed() {
 var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); // XMLHttpRequest object
 var data ='name=val'; //data with name and value to send to your_script.php
 request.open('post', 'your_script.php', false); //false - to make synchronous request

 request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
 request.send(data); //sends data

 console.log(data); //for debug to see if code is executed
 return true;
}

//register event emited when page is closed, it calls the pageClosed() function
window.addEventListener('beforeunload', pageClosed);
</script>
- In scriptul php apelat de Ajax se adauga functia ignore_user_abort(true);. Permite rularea scriptului php si dupa ce fereastra browser-ului este inchisa.

Inregistrare timp petrecut de utilizatori pe site

Iata o aplicatie practica a codului dat mai sus.
Urmatorul script inregistreaza intr-un fisier pe server timpul petrecut de utilizatori pe site (in format json). Aici se inregistreaza utilizatorul dupa IP.

1. Adaugati urmatorul script ajax in paginile din site:
<script>
var start_time = Math.floor(Date.now() /1000);

// sends to php the number of seconds the user spend on time
function pageClosed() {
 var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); // XMLHttpRequest object
 var data ='tms='+(Math.floor(Date.now() /1000) - start_time); //data to send to reg_time.php
 request.open('post', 'reg_time.php', false); //false - to make synchronous request

 request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
 request.send(data); //sends data

 console.log(data); //for debug to see if code is executed
 return true;
}

//register event emited when page is closed, it calls the pageClosed() function
window.addEventListener('beforeunload', pageClosed);
</script>
2. Adaugati urmatorul cod intr-un fisier php: "reg_time.php".
<?php
ignore_user_abort(true);

if(isset($_POST['tms'])){
 $tms = intval($_POST['tms']);
 $user = $_SERVER['REMOTE_ADDR']; //here we use the ip
 $file ='reg_time.json';
 $f_data =[];

 //add the $user and $tms in json format in $file
 if(!file_exists($file)){
 $f_data[$user] = $tms;
 } else {
 $f_data = json_decode(file_get_contents($file), true);
 if(isset($f_data[$user])) $f_data[$user] = $f_data[$user]+ $tms;
 else $f_data[$user] = $tms;
 }

 file_put_contents($file, json_encode($f_data));
}
3. Urmatorul cod php poate fi utilizat pentru a citi si afisa datele inregistrate in "reg_time.json":
<?php
$file ='reg_time.json';
$f_data =[];

if(file_exists($file)) $f_data = json_decode(file_get_contents($file), true);

foreach($f_data as $k=>$v){
 echo '<br>'. $k.' = '.$v.' seconds';
}

Un Test simplu in fiecare zi

HTML
CSS
JavaScript
PHP-MySQL
Engleza
Spaniola
Ce tag HTML5 este indicat sa contina meniul cu link-uri de navigare in site?
<section> <nav> <article>
<nav><ul>
 <li><a href="http://coursesweb.net/css/" title="CSS Course">CSS Course</a></li>
 <li><a href="http://www.marplo.net/jocuri/" title="Flash Games">Flash Games</a></li>
</ul></nav>
Ce proprietate CSS muta elementrul in dreapta sau stanga de la locul unde e?
text-align clear float
.some_class {
  width: 30%;
  float: left;
}
Clic pe metoda obiectului Math care rotunjeste numarul x descrescator la cel mai apropiat intreg.
Math.ceil(x) Math.abs(x) Math.floor(x)
var num = 12.34567;
num = Math.floor(num);
alert(num);       // 12
Indicati functia PHP care returneaza numarul de caractere dintr-un sir.
mb_strlen() count() stristr()
$str = "sir cu caractere utf-8 åèö";
$nrchr = mb_strlen($str);
echo $nrchr;        // 29
Clic pe raspunsul potrivit la intrebarea: "When it happened?".
On the 7th of July 1996 It is a quarter to 5. Nice weather.
When it happened? On the 8th of August 2001.
- Cand s-a intamplat? Pe 8 August 2001.
Indicati raspunsul potrivit la intrebarea: "¿Cuando sucedió?".
Hace buen tiempo. En el 8 de Agosto de 2001 Son las tres menos cuarto.
¿Cuando sucedió? En el 8 de Agosto de 2001.
- Cand s-a intamplat? Pe 8 August 2001.
Apelare functie Ajax la inchidere pagina

Last accessed pages

  1. Introducere in HTML (8533)
  2. Coduri pt culori (24941)
  3. Instructiuni repetitive for si while (1645)
  4. Curs HTML gratuit Tutoriale HTML5 (30146)
  5. Pronumele direct si indirect (758)

Popular pages this month

  1. Gramatica limbii engleze - Prezentare Generala (2367)
  2. Prezentul simplu si continuu - Present Tense Simple and Continuous (1301)
  3. Cursuri si Tutoriale: Engleza, Spaniola, HTML, CSS, Php-Mysql, JavaScript, Ajax (1197)
  4. Conditional IF in Limba Engleza - Fraze Conditionale (856)
  5. Prezent perfect si Prezent perfect continuu - Present perfect and Continuous (804)