本文實例講述了PHP+redis實現添加處理投票的方法。分享給大家供大家參考,具體如下:
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
|
<?php header( "Content-Type:text/html;charset=utf-8" ); include 'lib/mysql.class.php' ; $mysql_obj = mysql::getConn(); if ( class_exists ( 'Redis' )){ //redis $redis = new Redis(); $redis ->pconnect( '127.0.0.1' , 6379); if (isset( $_SERVER [ 'HTTP_REFERER' ])){ $url_md5 = md5( $_SERVER [ 'HTTP_REFERER' ]); } $adve_key = 'adve' ; $adve_key_exists = 'adve_exists' ; if (! $redis ->exists( $adve_key_exists )){ $list = $mysql_obj ->fetch_array( "select * from admin_online_adve" ); if ( $list ){ foreach ( $list as $key => $value ) { $url_hash = md5( $value [ 'adve_url' ]); $adve_hash_key = $adve_key . ":" . $url_hash ; $id = $value [ 'id' ]; $redis ->set( $adve_hash_key , $id ); $redis ->set( $adve_key_exists ,true); } } } $adve_new_key = $adve_key . ':' . $url_md5 ; if ( $redis ->exists( $adve_new_key )){ $adve_plus = $adve_new_key . ":plus" ; if (! $redis ->exists( $adve_plus )){ $redis ->set( $adve_plus ,1); } else { $redis ->incr( $adve_plus ); $num = $redis ->get( $adve_plus ); if ( $num >100){ $id = $redis ->get( $adve_new_key ); // insert to sql; $mysql_obj ->query( "update admin_online_adve set adve_num=adve_num+$num where id=$id" ); $redis ->set( $adve_plus ,1); } } } } ?> <html> <head> <meta http-equiv= "refresh" content= "1;url=https://itunes.apple.com/cn/app/san-guo-zhi15-ba-wangno-da-lu/id694974270?mt=8" > <title>統計</title> </head> <body> <img src= "loading.gif" >Loading... </body> </html> |
其中php連接mysql類mysql.class.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
<?php define( "MYSQL_SQL_GETDATA" , 1); define( "MYSQL_SQL_EXECUTE" , 2); class mysql_db{ var $_server ; //數據庫服務器地址 var $_user ; //數據庫連接帳號 var $_password ; //數據庫連接密碼 var $_dbname ; //數據庫名稱 var $_persistency =false; //是否使用持久連接 var $_isConnect = false; //是否已經建立數據庫連接 var $_charset = "utf8" ; //數據庫連接字符集 var $_isDebug = false; //是否Debug模式 var $_sql = array (); //執行sql語句數組 var $_db_connect_id ; //數據庫連接對象標識 var $_result ; //執行查詢返回的值 var $_record ; var $_rowset ; var $_errno = 0; var $_error = "connection error" ; var $_checkDB = false; function mysql_db( $dbserver , $dbuser , $dbpassword , $database , $persistency = false, $autoConnect =false, $checkdb = false) { $this ->_server = $dbserver ; $this ->_user = $dbuser ; $this ->_password = $dbpassword ; $this ->_dbname = $database ; $this ->_persistency = $persistency ; $this ->_autoConnect = $autoConnect ; $this ->_checkDB = $checkdb ; if ( $autoConnect ){ $this ->connection(); } } function connection( $newLink = false) { if (! $newLink ){ if ( $this ->_isConnect && isset( $this ->_db_connect_id)){ @mysql_close( $this ->_db_connect_id); } } $this ->_db_connect_id = ( $this ->persistency) ? @mysql_pconnect( $this ->_server, $this ->_user, $this ->_password):@mysql_connect( $this ->_server, $this ->_user, $this ->_password, $newLink ); if ( $this ->_db_connect_id) { if ( $this ->version() > '4.1' ) { if ( $this ->_charset != "" ) { @mysql_query( "SET NAMES '" . str_replace ('- ', ' ', $this->_charset)."' ", $this ->_db_connect_id); } } if ( $this ->version() > '5.0' ) { @mysql_query( "SET sql_mode=''" , $this ->_db_connect_id); } //檢測指定數據庫是否連接成功 if ( $this ->_checkDB){ $dbname = mysql_query( 'SELECT database()' , $this ->_db_connect_id); $dbname = mysql_fetch_array( $dbname ,MYSQL_NUM); $dbname = trim( $dbname [0]); } else { $dbname = '' ; } if ( $dbname == $this ->_dbname || $dbname == '' ){ if (!@mysql_select_db( $this ->_dbname, $this ->_db_connect_id)) { @mysql_close( $this ->_db_connect_id); $this ->_halt( "cannot use database " . $this ->_dbname); } } else { if ( $this ->_checkDB && ! $newLink ){ $this ->connection(true); } } return true; } else { $this ->_halt( 'connect failed.' ,false); } } function setCharset( $charset ){ //$charset = str_replace('-', '', $charset); $this ->_charset = $charset ; } function setDebug( $isDebug =true){ $this ->_isDebug = $isDebug ; } function query( $sql , $type = '' ) { return $this ->_runSQL( $sql ,MYSQL_SQL_GETDATA, $type ); } function execute( $sql ) { return $this ->_runSQL( $sql ,MYSQL_SQL_EXECUTE, "UNBUFFERED" ); } function _runSQL( $sql , $sqlType =MYSQL_SQL_GETDATA, $type = '' ) { if ( $type == "UNBUFFERED" ){ $this ->_result = @mysql_unbuffered_query( $sql , $this ->_db_connect_id); } else { $this ->_result = @mysql_query( $sql , $this ->_db_connect_id); } //測試模式下保存執行的sql語句 if ( $this ->_isDebug){ $this ->_sql[]= $sql ; } if ( $this ->_result) { return $sqlType ==MYSQL_SQL_GETDATA? $this ->getNumRows(): $this ->getAffectedRows(); } else { $this ->_halt( "Invalid SQL: " . $sql ); return false; } } function next( $result_type =MYSQL_ASSOC) { $this ->fetchRow( $result_type ); return is_array ( $this ->_record); } function f( $name ) { if ( is_array ( $this ->_record)){ return $this ->_record[ $name ]; } else { return false; } } function fetchRow( $result_type =MYSQL_ASSOC) { if ( $this ->_result ) { $this ->_record = @mysql_fetch_array( $this ->_result, $result_type ); return $this ->_record; } else { return false; } } function getAll( $sql , $primaryKey = "" , $result_type =MYSQL_ASSOC) { if ( $this ->_runSQL( $sql ,MYSQL_SQL_GETDATA)>=0){ return $this ->fetchAll( $primaryKey , $result_type ); } else { return false; } } function getOne( $sql , $result_type =MYSQL_ASSOC) { if ( $this ->_runSQL( $sql ,MYSQL_SQL_GETDATA)>0){ $arr = $this ->fetchAll( "" , $result_type ); if ( is_array ( $arr )){ return $arr [0]; } } else { return false; } } function fetchAll( $primaryKey = "" , $result_type =MYSQL_ASSOC) { if ( $this ->_result) { $i = 0; $this ->_rowset = array (); if ( $primaryKey == "" ) { while ( $this ->next( $result_type )) { $this ->_rowset[ $i ] = $this ->_record; $i ++; } } else { while ( $this ->next( $result_type )) { $this ->_rowset[ $this ->f( $primaryKey )] = $this ->_record; $i ++; } } return $this ->_rowset; } else { //$this->_halt("Invalid Result"); return false; } } function checkExist( $sql ) { return $this ->query( $sql )>0?true:false; } function getValue( $sql , $colset = 0) { if ( $this ->query( $sql )>0){ $this ->next(MYSQL_BOTH); return $this ->f( $colset ); } else { return false; } } function getNumRows() { return @mysql_num_rows( $this ->_result); } function getNumFields() { return @mysql_num_fields( $this ->_result); } function getFiledName( $offset ) { return @mysql_field_name( $this ->_result, $offset ); } function getFiledType( $offset ) { return @mysql_field_type( $this ->_result, $offset ); } function getFiledLen( $offset ) { return @mysql_field_len( $this ->_result, $offset ); } function getInsertId() { return @mysql_insert_id( $this ->_db_connect_id); } function getAffectedRows() { return @mysql_affected_rows( $this ->_db_connect_id); } function free_result() { $ret = @mysql_free_result( $this ->_result); $this ->_result = 0; return $ret ; } function version() { return @mysql_get_server_info( $this ->_db_connect_id); } function close() { return @mysql_close( $this ->_db_connect_id); } function sqlOutput( $isOut = true, $all = true){ if ( $all ){ $ret = implode( "<br>" , $this ->_sql); } else { $ret = $this ->_sql[ count ( $this ->_sql)-1]; } if ( $isOut ){ echo $ret ; } else { return $ret ; } } function _halt( $msg = "Session halted." , $getErr =true) { if ( $this ->_isDebug){ if ( $getErr ){ $this ->_errno = @mysql_errno( $this ->_db_connect_id); $this ->_error = @mysql_error( $this ->_db_connect_id); printf( "<b>MySQL _error</b>: %s (%s)<br></font>/n" , $this ->_errno, $this ->_error); } die ( $msg ); } else { die ( "Session halted." ); } } } ?> |
希望本文所述對大家PHP程序設計有所幫助。