The question we present to you today is about
- < ? PHP
- Setcookie ("Cookieid", $USERID);
- ?>
- < HTML>
- < BODY>
- < /body >
- < /html >
PHP reads the Setcookie function of the cookie in a total of six parameters, separated by commas:
The name of the cookie, which is a string, for example: "Cookieid". colons, commas, and spaces are not allowed during this period. This parameter is required, and all other parameters are optional. If only this parameter is given, then the cookie will be deleted.
The value of a cookie, usually a string variable, for example: $USERID. You can also assign one to it?? To skip the value setting.
The time the cookie expires. If omitted (or assigned a value of 0), the cookie will expire at the end of the conversation session. This parameter can be an absolute time, expressed in Dd-mon-yy HH:MM:SS, for example: "24-nov-99 08:26:00". The more common is to set a relative time. This is done through the time () function or the Mktime function. For example, time () +3600 will invalidate the cookie after one hours.
A path that is used to match a cookie. This parameter is used to avoid confusion when there is more than one set of cookies with the same name on a server. The effect of using the "/" path and omitting this parameter is the same. Note that Netscape's cookie definition is to place the domain name in front of the path, while PHP is the opposite.
The domain name of the server is also used to match cookies. Note that you must place a dot before the server's domain name (. )。 For example: ". friendshipcenter.com". Because unless there are more than two points in existence, this parameter is not acceptable.
The security level of a cookie is an integer. 1 means that the cookie can only be transmitted through a "secure" network. 0 or omitted means that any type of network can be 117-102 117-202 117-301.
Cookies and variables
When a PHP script extracts a cookie from the client's browser, it automatically converts it to a variable. For example, a cookie named Cookieid will become a variable $CookieID.
The contents of the cookie are reported to exist in the Http_cookie_vars array, and you can also access the specified cookie value through the name of the array and cookie:
Print $HTTP _cookie_vars[cookieid];
Remember every user
Looking back at the submitform.php3 file above, it's the role of adding the customer's name to the database, and now I want to add something to it. I want to assign a unique user logo to each user, and then put this flag in the cookie so that whenever a user visits my website, through a cookie and the user's logo, I can know who he is.
MySQL can be set to automatically assign a number to each new record, which starts from 1 and automatically adds 1 each time later. With a single line of SQL statements, you can easily add a field like this to a data table, which I call
Userid:
ALTER TABLE dbname
ADD COLUMN
USERID INT (one) not NULL
PRIMARY KEY auto_increment;
We have made some special settings for this field. First, the integer whose type is 11-bit is defined by "INT (11)", then the value of the field is not null with the "NOT NULL" keyword, and the "PRIMARY key" is used to set it as an indexed field, which makes the search faster; Finally, "Auto_increme NT "Defines a field that is automatically added to one.
When you insert a user's name into the database, you should set a cookie on their browser. This is the value of the USERID field we talked about just now:
- < ? PHP
- mysql_connect (localhost, username, password);
- mysql_select_db (dbname);
- mysql_query ("INSERT into TableName (first_name, last_name) VALUES (" $first _name "," $last _name ")");
- Setcookie ("Cookieid",
- MYSQL_INSERT_ID (),
- Time () +94608000,
- "/"); /* Cookies will not expire after three years * *
- ?>
The PHP function mysql_insert_id () returns the value of the field defined by Auto_increment after the last insert query was executed. This way, as long as you do not clear the browser's Cookies, the site will always "remember" you
PHP Read cookies
Let's write a script like Amazon.com. First, the PHP script checks whether the client's browser has sent a cookie, and if so, the user's name will be displayed. If the cookie is not found, a form is displayed, the customer registers their name, then adds it to the database, and sets the cookie in the client's browser.
First, let's show the contents of the cookie:
- < ? PHP
- Print $CookieID;
- ?>
Then, you can display the name:
- < ? PHP
- mysql_connect (localhost, username, password);
- mysql_select_db (dbname);
- $ Selectresult mysql_queryUSERID"$CookieID");
- $ Row mysql_fetch_array($selectresult);
- echo "Welcome your presence", $row [first_name], "!";
- ?>
The above is the implementation of PHP read the specific method of cookies.
http://www.bkjia.com/PHPjc/446055.html www.bkjia.com true http://www.bkjia.com/PHPjc/446055.html techarticle the question we are introducing to you today is about? PHP Setcookie (Cookieid, $USERID);? HTML body/body/html PHP Read the Setcookie function of the cookie a total of six parameters, separated by commas: ...