_php tips for how sessions and cookies operate in PHP

Source: Internet
Author: User
Tags session id php session sessions setcookie unique id

Session
PHP $_session can store the current user data information, when users visit the Web site, PHP will give each user access to create a session ID, the ID is a unique ID, saved on the client, and the user's conversation data is saved to the service side, PHP can store each of the different user information, and when the session expires, the user sessions information is invalidated.
Use session, when using PHP session, must be in the header plus session_start (), tell the server to start using session, and there should be no output before it, otherwise it will be an error.

<?php 
session_start (); 
 
PHP code ... 
? > 

PHP settings and get session
we can use PHP's $_session to set up and get session data, such as:

<?php 
session_start ();  
 
Set a session value 
$_session["name" = "Hello"; 
 
Saves the session as an array 
$_session["arr" = Array (' name ' => ' Hello ', ' url ' => ' http://www.helloweba.com ', ' type ' = > ' website '); 
? > 

Once the session data has been stored, we can use sessions on the site, such as we can get the data on another page:

<?php 
session_start ();  
 
Gets the saved session name 
Echo $_session["name"; 
 
Print array session 
Print_r ($_session["arr"]); 
? > 

PHP Delete Session
when session is no longer in use, we can use PHP to delete and empty session data as follows:

<?php 
unset ($_session["name"]); 
? > 

If you want to empty all session information for the current user, you can use the following code:

<?php 
Session_destroy (); 
? > 

Cookies
a cookie is a temporary file created on the current client by a user-accessed Web service to hold user information so that the next time a user continues to visit the site, the Web server can identify the user's information, and common cookies are used to save data such as user interface, user ID, and so on.
PHP Settings Cookie
we can use PHP's Setcookie () to create cookies on the client, this function provides the main three parameters, cookie name, value, and effective time length.

<?php 
$cookie _val = ' Chrome '; 
Setcookie ("Browser", $cookie _val, Time () +3600); 
? > 

Running the above code will create a cookie called chrome, and the cookie information expires 1 hours after the client is saved for 1 hours.
PHP receives cookies
when cookies are created, we can easily get cookie values, using PHP's $_cookie, as follows:

<?php 
if (isset ($_cookie[' browser ')) { 
  echo ' your browser is: '. $_cookie[' browser ']; 
> 

PHP Delete Cookies
If you want to completely delete the saved cookie information on your machine, you can use the following code:

<?php 
Setcookie ("browser", "", Time ()-3600); 

The above code empties the cookie named browser and sets the expiration date to 1 hours ago, completely emptying the cookie information.
This article from the beginner's point of view, explained the entry level of PHP Knowledge: Session and Cookie application, we do not have to delve into the principle, as long as the use of the line.

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.