Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the syntax of setcookie function and how to apply it

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what the setcookie function syntax is and how to apply it. The content of the article is of high quality, so Xiaobian shares it with you for reference. I hope you have a certain understanding of relevant knowledge after reading this article.

Cookies are pieces of information that are generated by a web server and exist on the client side. It is embedded in html messages, specified by the server, and passes information between the client and server. It is commonly used for: user personalization of web pages, counters, storage of information about visited sites, etc. What is the difference between the two? How does the setcookie function work?

What is the setcookie function syntax?

setcookie(stringCookieName,stringCookieValue,intCookieExpireTime,path,domain,intsecure);

PATH: indicates the directory on the web server, default is the directory where the page is called

DOMAIN: The domain name that cookies can use, default to the domain name of the page being called. This domain name must contain two ". ", so if you specify your top-level domain you must use". mydomain.com "

SECURE: If set to "1," cookies can only be remembered by servers that the user's browser considers secure.

How to use the setcookie function

For a site that needs to register, it will automatically identify the user's identity and send it information. If it is a stranger, it will tell him to register first. We create a small database with the following information: first name, last name, email address, visitcounter.

Follow these steps to create a table:

mysql>createdatabaseusers;

QueryOK,1rowaffected(0.06sec)

mysql>useusers;

Databasechanged

mysql>createtableinfo(FirstNamevarchar(20),LastNamevarchar(40),

emailvarchar(40),countvarchar(3));

QueryOK,0rowsaffected(0.05sec)

Well, now that we have the table we want, we can build a php page to check cookies against the database.

########################index.php##################################

$info=explode("&",$Example);

$FirstName=$info[0];

$LastName=$info[1];

$email=$info[2];

$count=$info[3];

$count++;

$CookieString=$FirstName. '&'.$ LastName. '&'.$ email. '&'.$ count;

SetCookie("Example",$CookieString,time()+3600);//Set a new cookie

echo"

Hello$FirstName$LastName,thisisyourvisitnumber:$count

Youremailaddressis:$email

";

mysql_connect()ordie("ProblemconnectingtoDataBase");//updateDB

$query="updateinfosetcount=$countwhereFirstName='$FirstName'and

LastName='$LastName'andemail='$email'";

$result=mysql_db_query("users",$query)ordie("Problems.... ");

}//EndExistingcookieinstructions

else{//BegininctructionsfornoCookie

echo"

ClickHereforSiteRegistration

";

}//EndNoCookieinstructions

?>

Note: If you are using a remote mysql server or unix server, you should use the following statement

mysql_connect("server","username","password")ordie("ProblemconnectingtoDataBase");

We want to check if a cookie with the specified name is sent in the html header. Remember, php can convert recognized cookies into corresponding variables, so we can check a variable named "Example":

...

}else{

...

}

If this cookie exists, we increment the counter by one and print user information. If this cookie does not exist, we recommend that users register first.

If cookies exist, we perform the following steps:

$info=explode("&",$Example);//splitthestringtovariables

$FirstName=$info[0];

$LastName=$info[1];

$email=$info[2];

$count=$info[3];

$count++;

$CookieString=$FirstName. '&'.$ LastName. '&'.$ email. '&'.$ count;

SetCookie("Example",$CookieString,time()+3600);//settinganewcookie

echo"

Hello$FirstName$LastName,thisisyourvisitnumber:$count

Youremailaddressis:$email

";

mysql_connect()ordie("ProblemconnectingtoDataBase");//updateDB

$query="updateinfosetcount=$countwhereFirstName='$FirstName'and

LastName='$LastName'andemail='$email'";

$result=mysql_db_query("users",$query)ordie("Problems.... ");

}//EndExistingcookieinstructions

The above program has three main parts: first get the cookie value, use the explode function to divide it into different variables, increment the counter, and set a new cookie. Then output the user information in html statements. Finally, update the database with the new counter values.

If the cookie does not exist, the following procedure will be executed:

else{//BegininctructionsfornoCookie

echo

ClickHereforSiteRegistration

";

}//EndNoCookieinstructions

About setcookie function syntax is what and how to apply to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report