Home > Backend Development > PHP Tutorial > Statistics through session and cookie

Statistics through session and cookie

WBOY
Release: 2016-07-25 08:47:49
Original
1005 people have browsed it
Statistics through session and cookie
  1. // Statistics through session
  2. //Use text to store data
  3. if($_SESSION[temp]==""){ //Judge whether the value of $_SESSION[temp]=="" is empty, where temp is a custom variable
  4. if(($fp=fopen("counter.txt","r"))==false){
  5. echo "Failed to open file!";
  6. }else{
  7. $counter=fgets( $fp,1024); //Read the data in the file
  8. fclose($fp); //Close the text file
  9. $counter++; //Increase the counter by 1
  10. $fp=fopen("counter.txt","w") ; //Open the text file for writing
  11. fputs($fp,$counter); //Increase the new statistics by 1
  12. fclose($fp); } //Close the text
  13. $_SESSION[temp]=1; //After the counter value is increased, assign 1 to $_SESSION[temp]
  14. }
  15. //Read statistical data from the text file
  16. if(($fp=fopen("counter. txt","r"))==false){
  17. echo "Failed to open file!";
  18. }else{
  19. $counter=fgets($fp,1024);
  20. fclose($fp);
  21. }
  22. // ---------------------------------------
  23. // Statistics through cookies
  24. $f_open = fopen ("count.txt","r+"); //Open the specified file
  25. $count = fgets($f_open); //Read the data in the file
  26. if(empty($_COOKIE['cookie_name'])) { //Determine whether the COOKIE exists
  27. setcookie("cookie_name",value,time()+1800); //If it does not exist, create the COOKIE
  28. $count = $count + 1; //Change the value of the variable $count Add 1
  29. rewind($f_open); //Open the specified file
  30. fwrite($f_open,$count); //Write new data to the file
  31. fclose($f_open); //Close the file
  32. }
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template