Use cookies with session in PHP

Source: Internet
Author: User
Tags php session setcookie
Use cookies and session in PHP

Cookies and sessions are much more in Java or PHP, and cookies can be seen as client-side technology, while session is the server technology. Like shopping cart and website automatic login can be implemented with cookies, the session is more biased to verify this piece, compared to cookie security is higher, because the session is stored on the server, can not be arbitrarily deleted or modified. Here is a simple sharing of my learning experience

Use of 1.cookie

If you need to save a cookie, you can use the Setcookie function directly on the PHP page to save the cookie using the following method

 
  

The first parameter is the key of the cookie, the second is value, and the third indicates when the cookie expires in seconds, and time () represents the current times. This code means that the cookie expires in 2 minutes.

The same is true for updating cookies. If it is Firefox, we can see cookies as follows


below to delete cookies

Setcookie ($cookiename, "), or Setcookie ($cookiename, NULL); Both of these methods can delete cookies

It is even easier to get a cookie, which can be obtained using $_cookie, which can also be used by a batch operation cookie via this predefined hyper-global array

Use of 2.session

(1) Enable session

(2) Put the object into the session

Session_Start ();  $_session["Password"]= "123456";

Two words can save Sessiona, just like this

To remove the session is also used $_session obtained, delete a single session can use Unset ($_session["password"]), if it is deleted all can use Session_destroy ();


The true principle of the session is not so well understood, it is difficult to understand deeply, and it is necessary to use Firebug to view HTTP requests and responses. When the server creates a session, the client browser returns a

PHPSESID, this ID is the unique ID of the session. The next time the browser client takes a session, it will go to the server side to remove the session information through this unique ID. If the client disables the cookie, the session cannot be shared according to the normal code. Here are two of the simplest practices

The first is URL rewriting, first to determine if there is phpsessid if there is set session_id (SID value);

The second is to modify the php.ini in the session.use_Trans_sid Set the value to 1


For the first type of PHP provides a constant called SID can be used directly, because this situation is more extreme usually not encountered. The specific code will not be written. There is a need to write a review for me, I sent directly to the past.

Note that the PHP session can also be stored in the object, it is important to note that the use of this object before using require_once to make it better


Finally, for the application of the session, I found a very simple verification code on the Internet, the code is as follows

 
  

You don't have to explain how to use it.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.