Jump to content

Recommended Posts

Warning: session_set_cookie_params(): Cannot change session cookie parameters when session is active in C:\xampp\htdocs\test\includes\config.php on line 7

Warning: session_set_cookie_params(): Cannot change session cookie parameters when session is active in C:\xampp\htdocs\test\includes\config.php on line 7

The Page not loading...

Kindly help to fix this.. 

Link to comment
https://forums.phpfreaks.com/topic/314673-session_set_cookie_params-error/
Share on other sites

without seeing any code at all, I'm gonna guess that PHP is actually telling you that you cannot alter that setting from with a php session.  You may have to do this edit by actually updating the PHP.ini file itself.

Edited by ginerjm

My config file

    ob_start();
    if(!isset($_SESSION)) 
    { 
        session_start(); 
    } 
    
    session_set_cookie_params(0);
    set_time_limit(0);
    ini_set('post_max_size', '500000M');
    ini_set('max_execution_time', '3600');
    ini_set("memory_limit","2562M");

42 minutes ago, Dhivya said:

My config file

wherever you found this, forget you saw it.

here's everything that's bad about this -

  1. don't use output buffing unless you WANT to buffer output. by using it to make bad code work, you are hiding mistakes in the code and it will make finding and fixing problems harder.
  2. don't conditionally start a session. if you want to use sessions, simply, unconditionally start them once, in an initialization/configuration file.
  3. as already stated about the error, and stated in the documentation for session_set_cookie_params(), you must call this before the session_start.
  4. session_set_cookie_params(0) is only valid in php8, where that usage would set the session.cookie_lifetime to zero, but the default is already zero.
  5. set_time_limit(0) and  ini_set('max_execution_time','3600') set the same thing, so these are first setting the max_execution_time to zero (which you should never do since a programming mistake that causes a script to never end will require that you restart the web server to regain control), then sets it to 3600.
  6. you cannot set post_max_size in a php script, because the value is used by the server before a php script is ever invoked.
  • Great Answer 1
10 hours ago, gw1500se said:

Show your new code.

    session_set_cookie_params(0);
    ob_start();   

        session_start(); 

        //set_time_limit(0);

//    ini_set('post_max_size', '500000M');
    ini_set('max_execution_time', '3600');
    ini_set("memory_limit","2562M");

    global $config;
    $Localmode = true;

  1. You were advised to use the code tags before you even posted any code.  Now you have made 2 posts and didn't use the code tag button to input your code.  Why is that?
  2. You said you changed things as advised.  What exactly did you change?  You commented out two settings that have nothing to do with your problem. 
  3. Where is this code?  Is it in a function or file that is being included?  It appears to be code in a function, because you inject a global variable into function scope  "global $config".
    1. This suggests that you have a function that you are then including or calling somehow, but you only provided this snippet of code, which gives no context to what you are doing.
  4. If you "did what was advised" then why are you still using ob_start()???  Do you know what ob_start() does and what it is for?

You are wasting everyone's time by providing parts of your code, and not following directions.  The error indicates that your code is starting a session before the session_set_cookie_params() call is made.  I'm going to guess that this is because you are providing a small piece of your actual code, and the problem won't be found in the 7 lines of code you cherry picked.

 

 

Sorry for not updating the code completely.. Here it's  

<?php
	session_set_cookie_params(0);
	ob_start();

	

        session_start(); 

    

	//set_time_limit(0);

//	ini_set('post_max_size', '500000M');
	ini_set('max_execution_time', '3600');
	ini_set("memory_limit","2562M");

    global $config;
	$Localmode = true; // false means server
	//Check the database connection
	if($Localmode){

	
	$var = $_SERVER['DOCUMENT_ROOT'].'/';

	
	$config['SiteAirticalpath'] = "https://psexports.in/";
	$config['SiteGlobalPath']	= $config['SiteAirticalpath']."/";
	$config['SiteLocalPath']	= $var;
	$config['SiteImagePath']	= $var."images/";
	$config['SiteJsPath']	    = $var."js/";
 	$config['SiteClassPath']	= $var."includes/classes/";
	$config['SiteIncPath']	    = $var."includes/";
	$config['SiteAdminPath']	= $config['SiteGlobalPath']."admin/";
	$config['SiteUploadPath']	= $config['SiteLocalPath']."admin/Uploads/";

	//$config['SiteTitle']	  	= ":: Data Grid ::";

	############# DB  Config########################
	$config['DBHostName']	= "localhost";
	$config['DBUserName']	= "";
	$config['DBPassword']	= "";
	$config['DBName']		= "";

	}
	$config['Limit'] = 10;
	$config['today'] = date('Y-m-d');

?>

 

Edited by Dhivya
On 4/16/2022 at 4:27 AM, Dhivya said:
<?php
	global $config;

?>

 

 

In my prior reply, I stated to you that this line is not relevant.  This is not a function is it?  The global keyword is used to inject a global variable into a php function.  Unlike javascript, PHP functions have no visibility to global variables, so if you want to use a global variable in a function, then you use global $variable_name.

 

function sayHello() {
    global $name;
    echo "Hello $name";
}

$name = 'Dhivya';

sayHello();

//Hello Dhivya

 

The code you have shown doesn't do anything of value, so it's hard to understand how this code is being used by you.  All you are doing is setting a bunch of configuration settings and a few variables.  It looks like you cribbed this code from somewhere for some reason.  Let me assume that it is just a config.php file you plan on including in other scripts.  Then something like this would make more sense.

<?php
	session_set_cookie_params(0);
	session_start(); 
	//set_time_limit(0);
 	//ini_set('post_max_size', '500000M');
	ini_set('max_execution_time', '3600');
	ini_set("memory_limit","2562M");

	$config = [];
	$Localmode = true; // false means server
	//Check the database connection
	if ($Localmode) {
		$var = $_SERVER['DOCUMENT_ROOT'].'/';	
		$config['SiteAirticalpath'] = "https://psexports.in/";
		$config['SiteGlobalPath']	= $config['SiteAirticalpath']."/";
		$config['SiteLocalPath']	= $var;
		$config['SiteImagePath']	= $var."images/";
		$config['SiteJsPath']	    = $var."js/";
 		$config['SiteClassPath']	= $var."includes/classes/";
		$config['SiteIncPath']	    = $var."includes/";
		$config['SiteAdminPath']	= $config['SiteGlobalPath']."admin/";
		$config['SiteUploadPath']	= $config['SiteLocalPath']."admin/Uploads/";
		//$config['SiteTitle']	  	= ":: Data Grid ::";

		############# DB  Config########################
		$config['DBHostName']	= "localhost";
		$config['DBUserName']	= "";
		$config['DBPassword']	= "";
		$config['DBName']		= "";
	}
	$config['Limit'] = 10;
	$config['today'] = date('Y-m-d');

// Don't use an end tag!!!!

 

Things to note:

  • People keep asking you why you are using ob_start()?  I removed it because it's a complicated feature, and you have not indicated why you are using it in a config file, or what you are doing that requires it
  • Don't include a PHP end tag in your scripts, especially when they are includes or class definitions for other scripts

A helpful comment:

This is not a good idea:

$config['SiteAirticalpath'] = "https://psexports.in/";
$config['SiteGlobalPath']	= $config['SiteAirticalpath']."/";
//etc.

There are 2 types of paths you need in PHP.

  1. URL's
  2. File system paths

Some of these variables seem to be URL's and some seem to be file system paths.  For URL's you absolutely don't want to code in a domain.  Just use a relative path for this, and let the webserver fill in the domain. 

For file system paths, there is a more standard way of building variables rather than depending on $_SERVER['DOCUMENT_ROOT']

Let's assume that your "yoursite" directory is the project directory for you web app. 

Let's assume that this new file you are creating is named "config.php" and is in the yoursite/config directory.  

You don't want to use that variable, because typically you don't want your includes and classes in "web space" ie. under the webroot directory.  You want a structure more like this:

/
└── var/
    └── www/
        └── yoursite/
            ├── public (webroot)/
            │   ├── images
            │   ├── js
            │   └── css
            ├── config/
            │   └── config.php  
            ├── src/
            │   └── classes
            └── vendor

This is more of a standard structure used by variable PHP frameworks, and accounts for use of composer to integrate component libraries, and generate a class autoloader, which will negate the need for you to require classes yourself.  You create a composer.json file for your project and let composer create the autoload file that you then include in your project (perhaps in this config.php file).

 

Here is how you would make a "file system" path variable that points to "/var/www/yoursite" that you can then use to include or load files.

<?php
    // yoursite/config/config.php
	session_set_cookie_params(0);
	session_start(); 
	//set_time_limit(0);
 	//ini_set('post_max_size', '500000M');
	ini_set('max_execution_time', '3600');
	ini_set("memory_limit","2562M");

	$config = [];
	$Localmode = true; // false means server

	$PROJECT_DIR = DIRNAME(__DIR__);

	//Check the database connection
	if ($Localmode) {		
		// Url's
		$config['SiteImagePath']	= "/images/";
		$config['SiteJsPath']	    = "/js/";
		$config['SiteAdminPath']	= "/admin/";
		$config['SiteUploadPath']	= "/admin/Uploads/";
      
		// File Paths
		$config['SiteIncPath']	    = "$PROJECT_DIR/src/";
		$config['SiteClassPath']	= "$PROJECT_DIR/src/classes/";
		
		//$config['SiteTitle']	  	= ":: Data Grid ::";

		############# DB  Config########################
		$config['DBHostName']	= "localhost";
		$config['DBUserName']	= "";
		$config['DBPassword']	= "";
		$config['DBName']		= "";
	}
	$config['Limit'] = 10;
	$config['today'] = date('Y-m-d');

// Don't use an end tag!!!!

 

Files that include /config/config.php just need to know their relative path to it.  Once included you have the $PROJECT_DIR variable available to use to make file system paths.  Again file system paths are only important for PHP functions that require a file system path.  They are not urls!  Just use relative paths for all your url's.

Assume you have a yoursite/public/index.php file

<?php
//yoursite/public/index.php
require_once("../config/config.php");
$data = print_r($config);
?>

<h1>Configuration</h1>
<p>$PROJECT_DIR: <?= $PROJECT_DIR ?>

<h2>$config</h2>
<pre><?= $data ?></pre>

<a href="<?= $config['SiteAdminPath'] ?>">Admin</a>

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.