Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hey there people, me again. Anyways,
I have a website:
http://inj3cti0n.is-a-geek.comthat 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?

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

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |