Solution to cross domain problem of session implemented by ThinkPHP framework

  • 2021-07-06 10:21:45
  • OfStack

ThinkPHP's session cross-domain problem has been encountered by many developers!
In fact, whether it is ThinkPHP or php itself, it is necessary to set session.cookie_domain when solving the cross-domain problem of session.
In ThinkPHP, you need to modify the configuration file conf/config. php
Add in line 1:


ini_set('session.cookie_domain', ".domain.com");// Cross-domain access Session

After summary, there are mainly the following solutions to the cross-domain problem of session:

Case 1: If you don't have the. htaccess file in your directory, that is, you don't take url pseudo-static, then you add in line 1 of conf/config. php:


ini_set('session.cookie_domain',".domain.com");// Cross-domain access Session

This may be used if you turn on debugging! But when debugging is turned off, it may not work!

Scenario 2: If you have the file. htaccess in your directory, then you add it in line 1 of the root directory, index. php:


<?php ini_set('session.cookie_domain',".domain.com");// Cross-domain access Session
//  Definition ThinkPHP Frame path 
define('THINK_PATH', '/ThinkPHP/');
// Define the project name and path 
define('APP_NAME', 'Www');
define( ' APP_PATH', '.');
//  Load the framework entry file 
require(THINK_PATH."/ThinkPHP.php");
// Instantiation 1 Web site application examples 
App::run();
?>

This method works whether debugging is started or not!


Related articles: