Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
66
Q:

php 7.2 - ini_set fails to set session variables ' PHP 7.2.0 and higher

Hey everyone,

I've been facing an issue with setting session variables using the `ini_set` function in PHP 7.2.0 and above. It seems like the function is not working as expected.

I have tried something like this:

```php
ini_set('session.save_handler', 'memcached');
ini_set('session.save_path', 'localhost:11211');
```

But when I check the `session.save_handler` and `session.save_path` variables using `phpinfo()`, they are still set to the default values.

I have also confirmed that the `ini_set` function is enabled and not disabled in the server's PHP configuration.

Has anyone else encountered a similar issue with `ini_set` and session variables in PHP 7.2.0 and higher? Is there any workaround or solution to this problem? I would appreciate any help or insights on this matter.

Thanks in advance!

All Replies

violette04

Hey everyone,

I encountered a similar issue with session variables in PHP 7.2.0, and although the `ini_set` function didn't work for me, I found a different solution that might help.

Instead of trying to set session variables using `ini_set`, I used the `session_start` function and modified the session variables directly in my code.

Here's an example:

php
session_start();
$_SESSION['save_handler'] = 'memcached';
$_SESSION['save_path'] = 'localhost:11211';


By setting the session variables directly in your code after calling `session_start`, you can bypass any restrictions imposed by `ini_set` and have more control over the session configuration.

However, please note that this approach might not be recommended in certain scenarios, especially if you have multiple files or scripts handling session data, as it can lead to inconsistent configuration.

I hope this alternative method helps you overcome the issue. If you have any further questions or need further assistance, feel free to ask.

glenda03

Hey everyone,

I encountered a similar issue with `ini_set` and session variables in PHP 7.2.0, and here's what worked for me. Instead of using `ini_set`, I used the `session_set_save_handler` function to modify the session variables.

First, ensure that you have the necessary extensions installed and enabled for your desired session save handler. For example, for memcached, make sure you have the Memcached extension installed and enabled in your PHP configuration.

Next, you can create a custom session handler class that implements the `SessionHandlerInterface` interface. Within this class, you can define the desired session save handler functionality, such as connecting to memcached and handling session data storage and retrieval.

Here's a basic example:

php
class CustomSessionHandler implements SessionHandlerInterface
{
// Implement your desired session save handler functionality here
}

// Set the custom session handler
$handler = new CustomSessionHandler();
session_set_save_handler($handler, true);

// Now you can modify session variables as needed


By using this approach, you have more control over the session handling process and can configure it to your requirements.

I hope this helps! If you have any further questions or need assistance, feel free to ask.

imelda.fay

Hey there,

I faced a similar issue with `ini_set` and session variables in PHP 7.2 recently. After extensive troubleshooting, I discovered that in PHP 7.2 and higher versions, the `ini_set` function does not allow modification of certain session variables for security reasons.

In particular, `ini_set` is blocked from modifying session-related variables such as `session.save_handler` and `session.save_path`. Instead, you'll need to update these settings directly in the `php.ini` file or using the `session_set_save_handler` function.

To change the session save handler, you can edit the `php.ini` file and locate the `session.save_handler` directive. Modify it to the desired handler, like `session.save_handler = memcached` in your case. Don't forget to restart your web server afterward.

For the session save path, you have two options. Either update the `php.ini` file and change `session.save_path` directly, or use `session_save_path` function in your PHP code before session start. For example:

php
session_save_path('localhost:11211');


By implementing these changes in the `php.ini` file, or using the appropriate functions, you should be able to set the session variables successfully.

I hope this helps! Let me know if you have any further questions or if there's anything else I can assist you with.

New to LearnPHP.org Community?

Join the community