CracKerMe / CracKerMe/dev_code
第九点关于cookie 的写法有缺陷
- Dominant language
- JavaScript
- Stars
- 12
- Forks
- 1
- PR merge metrics
- No merged PRs in 30d
Description
```
var setCookie = {
set: function(name, value) {
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
}
```
#### 这种方式 在已存在 cookie键值对的情况下,只是 新增 一组cookie。并没有实现 setCookie 的功能。
建议改进为
```
/**
* 设置cookie
* @param {string} name 键名
* @param {string} value 键值
* @param {integer} days cookie周期
*/
function setCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}else{
var expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
// 获取cookie
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
// 删除cookie
function deleteCookie(name) {
setCookie(name,"",-1);
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by locating the setCookie implementation shown in the issue and inspect how it handles an existing cookie key. Compare its behavior with the proposed setCookie, getCookie, and deleteCookie examples; done means setting an existing key updates it and the related cookie operations work as described.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100