<?php
setcookie("name", "Alex Porter", time()+3600);
?>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<body>
<!--<?php echo $_COOKIE["name"];?>-->
<script type="text/javascript">
function getCookie(name)
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]);
return null;
}
if(!getCookie("name"))
{
alert('失败');
}else{
alert(getCookie("name"));
}
</script>
</body>
</html>
本文通过一个简单的示例介绍了如何使用PHP设置一个名为'name'的Cookie,并利用JavaScript来读取这个Cookie的值。示例中展示了设置Cookie的有效期为一小时,并检查了Cookie是否设置成功。

433

被折叠的 条评论
为什么被折叠?



