» » PHP Session and Cookie operation

 

PHP Session and Cookie operation

Author: bamboo06 on 20-12-2014, 02:58, views: 2849

8
We need to use to track user information Session and Cookie, such as user login authentication, user browsing history record store shopping cart data, user session valid time limit. Today we come to understand how to operate under PHP is the Session and Cookie.
PHP Session and Cookie operation

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 .

Category: PHP Scripts

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
<
  • 0 Comments
  • 0 Articles
7 September 2017 21:46

Noel Harper

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
The PHP Session and Cookie operation went much better than I expected. I had the paper review service working on my papers while I continued with editing the website. The end-result pleased my boss.

<
  • 0 Comments
  • 0 Articles
4 October 2017 23:24

Bryce

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
PHP Session and Cookie operation is an excellent post you have shared over here with us. The essay writing services uk review shared on this too. All these settings and information posts are really good and helpful.

<
  • 0 Comments
  • 0 Articles
13 October 2017 23:52

Gale Snider

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog. is noocube available at gnc

<
  • 0 Comments
  • 0 Articles
24 October 2017 17:21

Muhammad Abuzar

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Good tip to handle cookies and session with the type of the browser your diagram very simple and easy to understand able. For food eBook: best Chinese buffet near me now

<
  • 0 Comments
  • 0 Articles
10 November 2017 22:54

Hello

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!
!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!!!1!

<
  • 0 Comments
  • 0 Articles
17 January 2018 00:18

Kolkata Escorts

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Beauti Queen amazing kolkata escorts agency alwyas ready for you at your doorstep. Beauti Queen provide you high profile independent girl in kolkata, escorts service in kolkata, kolkata escorts. If you are looking for the ultimate encounter in Kolkata then I am your sophisticated young Kolkata escorts that provides upscale companionship for making your night becomes true.
escorts service in kolkata, kolkata escorts, escorts in kolkata, independent escorts in kolkata, female escorts in kolkata

<
  • 0 Comments
  • 0 Articles
2 February 2018 18:31

Kolkata Escorts

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Renu Das Kolkata Escorts Services has gorgeous females provides Independent Escorts Service in Kolkata call girls at 100% satisfaction with VIP models. Provided Kolkata escorts at our agency are professional in nature and are eager to serve you at your place.
kolkata escorts, escorts in kolkata, escorts service in kolkata, female escorts in kolkata, escorts agency in kolkata, independent escorts in kolkata

Renu Das Kolkata Escorts Services has gorgeous females provides Independent Escorts Service in Kolkata call girls at 100% satisfaction with VIP models. Provided Kolkata escorts at our agency are professional in nature and are eager to serve you at your place.
kolkata escorts, escorts in kolkata, escorts service in kolkata, female escorts in kolkata, escorts agency in kolkata, independent escorts in kolkata

<
  • 0 Comments
  • 0 Articles
1 March 2018 05:54

Delhi Escorts

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Riya Chaudhary is an Independent Delhi Escort girl, who offers dazzling hot Delhi Escorts Service, incall or outcall services both to complete your dreams in real entertainment. We offers high class Independent Escorts in Delhi at Riya Chaudhary Delhi Escorts service agency for elite peoples in the city to avail ultimate satisfaction. you can spend your beautiful moments.
delhi escorts, independent escorts in delhi, escorts in delhi

Information
Comment on the news site is possible only within (days) days from the date of publication.