When setting a COOKIE on a page, you must refresh or go to the next page to get the value of the variable using $_cookie.
The reason is that when the page is first loaded by the browser, the cookie in the page is set and sent to the client's designated storage location, so $_cookie does not receive the value of the cookie variable sent by the client, and when it refreshes or arrives at the next page, The client will send a COOKIE corresponding to that address to the server before the page program runs on the server side, so $_cookie can get the value! Plainly, when each page is accessed, if the client finds a cookie corresponding to the access address, the cookie is sent to the server side before the program runs on the server. (personal view of this)
I am not strong expression ability, if unclear, please also sorry!
When setting up an array of cookies in PHP, you cannot use the same method of adding data as in PHP:
Copy CodeThe code is as follows:
Setcookie (' my_cookie[] ', 1);
Setcookie (' my_cookie[] ', 2);
Print_r ($_cookie); Array ([My_cookie] + = Array ([0] = 1))
The value added of the array is added successfully, but the index is not changed, and the data behind it overwrites the previous data!
From this
My_cookie[], the default point to the position of the first element of the data, which is indexed as
0 of the location. Note that it is not the same as in PHP! Remember to specify an array element index later with cookie data Oh!
$my _cookie[] = 1;
$my _cookie[] = 2;
Print_r ($my _cookie); Array ([0] = 1 [1] = 2)
?>
two ways to delete a cookie variable:
1.php
Copy CodeThe code is as follows:
Setcookie (' user_name_1 ', ' zhaofei299 ', Time () +3600); 1-hour survival time
Setcookie (' user_name_2 ', ' ZHAOFEI299 ', Time () +3600); 1-hour survival time
?>
2.php
Copy CodeThe code is as follows:
Setcookie (' user_name_1 '); First Kind
Setcookie (' user_name_2 ', "", Time ()-1); The second Kind
Print_r ($_cookie); Refresh page 2 above will output Array ([user_name_1] =)
/* Why the User_name_1 in the Super global variable $_cookie is not deleted (the variable is empty and does not mean that it does not exist), and
user_name_2 been removed? That's because two different ways to delete a variable!
The first: The lifetime of the cookie is set, except that its value is set to NULL by default, and the lifetime is the same as the browser, the browser
When off, cookies are deleted! So when you reopen a browser, the output address, you will find that the cookie variables are all deleted!
Comment out the two Setcookie () function sections in 2.php (re-export the address)!
The second kind: Also set the lifetime of the cookie, is to make the lifetime of the cookie must expire, the cookie is deleted, so the brush
New page, when the client sends a cookie to the server, $_cookie is not able to get the value of the cookie variable!
*/
?>
The session ID is stored in the client cookie by default!
Copy CodeThe code is as follows:
Session_Start ();
Print_r ($_cookie);
?>
There are two ways to set up cookies
Header (' set-cookie:user=zhaofei299 ');
Setcookie (' user ', ' zhaofei299 ');
Session variables cannot be overloaded with Get data or post data!
Using the session variable to pass an array, the object is not serialized!
When passing an object using the session variable, you must include the definition of the class object before calling Session_Start (), deserializing
(serialize)!
Deleting a single session variable can be deleted directly using unset ($_session[' * * *))!
Deleting all session variables can not be used with unset ($_session), as this will delete all session information, including stored in the cookie
PHPSESSID, which destroys the conversation between two pages, should use $_session = Array ();
Eliminate the session ID and lose contact between pages!
Session_destroy ();
Program Listing 1.1
Copy CodeThe code is as follows:
Session_Start ();
Header (' Content-type:text/html;charset=utf-8 ');
$_session[' a '] = ' a ';
$_session[' b '] = ' B ';
Unset ($_session); After the test, look at the notes again
$_session[' user '] = ' zhaofei299 ';
Echo ' session_id: '. session_id (). '
';
echo ' under test ';
?>
Copy CodeThe code is as follows:
Session_Start ();
echo $_session[' user ';
Echo session_id (); The session variable has changed
?>
two ways to pass the session ID (session_id):
1.cookie
2.url
Because the default session is cookie-based and the cookie is sent with the HTTP protocol, as with cookies, the
Session_Start () cannot have any output before!
Now I'm talking about the second, passing the session ID through a URL
This constant in PHP defines the SID to get the ID of the session
The use of sesssin_id!
Copy CodeThe code is as follows:
Session_Start ();
echo defined (' SID ')? ' True ': ' false '; True
Echo SID; There's nothing?
?>
Why is Sid's value null? What's wrong with it?
The reason is because the session is cookie-based by default, and the SID only session_id through the URL
The value will be assigned only when the data is passed!
Disable the cookie in the browser and you will see that the SID has output, not null!
Delete session
Be implemented in three steps.
Copy CodeThe code is as follows:
Session_destroy (); The first step: Delete the server-side session file, which uses
Setcookie (Session_name (), ", Time ()-3600); Step two: Delete the actual session:
$_session = Array (); Step three: Delete the $_session global variable array
?>
We all know that the session variable is stored on the server side, that is, the session variable will be saved in a directory in the server, I
You can find the address saved in the session file in the Session.save_path in php.ini.
The lifetime of the default session is the end of the browse close, but to know when the session expires, when the page Session_Start () is opened
Determine if the session ID exists, create one if it does not exist, or load the session ID variable into the page! Because expiration session_id will
A new is created, but it is saved on the server side of the session file and is not deleted (Close browser, open session file Save
Session_destory () function to clear the session ID and clear the corresponding session file, so that the most
Thorough removal!
session_id uses a URL to pass session variable data, because Session_Start () determines whether the session ID is stored when the session is opened
In, create one if it doesn't exist, or load the session ID variable into the page!
Instead, the URL is used to pass the session_id, but each refresh/entry page generates a session ID, so the page is
You can't get session_id variables set on another page, so using a session doesn't make sense!
Workaround:Before Session_Start (), manually set the page's session_id so that the page can be set on the previous page
Session variable, it also realizes the transfer of the conversation, the following code can be explained!
Cookies are disabled
1.php
Copy CodeThe code is as follows:
Session_Start ();
$_session[' user '] = ' zhaofei299 ';
Echo ' next page ';
?>
The 4th line of code in 1.php can also be written as: Echo ' next page ';
You can set session.use_trans_sid in php.ini to 1 so that when you pass the session ID using a URL,
The browser will automatically append the session_id to the URL!
It's like typing in the browser: www.baidu.com, the browser will automatically change it to http://www.baidu.com/
2.php
Copy CodeThe code is as follows:
session_id ($_get[' phpsessid '); Manually set the session_id, which allows you to use the previous page's
session_id the variable, it also realizes the session!
Session_Start ();
Print_r ($_session);
?>
commonly used session functions:
Copy CodeThe code is as follows:
BOOL Session_Start (void); Initialize session
BOOL Session_destroy (void): Deletes the server-side session Association file.
String session_id () ID of the current session
String Session_name () the name of the session currently accessed, which is the cookie name where the client holds the session ID. Default
Phpsessid.
Array Session_get_cookie_params () the details of the session associated with this session.
String Session_cache_limiter () controls client-side caching of pages using session
INI session_cache_expire () controls client cache time
BOOL Session_destroy () Delete the server-side file that holds session information
void session_set_cookie_params (int lifetime [, String path [, string domain [, bool
secure [, BOOL HttpOnly]]]) sets the details of the session associated with this session
BOOL Session_set_save_handler (callback open, callback close, callback read, callback
Write, callback destroy, callback GC) defines the function that handles the session (not the default way)
BOOL SESSION_REGENERATE_ID ([bool delete_old_session]) assigns a new session ID
http://www.bkjia.com/PHPjc/327786.html www.bkjia.com true http://www.bkjia.com/PHPjc/327786.html techarticle when setting a COOKIE on a page, you must refresh or go to the next page to get the value of the variable using $_cookie. The reason is because when the page is first loaded by the browser, the page ...