How to count the number of online users of a website using php _ PHP Tutorial

Source: Internet
Author: User
Php allows you to count the number of online users of a website. Php: how to count the number of online users of a website this article describes how to count the number of online users of a website in php. Share it with you for your reference. The specific implementation method is as follows :? 1234 how php calculates the number of online users of a website

This example describes how to use php to count the number of online users of a website. Share it with you for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

Function getIpAddress () {// obtain the IP address of the current user

If (getenv ('http _ CLIENT_IP ')){

$ Ip = getenv ('http _ CLIENT_IP ');

} Elseif (getenv ('http _ X_FORWARDED_FOR ')){

$ Ip = getenv ('http _ X_FORWARDED_FOR ');

} Elseif (getenv ('remote _ ADDR ')){

$ Ip = getenv ('remote _ ADDR ');

} Else {

$ Ip = $ _ SERVER ['remoe _ ADDR '];

}

Return $ ip;

}

Function writeover ($ filename, $ data, $ method = 'W', $ chmod = 0 ){

$ Handle = fopen ($ filename, $ method );

! Handle & die ("file opening failed ");

Flock ($ handle, LOCK_EX );

Fwrite ($ handle, $ data );

Flock ($ handle, LOCK_UN );

Fclose ($ handle );

$ Chmod & @ chmod ($ filename, 0777 );

}

Function count_online_num ($ time, $ ip ){

$ FileCount = './count.txt ';

$ Count = 0;

$ Gap = 900; // Click it in 15 minutes.

If (! File_exists ($ fileCount )){

$ Str = $ time. "\ t". $ ip. "\ r \ n ";

Writeover ($ fileCount, $ str, 'W', 1 );

$ Count = 1;

} Else {

$ Arr = file ($ fileCount );

$ Flag = 0;

Foreach ($ arr as $ key => $ val ){

$ Val = trim ($ val );

If ($ val! = ""){

List ($ when, $ seti) = explode ("\ t", $ val );

If ($ seti ==$ ip ){

$ Arr [$ key] = $ time. "\ t". $ seti;

$ Flag = 1;

} Else {

$ CurrentTime = time ();

If ($ currentTime-$ when> 900 ){

Unset ($ arr [$ key]);

} Else {

$ Arr [$ key] = $ val;

}

}

}

}

If ($ flag = 0 ){

Array_push ($ arr, $ time. "\ t". $ ip );

}

$ Count = count ($ arr );

$ Str = implode ("\ r \ n", $ arr );

$ Str. = "\ r \ n ";

Writeover ($ fileCount, $ str, 'W', 0 );

Unset ($ arr );

}

Return $ count;

}

$ Time = time ();

$ Ip = getIpAddress ();

$ Online_num = count_online_num ($ time, $ ip );

Echo $ online_num;

?>

I hope this article will help you with php programming.

Examples in this article describes how php can calculate the number of online users of a website. Share it with you for your reference. The specific implementation method is as follows :? 1 2 3 4...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.