- SetCookie ("Cookie", "cookievalue", time () + 3600, "/forum", ".jbxue.com", 1 );
1) CookiePHP supports Cookie receiving and processing very well and is completely automatic. it is as simple as the FORM variable principle. For example, if you set a Cookie named MyCookier, PHP will automatically analyze it from the HTTP header received by the WEB server and form a variable named $ myCookie, which is the same as a common variable, the value of this variable is the Cookie value. Arrays also apply. Another method is to reference the global variable $ HTTP_COOKIE_VARS array of PHP. Examples are as follows: (assuming these are all set in the previous page and still valid)
- Echo $ MyCookie;
- Echo $ CookieArray [0];
- Echo $ _ COOKIE ["MyCookie"];
- Echo $ HTTP_COOKIE_VARS ["MyCookie"];
- ?>
2) There are two ways to delete an existing Cookie:
- 1. SetCookie ("Cookie ","");
- 2. SetCookie ("Cookie", "value", time ()-1/time ());
3) restrictions on the use of cookies 1. it must be set before the HTML file content is output; 2. inconsistent processing of cookies by different browsers and sometimes incorrect results may occur. 3. restrictions are imposed on the client. A browser can create a maximum of 30 cookies, each of which cannot exceed 4 kB. each WEB site can set a maximum of 20 cookies.
3,Session configuration and application
- Session_start (); // initialize the session. it must be in the file header.
- $ _ SESSION [name] = value; // Configure Seeeion
- Echo $ _ SESSION [name]; // use session
- Isset ($ _ SESSION [name]); // judge
- Unset ($ _ SESSION [name]); // delete
- Session_destroy (); // consume all sessions
- ?>
Note: session_register (), session_unregister, and session_is_registered are no longer used in php5.
1. cookie usage example
-
If ($ _ GET ['out'])
- {// Used to cancel cookies
- Setcookie ('id ',"");
- Setcookie ('pass ',"");
- Echo "script" location. href = 'login. php 'script '; // because cookies do not take effect immediately, they only take effect when you refresh again. Therefore, after cancellation, the page is automatically refreshed.
- }
-
If ($ _ POST ['name'] & $ _ POST ['password']) // if the variable username and password exist, set cookies below
- {// Used to set cookies
- Setcookie ('id', $ _ POST ['name'], time () + 3600 );
- Setcookie ('pass', $ _ POST ['password'], time () + 3600 );
- Echo "script location. href = 'login. php' script"; // enables cookies to take effect immediately
-
}
- If ($ _ COOKIE ['id'] & $ _ COOKIE ['pass'])
- {// When cookies are successfully set, they are used to display cookies
- Echo "logon successful!
Username: ". $ _ COOKIE ['id']."
Password: ". $ _ COOKIE ['pass'];
- Echo"
";
- Echo "cancel cookies"; // double quotation marks. if there are quotation marks, use single quotation marks.
- }
- ?>
-
-
2. session usage example
-
// Session usage instance
- Session_start (); // start the session, which must be placed in the first sentence; otherwise, an error occurs.
- If ($ _ GET ['out'])
- {
-
Unset ($ _ SESSION ['id']);
- Unset ($ _ SESSION ['pass']);
- }
-
If ($ _ POST ['name'] & $ _ POST ['password'])
- {
- // Set the session
- $ _ SESSION ['id'] = $ _ POST ['name'];
- $ _ SESSION ['pass'] = $ _ POST ['password'];
- }
-
If ($ _ SESSION ['id'] & $ _ SESSION ['pass'])
- {
- Echo "logon successful!
User id: ". $ _ SESSION ['id']."
User password: ". $ _ SESSION ['pass'];
- Echo"
";
- Echo "deregistering a session ";
- }
- ?>
-
-
|