setcookie()
在PHP中使用该函数。此函数接受以下参数。
1
2
3
4
5
6
7
8
9
10
11
12 |
function wpb_cookies_tutorial1() { $visit_time = date ( 'F j, Y g:i a' ); if (!isset( $_COOKIE [ $wpb_visit_time ])) { // set a cookie for 1 year setcookie( 'wpb_visit_time' , $current_time , time()+31556926); } } |
wpb_visit_time
。
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 |
function wpb_cookies_tutorial2() { // Time of user's visit $visit_time = date ( 'F j, Y g:i a' ); // Check if cookie is already set if (isset( $_COOKIE [ 'wpb_visit_time' ])) { // Do this if cookie is set function visitor_greeting() { // Use information stored in the cookie $lastvisit = $_COOKIE [ 'wpb_visit_time' ]; $string .= 'You last visited our website ' . $lastvisit . '. Check out whats new' ; return $string ; } } else { // Do this if the cookie doesn't exist function visitor_greeting() { $string .= 'New here? Check out these resources...' ; return $string ; } // Set the cookie setcookie( 'wpb_visit_time' , $visit_time , time()+31556926); } // Add a shortcode add_shortcode( 'greet_me' , 'visitor_greeting' ); } add_action( 'init' , 'wpb_cookies_tutorial2' ); |
1 |
unset( $_COOKIE [ 'wpb_visit_time' ]); |
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 |
function wpb_cookies_tutorial2() { // Time of user's visit $visit_time = date ( 'F j, Y g:i a' ); // Check if cookie is already set if (isset( $_COOKIE [ 'wpb_visit_time' ])) { // Do this if cookie is set function visitor_greeting() { // Use information stored in the cookie $lastvisit = $_COOKIE [ 'wpb_visit_time' ]; $string .= 'You last visited our website ' . $lastvisit . '. Check out whats new' ; // Delete the old cookie so that we can set it again with updated time unset( $_COOKIE [ 'wpb_visit_time' ]); return $string ; } } else { // Do this if the cookie doesn't exist function visitor_greeting() { $string .= 'New here? Check out these resources...' ; return $string ; } } add_shortcode( 'greet_me' , 'visitor_greeting' ); // Set or Reset the cookie setcookie( 'wpb_visit_time' , $visit_time , time()+31556926); } add_action( 'init' , 'wpb_cookies_tutorial2' ); |