1
2
3
4
5
6
|
setcookie(
"user"
,
"AlexPorter"
,time()+3600);
?>
.....
|
1
2
3
4
5
6
7
|
//Printacookie
echo
$_COOKIE
[
"user"
];
//Awaytoviewallcookies
print_r(
$_COOKIE
);
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
|
if
(isset(
$_COOKIE
[
"user"
]))
echo
"Welcome"
.
$_COOKIE
[
"user"
].
"!
;
else
echo
"Welcomeguest!
;
?>
|
1
2
3
4
|
//settheexpirationdatetoonehourago
setcookie(
"user"
,
""
,time()-3600);
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
functionsetCookie(c_name,value,expiredays)
{
varexdate=newDate()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+
"="
+escape(value)+
((expiredays==
null
)?
""
:
";expires="
+exdate.toGMTString())
}
|
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
|
functiongetCookie(c_name)
{
if
(document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name+
"="
)
if
(c_start!=-1)
{
c_start=c_start+c_name.length+1
c_end=document.cookie.indexOf(
";"
,c_start)
if
(c_end==-1)
c_end=document.cookie.length
returnunescape(document.cookie.substring(c_start,c_end))
}
}
return
""
}
|
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
|
functioncheckCookie()
{
username=getCookie(
'username'
)
if
(username!=
null
&&username!=
""
)
{alert(
'Welcomeagain'
+username+
'!'
)}
else
{
username=prompt(
'Pleaseenteryourname:'
,
""
)
if
(username!=
null
&&username!=
""
)
{
setCookie(
'username'
,username,365)
}
}
}
|
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
|
functiongetCookie(c_name)
{
if
(document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name+
"="
)
if
(c_start!=-1)
{
c_start=c_start+c_name.length+1
c_end=document.cookie.indexOf(
";"
,c_start)
if
(c_end==-1)
c_end=document.cookie.length
returnunescape(document.cookie.substring(c_start,c_end))
}
}
return
""
}
functionsetCookie(c_name,value,expiredays)
{
varexdate=newDate()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+
"="
+escape(value)+
((expiredays==
null
)?
""
:
";expires="
+exdate.toGMTString())
}
functioncheckCookie()
{
username=getCookie(
'username'
)
if
(username!=
null
&&username!=
""
)
{alert(
'Welcomeagain'
+username+
'!'
)}
else
{
username=prompt(
'Pleaseenteryourname:'
,
""
)
if
(username!=
null
&&username!=
""
)
{
setCookie(
'username'
,username,365)
}
}
}
|