Home Backend Development PHP Tutorial php:Does Session have to rely on Cookie_PHP Tutorial

php:Does Session have to rely on Cookie_PHP Tutorial

Jul 14, 2016 am 10:10 AM
cookie php session use rely Can client case yes default

The session in php can use the client's cookie by default (to distinguish it from cookies in the ordinary sense, I call it session cookie, and cookies in the ordinary sense are cookies) to save the session id, but in php Can the session only use session cookies?

Of course not, otherwise why bother to create a session? It is better to just use cookies. One of the advantages of sessions is that when the client's cookie is disabled, the session id will be automatically attached to the URL, so that the session id can be used Can remember session variables.

Let me write two files to confirm. First, disable cookies in the browser.


[php]
//The file name is test1.php
session_start();
session_register("url");
$url="test2.php";
echo "goto test2.php";
?>

//The file name is test1.php
session_start();
session_register("url");
$url="test2.php";
echo "goto test2.php";
?> [php]
session_start();
if (session_is_registered("url")) {
echo "congratulations.";
$url="test1.php";
echo "goto test1.php";
} else
echo "failed.";
?>

session_start();
if (session_is_registered("url")) {
echo "congratulations.";
$url="test1.php";
echo "goto test1.php";
} else
echo "failed.";
?>


Now enter "http://localhost/test1.php" in the browser, move the mouse over the link and look at the address on the status bar. It is not simply "http://localhost/test2.php", but this In the form: "http://localhost/test2.php?phpsessid=6e3610749f7ded3784bc4a4dd10f879b".
You can also view the source file of html, the source file is in this form:
goto test2.php
So this is entirely due to PHP and has nothing to do with the browser, which means that the session is valid no matter what browser you use, not only for IE as some people think.
However, our hyperlink statement is output by the echo statement. What will happen if the hyperlink is not included in the PHP tag ? Let’s write an example to verify it. Make test1.php a little bit Modification:


[html]
session_start();
session_register("url");
$url="test2.php";
echo "goto test2.php";?>
(html form)goto test2.php

session_start();
session_register("url");
$url="test2.php";
echo "goto test2.php";?>
(html form)goto test2.php
Enter "http://localhost/test1.php" in the browser, move the mouse to the two links respectively to see if there is any difference? You can see that the two links are exactly the same, and the session ID will be automatically attached later. .So don’t worry that links that are not included in the php tag will become invalid, php will not be so stupid.
But when using it, please note that you must first use the session_start() function to tell php to start using the session, even if you only have html code in this file, such as:




gogogo
…………

I remember someone said that this advantage can only be exerted under linux/unix, but I use win2000p+apache1.3.17+php4.0.4pl1, php is the apache module mode, but it still works. On the contrary, I switched When I went to Linux for testing, it didn't work. In fact, an option during compilation --enable-trans-sid controls whether this function is useful. When compiling according to PHP default, this function is not turned on. Just Just add it when recompiling. My configuration is apache1.3.17+php4.0.4pl1, and php is in apache module mode. After recompiling on Linux, the test with netscape navigator4.7 can pass (this further proves that it has nothing to do with the browser) ).
Session alone cannot be used across windows. Even if you enable cookies, when you have a legal session id in a window (recorded in the session cookie, not in the URL), and then open a new window to enter the same page, You will have a new session id again without affecting the previous window.
If you want to use the same session ID across windows, you can only specify the session ID after the URL. That is to say, if you copy the URL of the window with the session ID and paste it in a newly opened window, it will still be used. Knowing the principle of session id, it is not difficult to implement a cross-window session. You can combine cookies with sessions. First obtain the current legal session id, then record it in the cookie, and then read the cookie in other windows to obtain it. The current session id.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477502.htmlTechArticleThe session in php can use client cookies by default (to distinguish it from cookies in the ordinary sense, I It is called a session cookie (a cookie in the ordinary sense is a cookie) to save sessi...
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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

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.

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

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.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

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 Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles