Text message Verification Code sending class for IMEI restrictions implemented by php

Source: Internet
Author: User

Text message Verification Code sending class for IMEI restrictions implemented by php

Text message Verification Code sending class for IMEI restrictions implemented by php

?

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

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

<? Php

Class Api_Sms {

Const EXPIRE_SEC = 1800; // expiration Interval

Const RESEND_SEC = 60; // resend Interval

Const ONE_DAY_FREQ = 5; // number of messages sent to the same mobile phone number per day

Const ONE_DAY_IMEI_COUNT = 3; // Number of IMEI messages sent to the same mobile phone number each day

 

Public $ error = array ();

 

 

/**

* Send a verification code to a specified mobile phone number

* @ Param $ mobile

* @ Param $ imei

* @ Return bool

*/

Public function sendVerifyCode ($ mobile, $ imei ){

If (! $ This-> isMobile ($ mobile )){

$ This-> error = array ('code' =>-1, 'msg '=>' this phone number is amazing. Please enter it correctly and try again ');

Return false;

}

 

$ Redis = Api_Common: redis ();

$ VcKey = 'vc _ '. $ mobile;

$ LimitKey = 'vc _ LIMIT _ '. $ mobile;

 

// Verification Code resend limit

$ Data = json_decode ($ redis-> get ($ vcKey), true );

If ($ data & time () <$ data ['resend _ expire ']) {

$ This-> error = array ('code' =>-1, 'msg '=>' the text message has been sent within 1 minute. Please wait ');

Return false;

}

 

// Mobile phone number and IMEI restrictions

$ SendCnt = $ redis-> zScore ($ limitKey, $ imei );

If ($ sendCnt & $ sendCnt> = self: ONE_DAY_FREQ ){

$ This-> error = array ('code' =>-1, 'msg '=>' didn't receive the text message? Please wait or check whether the text message is blocked ');

Return false;

}

$ ImeiCnt = $ redis-> zCard ($ limitKey );

If ($ imeiCnt> = self: ONE_DAY_IMEI_COUNT &&! $ SendCnt ){

$ This-> error = array ('code' =>-1, 'msg '=>' exceeds the verification code sending device limit ');

Return false;

}

 

// Obtain the verification code

If (! $ Data ){

$ Vc = strval (rand (100000,999 999 ));

$ Data = array ('vc '=>$ vc, 'resend _ expire' => 0 );

$ Redis-> set ($ vcKey, json_encode ($ data ));

$ Redis-> expire ($ vcKey, self: EXPIRE_SEC); // set the verification code expiration time

}

$ Vc = $ data ['vc '];

 

$ Content = 'security verification code: '. $ vc;

$ Result = $ this-> send ($ mobile, $ content );

If ($ result ){

// Reset the retransmission time limit

$ Data ['resend _ expire '] = time () + self: RESEND_SEC;

$ Ttl = $ redis-> ttl ($ vcKey );

$ Redis-> set ($ vcKey, json_encode ($ data ));

$ Redis-> expire ($ vcKey, $ ttl );

 

// Set the mobile phone number and IMEI limit

$ Redis-> zIncrBy ($ limitKey, 1, $ imei );

$ Redis-> expireAt ($ limitKey, strtotime (date ('Y-m-d', strtotime ('+ 1 Day '))));

}

Return $ result;

}

 

/**

* Send a text message to a specified mobile phone number

* @ Param $ mobile

* @ Param $ content

* @ Return bool

*/

Public function send ($ mobile, $ content ){

// TODO calls the API of a specific service provider

Return true;

}

 

/**

* Determine whether the mobile phone number is valid

* @ Param $ mobile

* @ Return bool

*/

Private function isMobile ($ mobile ){

If (preg_match ('/^ 1 \ d {10} $/', $ mobile ))

Return true;

Return false;

}

 

/**

* Verify the SMS Verification Code

* @ Param $ mobile

* @ Param $ vc

* @ Return bool

*/

Public function checkVerifyCode ($ mobile, $ vc ){

$ VcKey = 'vc _ '. $ mobile;

$ VcData = json_decode (Api_Common: redis ()-> get ($ vcKey), true );

If ($ vcData & $ vcData ['vc '] ===$ vc ){

Return true;

}

Return false;

}

 

/**

* Clear the verification code

* @ Param $ mobile

*/

Public function cleanVerifyCode ($ mobile ){

$ Redis = Api_Common: redis ();

$ VcKey = 'vc _ '. $ mobile;

$ LimitKey = 'vc _ LIMIT _ '. $ mobile;

$ Redis-> del ($ vcKey );

$ Redis-> del ($ limitKey );

}

}

Additional text message Verification Code implemented by other users

?

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

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

<?

/*--------------------------------

Function: send text messages through the php http interface of China SMS

Modified on:

English version (http://http.c123.com/tx? Uid = user account & pwd = MD5 32-bit password & mobile = Number & content = content

Status:

100 sent successfully

101 Verification Failed

102 insufficient SMS

103 operation failed

104 invalid characters

105 too many contents

Too many 106 numbers

107 frequency too fast

108 The number is blank

109 account freeze

110 frequent single message sending prohibited

111 system tentative sending

The 112 number is incorrect.

120 system upgrade

--------------------------------*/

$ Uid = '000000'; // User Account

$ Pwd = '000000'; // Password

$ Mobile = '2017 12341234,13512341234, 100'; // number

$ Content = 'HTTP interface of China SMS web'; // content

// Instant sending

$ Res = sendSMS ($ uid, $ pwd, $ mobile, $ content );

Echo $ res;

 

// Timed sending

/*

$ Time = '2017-05-27 ';

$ Res = sendSMS ($ uid, $ pwd, $ mobile, $ content, $ time );

Echo $ res;

*/

Function sendSMS ($ uid, $ pwd, $ mobile, $ content, $ time = '', $ mid = '')

{

$ Http = 'HTTP: // http.c123.com/tx /';

$ Data = array

(

'Uid' => $ uid, // User Account

'Pwd' => strtolower (md5 ($ pwd), // MD5 32-bit password

'Mobile' => $ mobile, // number

'Content' => $ content, // content

'Time' => $ time, // timed sending

'Mid '=> $ mid // subextension number

);

$ Re = postSMS ($ http, $ data); // submit in POST Mode

If (trim ($ re) = '000000 ')

{

Return "sent successfully! ";

}

Else

{

Return "failed to send! Status: ". $ re;

}

}

 

Function postSMS ($ url, $ data = '')

{

$ Row = parse_url ($ url );

$ Host = $ row ['host'];

$ Port = $ row ['Port']? $ Row ['Port']: 80;

$ File = $ row ['path'];

While (list ($ k, $ v) = each ($ data ))

{

$ Post. = rawurlencode ($ k). "=". rawurlencode ($ v). "&"; // URL conversion standard code

}

$ Post = substr ($ post, 0,-1 );

$ Len = strlen ($ post );

$ Fp = @ fsockopen ($ host, $ port, $ errno, $ errstr, 10 );

If (! $ Fp ){

Return "$ errstr ($ errno) \ n ";

} Else {

$ Receive = '';

$ Out = "POST $ file HTTP/1.1 \ r \ n ";

$ Out. = "Host: $ host \ r \ n ";

$ Out. = "Content-type: application/x-www-form-urlencoded \ r \ n ";

$ Out. = "Connection: Close \ r \ n ";

$ Out. = "Content-Length: $ len \ r \ n ";

$ Out. = $ post;

Fwrite ($ fp, $ out );

While (! Feof ($ fp )){

$ Receive. = fgets ($ fp, 128 );

}

Fclose ($ fp );

$ Receive = explode ("\ r \ n", $ receive );

Unset ($ receive [0]);

Return implode ("", $ receive );

}

}

?>

The above is all the content of this article. I hope you will like it.

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.