PHP Learning Notes (26) initial use of Session in PHP (cookie based)

Source: Internet
Author: User

There are several ways to use the session

1. Cookies based

2. Based on the URL of the SID string

3. stored in the database

4. Stored in the memcache (the most efficient)


<?php/** * Session control * PHP.ini can be set to save location * session.sava_path= "/xxxxx/xxxxx" * session.** to set up Sessio
 N Property * * 1. Open Session * If the session is based on a cookie, then the session_start is not allowed to have any form of output * or in the configuration file to use Session.auto_start=1; * If the configuration file is used, the object cannot be placed in the object file, so it is not recommended to use the configuration file to open * * 2. Destroy Session * * * * *//Open session or use open SessionID session_start (

	);
	As long as the current session, and then open other pages, you can through the _session["AA" and _session["BB"] to access the content $_session["AA"]= "111";
	
	$_session["BB"]= "222";
	echo "session_id:". session_id ();
	echo "<br>session_name:". Session_name (); If a cookie is turned on, it can be found in the default cookie SessionID//phpsessid is the Session.name property value echo "<br>cookieid:" in php.ini. $_cookie[
	"Phpsessid"];
	
	echo "<br>cookiename:". $_cookie[session_name ()];
	
	Destroysession ();
		
		/** * Destroy session/function Destroysession () {//1. Open session//On new page you need to open session//Session_Start first ();
		2. Delete data//Delete the value method in session One: unset ($_session["AA"]);
		
		Delete Value method in session two: $_session=array (); 3. Delete Client in cookie sEssionid if (Isset ($_cookie[session_name ())) {//need to specify the path of the COOKIE in php.ini Session.cookie_path Setcookie (
		), ", Time ()-3600, '/');
		
		};
	4. Complete destruction of Session Session_destroy (); }?>


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.