Session
PHP's $ _SESSION current user data can be stored, when the user visits WEB site, PHP will give users access to each create a session ID, the ID is a unique ID, saved on the client, and the user's session data is saved to the the service side, PHP can be stored for each different user information when the session expires, the user session information will be invalidated.
To use Session, when using the PHP session, be sure to add in the page header session_start (), tells the server to start using the session, but before that it should not have any output, otherwise it will error.
<?php
session_start();
//PHP code...
?>
PHP settings and get Session
We can use the PHP $ _SESSION to set and get Session data, such as:
<?php
session_start();
//set session value
$_SESSION["name"] = "Hello";
//save session as array
$_SESSION["arr"] = array('name' => 'Hello', 'url' => 'http://www.goocode.net', 'type'=> 'website');
?>
Session data is stored once, we can use the Session on the site, for example, we can get the data in another page Session:
<?php
session_start();
//get saved Session name
echo $_SESSION["name"];
//print session array
print_r($_SESSION["arr"]);
?>
Use PHP to delete Session
When you no longer use Session, we can use the PHP session data will be deleted and emptied, as follows:
<?php
unset($_SESSION["name"]);
?>
If you want to clear all of the current user Session information can use the following code:
<?php
session_destroy();
?>
Cookie
Cookie is a temporary file and web services by end users access to the creation of the current client, to store user information, so that when the user next to continue to access the site, the site server is able to identify the user information, a common user interface to save Cookie , user ID and other data.
Use PHP to set Cookie
We can use PHP's setcookie () to create a cookie on the client, this function provides three main parameters, cookie name, value and effective duration.
<?php
$cookie_val = 'Chrome';
setcookie("browser", $cookie_val, time()+3600);
?>
Run the above code will create a name for Chrome, Cookie, and stored in the client one hour, the cookie information after failure of one hour.
Receiving cookie by PHP
When Cookie is created, we can easily get to the cookie value, use the PHP $ _COOKIE, usage is as follows:
<?php
if(isset($_COOKIE['browser'])) {
echo 'Your browser is:' . $_COOKIE['browser'];
}
?>
Use PHP to delete cookie
If you want to completely delete the cookie information stored on your machine, you can use the following code:
<?php
setcookie("browser", "", time()-3600);
?>
The above code name for the browser's cookie will be emptied, and the validity is set to one hour before completely emptied the cookie information.
In addition there are cookie javascript front-end operations example, this site has articles about it.
From the point of view of a beginner, explain the entry-level knowledge PHP: Session and Cookie applications, we do not have to go into the theory, just use the line. 2015 is about to come, the next GOOCODE intends to show you several front-end and back-end PHP interactive project for everyone to share, of course, will use Session and Cookie, like WEB chat rooms, online video, HTML5 online live and so on, so stay tuned .