Php session control _ PHP Tutorial

Source: Internet
Author: User
Tags php session
Php session control. Session Control in php when the browser closes the cookie data, the website cannot pass through the cookie, but the url parameter can still be passed (session ), in fact, write the php se php session control

When the browser closes the cookie data, the website will not be able to pass through the cookie, but the url parameter transmission can still be performed (session). In fact, the session control of php is the same as the cookie session control.

First, create the php file to be used.

This step is the same as cookie. In fact, the session can also be passed through cookies. on the basis of the cookie, enable session: session_start () in the header ()

 Note that the jump page of login. php cannot use the header but can only be redirected through javascript.

// Jump to the echo 'script '; | r | echo location = 'index. php'; | r | echo 'script';

Change $ _ COOKIE [] to $ _ SESSION [].

The session is passed through cookies. The following describes how to pass url parameters.

First: Pass parameters by using sid, that is, add "? Sid = "

In this way, you can use PHPSESSID in the configuration file to replace the sid, and the same effect can be achieved.

Login. php

 The php part of login. php is also slightly modified. 

 ; // The Jump page cannot be the header if (isset ($ _ POST [sub]) {include conn. inc. php; $ SQL = select id from users where name = '{$ _ POST [name]}' and password = '. md5 ($ _ POST [password]). '; $ result = $ mysqli-> query ($ SQL); // save the data if ($ result-> num_rows> 0) {$ row = $ result-> fetch_assoc (); $ _ SESSION [username] =$ _ POST [name]; $ _ SESSION [uid] =$ _ POST [uid]; $ _ SESSION [isLogin5] = 1; // jump to the echo 'script '; | r | echo location = 'index. php? Sid =. session_id (). '; // call session_id () | r | echo 'script';} The echo user name and password are incorrect.}?>
For other pages, you only need to add "? Sid = ".

Unlike the cookie, deregistering a program involves four steps:Enable, clear, delete, and completely destroy

// Enable session session_start (); // The session value $ _ SESSION = array (); // delete the sessionid (isset ($ _ cookie [session_name ()]) in the COOKIE) {setCookie (session_name (), '', time ()-3600, '/'); // be sure to write the fourth parameter (path)} // completely destroy session session_destroy ();

SecondYou do not need to set whether to enable the cookie data function in the browser and choose whether to use the cookie or session for transmission.

A, The link or form is followed "? ", This is similar to passing through sid, but SID is a constant.

Index. php:

> Page 2> Page 3> exit

Login. php:
// Jump to the echo 'script '; | r | echo location = 'index. php ?. SID. '; // The SID constant. if the cookie is enabled, the cookie is used. if the cookie is not enabled, the session is used. | r | echo 'script';

 Method = post> user logon 
User name
Password

BModify the php. ini configuration file

The code is basically the same as that transmitted by cookies. it only needs to start session: session_start ();

Practice: change the value of session. use_trans_sid in the configuration file to 1.
Purpose: add the PHPSESSID format to all links by default.

When the browser closes the cookie data, the website will not be able to pass through the cookie, but the url parameter can still be passed (session). In fact, the php se...

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.