Computing.Net > Forums > Web Development > Cookie Creation

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Cookie Creation

Reply to Message Icon

Name: Inj3cti0n
Date: September 28, 2009 at 10:18:46 Pacific
OS: Windows 7 Ultimate
CPU/Ram: 3.192 GHz / 3070 MB
Product: Dell / Dell dxp051
Subcategory: General
Comment:

Hey there people, me again. Anyways,


I have a website:
http://inj3cti0n.is-a-geek.com

that I want to make a cookie for, but I have NO IDEA HOW.

Anyways, I want to put up a index.html, other then the one that is there now. It containing what ever I want. On that page, I am going to put Accept / Decline and when the link "Accept" is clicked they are then taken to the actual site contents, when they leave the site and come back, they don't see that one page with the Accpet/Decline.

Any ideas?



Sponsored Link
Ads by Google

Response Number 1
Name: shutat
Date: October 3, 2009 at 01:35:48 Pacific
Reply:

You've got a few choices - with straight html, you can set a cookie using the meta header; however, to process it, you'd need scripting on either the client or server side.

One thing to be aware of is user's may have cookie support disabled, so there may be a chance that any of the methods will not work.

html alone

<head>
<meta http-equiv="Set-Cookie" content="name=some_value;expires=some_date; path=web_site_path">
</head>

You can also use client side scripting such as javascript. The down side is your visitors may not have it enabled.

<script type="text/javascript">

function setc(cdata, ndays) {

   /* cdata: what to store in the cookie
      ndays: how many days to keep cookie */

   var d = new Date();
   d.setDate(d.getDate() + ndays);
   document.cookie = "some_name=" + escape(cdata) +
   ";expires=" + d.toGMTString();   

   location.reload();
} 

function getc() {

   if(document.cookie.indexOf("some_name") == -1) {
      var atag = document.createElement("a");

      atag.id = "someid";
      atag.href = "javascript:setc('test data', 1);"; // 1 day cookie
      atag.innerHTML = "accept"; // click text to display

      document.body.appendChild(atag);
   }
}
</script>
<noscript>this site requires javascript, but it's unsupported or disabled</noscript>

<body onload="getc()">

Server side scripting is a bit different in how it's set up, but it doesn't depend on the user having scripting enabled in his or her browser. A php example

<?php

   if(isset($_GET["m"]) && $_GET["m"] === "accept") { // ignore queries other than m=accept

      setcookie("some_name", "test data", time() + 3600 * 24); // one day cookie
      header("location: " . $_SERVER["PHP_SELF"]); // strip off query string

   } else if(isset($_COOKIE["some_name"]) && $_COOKIE["some_name"] === "test data") {

      // serve actual contents

   } else {

      // serve notice
 
      echo "<a href='" . $_SERVER["PHP_SELF"] . "?m=accept'>accept</a>";
   }
?>

HTH


0
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Cookie Creation

Dam PHP! www.computing.net/answers/webdevel/dam-php/752.html

Cookie Problems (I think) www.computing.net/answers/webdevel/cookie-problems-i-think/1247.html

Cookie Client Side www.computing.net/answers/webdevel/cookie-client-side/1150.html