지난 2 주 동안 나는 쿠키에 페이지 아이디를 저장하고 다른 페이지에서 그것을 검색하고있었습니다.PHP로 저장된 변수 사용 PHP
마지막으로 나는 그것을 풀었지만 이제는이 PHP 코드에서이 이드 (쿠키에 저장하고 가져온 것)를 사용하려고하는 다른 문제가 있습니다.
JavaScript가 클라이언트 측 코드이고 PHP가 서버 측 코드라는 것을 알고 있지만이 작업을 수행해야합니다. 이걸 도와주세요.
이 큰 노력하고 있습니다 내 자바 스크립트 코드이며 이미 쿠키에 저장 관리하는 경우, 나는이 라인 "+ value.favoriteid +"
<script>
/*
* Create cookie with name and value.
* In your case the value will be a json array.
*/
function createCookie(name, value, days) {
var expires = '',
date = new Date();
if (days) {
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = '; expires=' + date.toGMTString();
}
document.cookie = name + '=' + value + expires + '; path=/';
}
/*
* Read cookie by name.
* In your case the return value will be a json array with list of pages saved.
*/
function readCookie(name) {
var nameEQ = name + '=',
allCookies = document.cookie.split(';'),
i,
cookie;
for (i = 0; i < allCookies.length; i += 1) {
cookie = allCookies[i];
while (cookie.charAt(0) === ' ') {
cookie = cookie.substring(1, cookie.length);
}
if (cookie.indexOf(nameEQ) === 0) {
return cookie.substring(nameEQ.length, cookie.length);
}
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
var faves = new Array();
$(function(){
var favID;
var query = window.location.search.substring(1);
\t var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
var favID = (pair[0]=='ID' ? pair[1] :1)
//alert(favID);
\t }
\t $(document.body).on('click','#addTofav',function(){
var fav = {'favoriteid':favID};
faves.push(fav);
\t var stringified = JSON.stringify(faves);
createCookie('favespages', stringified);
location.reload();
\t });
var myfaves = JSON.parse(readCookie('favespages'));
if(myfaves){
faves = myfaves;
} else {
faves = new Array();
}
$.each(myfaves,function(index,value){
var element = '<li class="'+index+'"><h4>'+value.favoriteid+'</h4> ';
$('#appendfavs').append(element);
});
});
</script>
시도'인 print_r ($ _ COOKIE)는'또한 http://php.net/manual/en/features.cookies.php 살펴 –
쿠키는 클라이언트에 저장되고 서버로 발송에 실패하는이 . 그래서 자바 스크립트와 PHP에서 동일한 쿠키로 재생할 수 있지만 서버와 클라이언트를 구별 할 필요가 있습니다. –