search
HomeBackend DevelopmentPHP TutorialSession control in php_PHP tutorial

php session session control

When the browser turns off cookie data, the website will not be able to use cookie transfer, but the URL parameter transfer can still be carried out (session). In fact, the overall session control of writing PHP is the same as the cookie session control

First create the php file to be used in writing

Session control in php_PHP tutorial

This step is the same as cookie. In fact, the session can also be passed through cookies. Based on the cookie, open the session at the beginning: session_start()

<!--?php

	session_start();
    //判断:如果没登录自动跳转到登录页面
    if(!$_SESSION[isLogin5]){
        header(Location:login.php);
    }</pre-->

下面要注意的是,login.php 的跳转页面不能使用header 而只能通过 javascript 进行跳转

//跳转界面
echo &#39;<script>&#39;;|r|
echo location=&#39;index.php&#39;;|r|
echo &#39;</script>&#39;;

Then change $_COOKIE[ ] to $_SESSION[ ]

This is how session is passed through cookies. The following mainly explains how to pass url parameters

The first : Pass parameters through sid, that is, add "?sid="

after the link or form

This method can also use the PHPSESSID in the configuration file to replace the sid and achieve the same effect

login.php




The php part of login.php has also been slightly modified.

<!--?php
	
	session_start();
	echo session_id().<br-->;  //跳转页面不能不是header

    if(isset($_POST[sub])){
    	include conn.inc.php;

    	$sql=select id from users where name=&#39;{$_POST[name]}&#39; and password=&#39;.md5($_POST[password]).&#39;;

    	$result=$mysqli->query($sql);

    	//保存数据
    	if($result->num_rows > 0){
    		$row=$result->fetch_assoc();
    		
    		$_SESSION[username]=$_POST[name];
    		$_SESSION[uid]=$_POST[uid];
    		$_SESSION[isLogin5]=1;

    		//跳转界面
    		echo &#39;<script>&#39;;|r|
    		echo location=&#39;index.php?sid=.session_id().&#39;;   //将session_id() 调过来|r|
    		echo &#39;</script>&#39;;
    	}
    	echo 用户名密码有误;
    }
?>
For other pages, just add “?sid=” after the link.

The logout process is not like a cookie, it has four steps: open, clear, delete and completely destroy

    //开启session
    session_start();

    //情况session值
    $_SESSION=array();

    //删除客户端的在cookie中的sessionid
    if(isset($_COOKIE[session_name()])){
        setCookie(session_name(),&#39;&#39;,time()-3600,&#39;/&#39;); //一定要写上第四个参数(路径)
    }

    //彻底销毁session
    session_destroy();


The second type does not need to be set to automatically choose whether to use cookies or sessions for delivery based on whether the browser has turned on the cookie data function.

a, links or forms are followed by "?". This is similar to passing through sid, but SID is a constant

index.php:

    >第二页

    >第三页

    >退出

login.php:
   		//跳转界面
    		echo &#39;<script>&#39;;|r|
    		echo location=&#39;index.php?.SID.&#39;;   //SID 常量如果开启cookie则使用cookie,如果没开启就用session|r|
    		echo &#39;</script>&#39;;



method=post>

User login

 
用户名
密码

b, modify the php.ini configuration file

The code is basically the same as that passed by the cookie, except that the session needs to be opened at the beginning: session_start();

Method: Change the value of session.use_trans_sid in the configuration file to 1
Function: Add the form of PHPSESSID to all links by default

Session control in php_PHP tutorial

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/970254.htmlTechArticlephp session control When the browser turns off cookie data, the website will not be able to use cookie delivery, and the url Parameter transfer can still be carried out (session). In fact, the se...
Statement
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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools