- Setcookie ("Cookie", "Cookievalue", Time () +3600, "/forum", ". jbxue.com", 1);
Copy Code1), receiving and processing cookiephp support for the reception and processing of cookies is very good, is completely automatic, and the same as the principle of the form variable, especially simple. For example, setting up a cookie,php named Mycookier will automatically parse it from the HTTP header received by the Web server and form a variable like the normal variable, named $ MyCookie, which is the value of the cookie. arrays also apply. Another way is to reference the PHP global variable $http_cookie_vars array. Examples are as follows: (assuming that they were set up in the previous page and still valid)
- Echo $MyCookie;
- echo $CookieArray [0];
- echo $_cookie["MyCookie"];
- echo $HTTP _cookie_vars["MyCookie"];
- ?>
Copy Code2), delete cookie to delete an existing cookie, there are two ways:
- 1, Setcookie ("Cookie", "");
- 2, Setcookie ("Cookie", "Value", Time () -1/time ());
Copy Code3), the use of the restriction of the cookie 1, must be set before the content output of the HTML file, 2. Different browsers have inconsistent handling of cookies and sometimes have incorrect results. 3, the limit is on the client. A browser can create a maximum of 30 cookies, and each cannot exceed 4KB, and each Web site can set a total of no more than 20 cookies. 3. Session Configuration and Application
- Session_Start (); Initializes the session. Need to be in the header of the file
- $_session[name]=value; Configure Seeeion
- Echo $_session[name]; Use session
- Isset ($_session[name]); Judge
- Unset ($_session[name]); Delete
- Session_destroy ();//Consume all session
- ?>
Copy CodeNote: Session_register (), session_unregister,session_is_registered is no longer used under PHP5. 1. Examples of usage of cookies
if ($_get[' out ')
- {//For cancelling cookies
- Setcookie (' id ', ' ");
- Setcookie (' Pass ', ' ");
- echo ""; Because cookies are not effective in time, they only take effect when you refresh them again, so the page automatically refreshes when you log off.
- }
if ($_post[' name ']&&$_post[' password ')//If the variable username and password exist, set the cookie below
- {//For setting cookies
- Setcookie (' id ', $_post[' name '],time () +3600);
- Setcookie (' Pass ', $_post[' password '],time () +3600);
- echo ""; Make cookies effective in time
}
- if ($_cookie[' id ']&&$_cookie[' pass ')
- {//cookies is used to display cookies after successful setting
- echo "Login Successful!
User name: ". $_cookie[' id ')." Password: ". $_cookie[' pass ');
- echo "
";
- echo "Log off cookies"; In double quotation marks, if there is another quotation mark, you need to use single quotation marks.
- }
- ?>
Copy Code
Copy Code2. Session Usage Example
Session Usage Example
- Session_Start ();//start session, must be placed in the first sentence, otherwise it will be wrong.
- if ($_get[' out ')
- {
Unset ($_session[' id ');
- unset ($_session[' Pass ');
- }
if ($_post[' name ']&&$_post[' password '))
- {
- Used to set session
- $_session[' id ']=$_post[' name '];
- $_session[' pass ']=$_post[' password ';
- }
if ($_session[' id ']&&$_session[' pass ')
- {
- echo "Login Successful!
User id: ". $_session[' id ']." User password: ". $_session[' pass ');
- echo "
";
- echo "Logout session";
- }
- ?>
Copy Code
Copy Code |