I am using these variables in my site:
$_SESSION["domain.com"]["var1"]
$_SESSION["domain.com"]["var2"]
$_SESSION["domain.com"]["var3"]
and so on... how can i set all of these variables to a certain value at once?
Are you just trying to set them all at the same time? Just set ['domain.com']
to an array.
$_SESSION['domain.com'] = array(
'var1' => 'one',
'var2' => 'two',
'var3' => 'three',
);
Write a simple nested loop:
foreach ($_SESSION as &$vars) {
foreach ($vars as &$value) {
$value = $default_value;
}
}
I use reference variables so the loop can modify the elements directly, instead of having to assign to $_SESSION[$domain][$variable]
.
Here is some sample code to get you up and running:
$variables=[];
$setTo=1;
foreach($_SESSION['domain.com'] as $key=>$v){
$variables[]=$key;
}
$prefix='$_SESSION["domain.com"]["';
$eval=$prefix.implode('"]='.$prefix,$variables).'"]='.$setTo.";";
eval($eval);
Perfect for production use.