Various actions for cookies

Source: Internet
Author: User
Tags php tutorial setcookie

Creating and storing cookies
In this example we want to create a cookie that stores the visitor's name. When visitors visit the site for the first time, they are asked to fill in their names. The name is stored in the cookie. When visitors visit the site again, they receive a welcome word.

First, we'll create a function that stores the visitor's name in the cookie variable:

function Setcookie (c_name,value,expiredays)
{
var exdate=new Date ()
Exdate.setdate (Exdate.getdate () +expiredays)
document.cookie=c_name+ "=" +escape (value) +
((expiredays==null)? "": "Expires=" +exdate.togmtstring ())
}

The parameters in the above function hold the name, value, and expiration days of the cookie.

In the above function, we first convert the number of days to a valid date, and then we save the cookie name, value, and its expiration date to the Document.cookie object.

After that, we'll create another function to check if the cookie is set:

Function GetCookie (c_name)
{
if (document.cookie.length>0)
  {
  C_start=d Ocument.cookie.indexOf (c_name + "=")
  if (c_start!=-1)
    {
    c_ Start=c_start + c_name.length+1
    c_end=document.cookie.indexof (";", C_start)
     if (c_end==-1) c_end=document.cookie.length
    return unescape (document.cookie.substring ( c_start,c_end))
   }
 }
Return "
}

<?php Tutorial
Setcookie ("Cookie[one]", "Cookieone", Time () +36000, "/");
Setcookie ("Cookie[two]", "Cookietwo", Time () +36000, "/");
Setcookie ("Cookie[three]", "Cookiethree", Time () +36000, "/", ". 65.la");
Page again
if (Isset ($_cookie["COOKIE"]) {
foreach ($_cookie["COOKIE"] as $name => $value) {
$name = Htmlspecialchars ($name);
$value = Htmlspecialchars ($value);
echo "$name: $value <br/>n";
}
}

?>


Today, JS to the cookie operating system, including: JS read cookie,js add cookie,js delete cookies, examples are as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>cookie processing functions (written for me, not what I think: improving object-oriented) </title>
<script language= "JavaScript" type= "text/Web Effects" >
function Addcookie (objname,objvalue,objhours) {//Add cookies
var str = objname + "=" + Escape (ObjValue);
if (objhours > 0) {//0 does not set the expiration time, the cookie automatically disappears when the browser shuts down
var date = new Date ();
var ms = objhours*3600*1000;
Date.settime (Date.gettime () + ms);
str + = "; Expires= "+ date.togmtstring ();
}
Document.cookie = str;
Alert ("Add cookie succeeded");
}

function GetCookie (objname) {//Get the value of the cookie for the specified name
var arrstr = Document.cookie.split (";");
for (var i = 0;i < Arrstr.length;i + +) {
var temp = arrstr[i].split ("=");
if (temp[0] = = objname) return unescape (temp[1));
}
}

function Delcookie (name) {//To delete a cookie with the specified name, you can set its expiration time to a past time
var date = new Date ();
Date.settime (Date.gettime ()-10000);
Document.cookie = name + "=A;" Expires= "+ date.togmtstring ();
}
read out all the cookie words? string up
function Allcookie () {//Read all saved cookie strings
var str = document.cookie;
if (str = = "") {
str = "Do not save any cookies";
}
alert (str);
}

function $ (m,n) {
return document.forms[m].elements[n].value;
}

function add_ () {
var cookie_name = $ ("MyForm", "cookie_name");
var Cookie_value = $ ("MyForm", "Cookie_value");
var cookie_expirehours = $ ("MyForm", "cookie_expireshours");
Addcookie (cookie_name,cookie_value,cookie_expirehours);
}

function get_ () {
var cookie_name = $ ("MyForm", "cookie_name");
var cookie_value = GetCookie (cookie_name);
alert (Cookie_value);
}

function Del_ () {
var cookie_name = $ ("MyForm", "cookie_name");
Delcookie (Cookie_name);
Alert ("Delete succeeded");
}
</script>

//Add cookie
Function Addcookie (name,value,expires,path,domain) {
 var str=name+ ' = ' +escape (value);
 if (expires!= "") {
  var date=new date ();
  Date.settime (Date.gettime () +expires*24*3600*1000) ;//expires units are days
  str+= "; expires=" +date.togmtstring ();
 }
 if (path!= "") {
  str+= ;p ath= "+path;//Specify the directory for accessible Cookies
 
 if (domain!=" ") {
  str+=";d omain= "+domain;// Specifies the domain
 }
 document.cookie=str for which the cookie can be accessed;
}
//Get cookie
Function GetCookie (name) {
 var str=document.cookie.split (";")
 for (var i=0;i<str.length;i++) {
 var str2=str[i].split ("=");
  if (str2[0]==name) Return unescape (str2[1]);
 }
}
//delete cookie
function Delcookie (name) {
 var date=new date ();
 date.settime (date.gettime ()-10000);
 document.cookie=name+ "=n;expire=" +date.togmtstring ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.