How to Create a Cookie on <a href> tag using PHP?

Discussion in 'PHP' started by FR3@K, Oct 3, 2007.

  1. hemlata

    hemlata Peon

    Messages:
    18
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #21
    Hello,

    There were lots of arguments held with this ticket. Here is the simple solution for FR3@K

    function createCookie(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=/";
    	alert('Cookie set with value :: ' + value);
    }
    
    function readCookie(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)
    		{
    			var val = c.substring(nameEQ.length,c.length);
    			alert('Reading cookie as :: ' + val);
    			return val;
    		}
    	}
    	return null;
    }
    
    function eraseCookie(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=/";
    	alert('Cookie Deleted successfully.');
    }
    
    
    </script>
    <body>
    <a href="#" onClick = "createCookie('wide1', 'Medium', 'time()-3600')">Create cookie 1</a><br>
    <a href="#" onclick="readCookie('wide1')" class="page">Read cookie 1</a><br>
    <a href="#" onclick="eraseCookie('wide1', '','-1')" class="page">Delete cookie 1</a>
    PHP:
    Hope this will solve your as well as other forum member's issues..:D
    Regards
     
    hemlata, Oct 4, 2007 IP
  2. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #22
    i really appreciate that you tried to solve my problem ,
    but this code is still not the thing that i am trying to do.
    I need the cookie value to be retrieved like this:
    <body id="page" if cookie value = normal , echo class="cookie value" else echo class="font-small width-fluid">
     
    FR3@K, Oct 4, 2007 IP
  3. FR3@K

    FR3@K Banned

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #23
    ok this thread can be closed, i got a js script from joesgraphics , thanks to him , he is really helpful , helped me a lot , for about 2 hours on msn.
    +rep to him :D
     
    FR3@K, Oct 5, 2007 IP