<
!DOCTYPE html
>
<
html lang
=
"en"
>
<
head
>
<
title
>
鄭州傳智播客
<
/
title
>
<
meta charset
=
"UTF-8"
>
<
meta
name
=
"Author"
content
=
"PHP就業老師"
/
>
<
style type
=
"text/css"
>
*
{
margin
:
0
;padding
:
0
;
}
<
/
style
>
<
script
type
=
"text/javascript"
src
=
"js/jquery-1.11.3.min.js"
>
<
/
script
>
<
script
type
=
"text/javascript"
>
var InterValObj;
/
/
timer變量,控制時間
var count
=
60
;
/
/
間隔函數,
1
秒執行
var curCount;
/
/
當前剩餘秒數
function codeRandom
(
chars
)
{
var res
=
""
;
for
(
var i
=
0
;i
<
chars;i
+
+
)
{
res
+
=
Math.floor
(
Math.random
(
)
*
10
)
;
}
return
res;
}
/
/
timer處理函數
function SetRemainTime
(
)
{
if
(
curCount
=
=
0
)
{
window
.clearInterval
(
InterValObj
)
;
/
/
停止計時器
$
(
"#sub"
)
.removeAttr
(
"disabled"
)
;
/
/
啓用按鈕
$
(
"#sub"
)
.val
(
"重新發送驗證碼"
)
;
code
=
""
;
/
/
清除驗證碼。如果不清除,過時間後,輸入收到的驗證碼依然有效
}
else
{
curCount
--;
$
(
"#sub"
)
.val
(
"請在"
+
curCount
+
"秒內輸入驗證碼"
)
;
}
}
/
/
獲取手機號碼
$
(
function
(
)
{
$
(
"#sub"
)
.click
(
function
(
)
{
var
phone
=
$
(
"#phone"
)
.val
(
)
;
/
/
獲取手機號碼
curCount
=
count;
if
(
phone
!
=
''
)
{
/
/
驗證手機有效性
var myreg
=
/
^
(
(
(
13
[
0
-9
]
{
1
}
)
|
(
15
[
0
-9
]
{
1
}
)
|
(
18
[
0
-9
]
{
1
}
)
)
+
\d
{
8
}
)
$
/
;
if
(
!myreg.test
(
$
(
'
#phone').val()))
{
alert
(
'請輸入有效的手機號碼!'
)
;
return
false
;
}
phone
=
$
(
'
#phone').val();
/
/
驗證碼
(
隨機生成
)
var code
=
codeRandom
(
4
)
;
/
/
設置
button
效果,開始計時
$
(
"#sub"
)
.attr
(
"disabled"
,
"true"
)
;
$
(
"#sub"
)
.val
(
"請在"
+
curCount
+
"秒內輸入驗證碼"
)
;
InterValObj
=
window
.setInterval
(
SetRemainTime
,
1000
)
;
/
/
啓動計時器,
1
秒執行一次
/
/
發送數據到後臺 通過Ajax
$.ajax
(
{
/
/
設置的參數
type
:
"post"
,
url
:
"data.php"
,
data
:
{
"code"
:
code
,
"phone"
:
phone
}
,
success
:
function
(
msg
)
{
alert
(
msg
)
;
}
}
)
;
}
else
{
alert
(
'請填寫手機號碼'
)
;
}
}
)
;
}
)
;
<
/
script
>
<
/
head
>
<
body
>
<
h
1
>
PHP
-
短信驗證碼
<
/
h
1
>
手機驗證碼
:
<
input type
=
"text"
name
=
"phone"
id
=
"phone"
>
<
input
id
=
"sub"
type
=
"button"
value
=
"發送驗證碼"
/
>
<
/
body
>
<
/
html
>
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
|
?php
/
*
*
*
Created
by
PhpStorm.
*
User
:
Leo
*
Date
:
2017
/
8
/
30
*
Time
:
14
:
59
*
/
/
/
$_post
$
phone
=
isset
(
$_POST['
phone
']
)
?$_POST['
phone
']
:
'';
$code
=
isset
(
$_POST['code']
)
?$_POST['code']
:
'';
require
(
dirname
(
__FILE__
)
.'
/
config.php'
)
;
require
(
dirname
(
__FILE__
)
.'
/
SendSMS.php'
)
;
/
/
實例化短信發送類
$sms
=
new
SendSMS
(
$options['account']
,
$options['
password
']
)
;
$
context
=
'驗證碼'.$code;
$res
=
$sms
-
>
send
(
$
phone
,
$
context
)
;
if
(
$res
)
{
echo
"成功"
;
}
else
{
echo
"失敗"
;
|
01
02
03
04
05
06
07
08
09
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
|
<
?php
/
*
*
*
Created
by
PhpStorm.
*
User
:
Leo
*
Date
:
2017
/
8
/
30
*
Time
:
15
:
26
*
/
/
*
*
*
設置用戶信息
*
/
class
SendSMS
{
const SENDURL
=
'http
:
/
/
gd.ums
86.
com
:
8899
/
sms
/
Api
/
Send.do';
private $_un;
private $_pw;
function __construct
(
$user
,
$pwd
)
{
$this
-
>
_un
=
$user;
$this
-
>
_pw
=
$pwd;
}
function send
(
$
phone
,
$
content
,
$isreport
=
0
)
{
/
/
發送數據
$
data
=
array
(
'un'
=
>
$this
-
>
_un
,
'pw'
=
>
$this
-
>
_pw
,
'sm'
=
>
$
content
,
'da'
=
>
$
phone
,
'rd'
=
>
$isreport
,
'rf'
=
>
2
,
'tf'
=
>
3
,
'dc'
=
>
15
,
)
;
$
url
=
SendSMS
:
:
SENDURL.'?'.http_build_query
(
$
data
)
;
$this
-
>
curlGet
(
$
url
)
;
}
public function curlGet
(
$
url
)
{
$ch
=
curl_init
(
)
;
curl_setopt
(
$ch
,
CURLOPT_HEADER
,
0
)
;
curl_setopt
(
$ch
,
CURLOPT_RETURNTRANSFER
,
1
)
;
curl_setopt
(
$ch
,
CURLOPT_URL
,
$
url
)
;
$res
=
curl_exec
(
$ch
)
;
curl_close
(
$ch
)
;
return
$res;
}
}
|
<
!DOCTYPE html
>
<
html lang
=
"en"
>
<
head
>
<
title
>
鄭州傳智播客
<
/
title
>
<
meta charset
=
"UTF-8"
>
<
meta
name
=
"Author"
content
=
"PHP就業老師"
/
>
<
style type
=
"text/css"
>
*
{
margin
:
0
;padding
:
0
;
}
<
/
style
>
<
script
type
=
"text/javascript"
src
=
"js/jquery-1.11.3.min.js"
>
<
/
script
>
<
script
type
=
"text/javascript"
>
var InterValObj;
/
/
timer變量,控制時間
var count
=
60
;
/
/
間隔函數,
1
秒執行
var curCount;
/
/
當前剩餘秒數
function codeRandom
(
chars
)
{
var res
=
""
;
for
(
var i
=
0
;i
<
chars;i
+
+
)
{
res
+
=
Math.floor
(
Math.random
(
)
*
10
)
;
}
return
res;
}
/
/
timer處理函數
function SetRemainTime
(
)
{
if
(
curCount
=
=
0
)
{
window
.clearInterval
(
InterValObj
)
;
/
/
停止計時器
$
(
"#sub"
)
.removeAttr
(
"disabled"
)
;
/
/
啓用按鈕
$
(
"#sub"
)
.val
(
"重新發送驗證碼"
)
;
code
=
""
;
/
/
清除驗證碼。如果不清除,過時間後,輸入收到的驗證碼依然有效
}
else
{
curCount
--;
$
(
"#sub"
)
.val
(
"請在"
+
curCount
+
"秒內輸入驗證碼"
)
;
}
}
/
/
獲取手機號碼
$
(
function
(
)
{
$
(
"#sub"
)
.click
(
function
(
)
{
var
phone
=
$
(
"#phone"
)
.val
(
)
;
/
/
獲取手機號碼
curCount
=
count;
if
(
phone
!
=
''
)
{
/
/
驗證手機有效性
var myreg
=
/
^
(
(
(
13
[
0
-9
]
{
1
}
)
|
(
15
[
0
-9
]
{
1
}
)
|
(
18
[
0
-9
]
{
1
}
)
)
+
\d
{
8
}
)
$
/
;
if
(
!myreg.test
(
$
(
'
#phone').val()))
{
alert
(
'請輸入有效的手機號碼!'
)
;
return
false
;
}
phone
=
$
(
'
#phone').val();
/
/
驗證碼
(
隨機生成
)
var code
=
codeRandom
(
4
)
;
/
/
設置
button
效果,開始計時
$
(
"#sub"
)
.attr
(
"disabled"
,
"true"
)
;
$
(
"#sub"
)
.val
(
"請在"
+
curCount
+
"秒內輸入驗證碼"
)
;
InterValObj
=
|