Home Backend Development PHP Tutorial PHP Session Usage and Sessions Into Example Application_PHP Tutorial

PHP Session Usage and Sessions Into Example Application_PHP Tutorial

Jul 13, 2016 am 10:58 AM
php session and Example application usage

PHP Session usage and Sessions into instance application


The reason why the session header information has been sent is the same as that of cookies.
In PHP Tutorial 5, all PHP session registry configuration options are configurable during programming. Generally, we do not need to modify their configuration. To learn about PHP session registry configuration options, please refer to the Session in the manual. Processing function.
When the session saves data, it is stored through the serialized $_SESSION array, so there are problems with serialization. There may be special character values ​​that need to be encoded with the base64_encode function, and then decoded with base64_decode when reading


Below is a simple script that you should start a PHP session at the beginning of your PHP code.

session_start(); // start up your PHP session!
?>

This small piece of code will register a user's session with the server, allowing you to start saving user information and assigning a UID (unique identification number to that user's session).

Storing session variables
When you want to store user data in session use the $_SESSION associative array. This is where you two store and retrieve session data. There were other ways to perform this storage operation in previous PHP versions, but it has been updated and this is the correct way to do it.

session_start();
$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>
Take a look at a simple shopping cart example

session_start();
if(isset($_SESSION['views']))
$_SESSION['views'] = $_SESSION['views']+ 1;
else
$_SESSION['views'] = 1;

echo "views = ". $_SESSION['views'];
?>

session_start();
if(isset($_SESSION['cart']))
Unset($_SESSION['cart']);
?>

session_start();
session_destroy();
?>


Session usage examples

/**
* Verify the legitimacy of the session
*
​*/
function sessionVerify() {
If(!isset($_SESSION['user_agent'])){
         $_SESSION['user_agent'] = MD5($_SERVER['REMOTE_ADDR']
.$_SERVER['HTTP_USER_AGENT']);
}
/* If the user session ID is forged, reassign the session ID */
elseif ($_SESSION['user_agent'] != MD5($_SERVER['REMOTE_ADDR']
. $_SERVER['HTTP_USER_AGENT'])) {
session_regenerate_id();
}
}

/**
* Destroy session
* Perfect implementation in three steps, don’t miss it
*
​*/
function sessionDestroy() {
Session_destroy();
setcookie(session_name(),'',time()-3600);
$_SESSION = array();
}
?>


Sessions solve the problem of PHP allowing you to store user information on the server for later use (i.e. username, items in shopping cart, etc.). However, this session information is temporary and will usually be deleted soon after the user has left the website, which uses the session.

It is important to think about if temporary storage of sessions is appropriate for your website. If you need a longer term storage, you'll need to find another solution, like a MySQL database tutorial.

Session works by creating a unique identification number (UID) for each visitor and storing variables based on this ID. This helps prevent two users from getting their data confused with another when visiting the same web page.

Note: If you are not experienced with session programming, it is not recommended that you use it on websites that require highly secure sessions, because there are security holes that require some advanced technology to block.

Start a PHP session
Before you can start storing user information in your PHP session, you must first start the session. When you start a session, it must be at the beginning of your code, before any HTML or text is sent.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632000.htmlTechArticlePHP Session usage and Sessions into the instance application session. The reason why the header information has been sent is the same as the cookie. In PHP Tutorial 5 In, all the registry configuration options of php session are programmable...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles