Проверить Сессии в массив?

Что я видел в Windows 8.1, так это то, что когда действие крышки изменяется для схемы питания, тогда эта схема питания должна быть как активной, так и предпочтительной схемой питания. Схема активной мощности может быть установлена ​​PowerCfg, и предпочтительная схема мощности может быть установлена ​​реестром.

Вот сценарий Powershell для их изменения и действия с крышкой:

#Enable High performance
$powerScheme = "High performance"

#Find selected power scheme guid
$guidRegex = "(\{){0,1}[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}(\}){0,1}"
[regex]$regex = $guidRegex
$guid = ($regex.Matches((PowerCfg /LIST | where {$_ -like "*$powerScheme*"}).ToString())).Value

#Change preferred scheme
$regGuid = "{025A5937-A6BE-4686-A844-36FE4BEC8B6D}"
$currentPreferredScheme = Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\ControlPanel\NameSpace\$regGuid -Name PreferredPlan 
if ($currentPreferredScheme.PreferredPlan -ne $guid) {
    Set-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\ControlPanel\NameSpace\$regGuid -Name PreferredPlan -Value $guid
    Write-Host -ForegroundColor Green "Preferred scheme successfully changed. Preferred scheme is now '$powerScheme'." 
} else {
    Write-Host -ForegroundColor Yellow "Preferred scheme does not need to be changed. Preferred scheme is '$powerScheme'." 
}

#Change active scheme
$currentActiveScheme = PowerCfg /GETACTIVESCHEME
if ($currentActiveScheme | where {$_ -notlike "*$guid*"}) {
    PowerCfg /SETACTIVE $guid
    Write-Host -ForegroundColor Green "Power scheme successfully changed. Current scheme is now '$powerScheme'." 
} else {
    Write-Host -ForegroundColor Yellow "Power scheme does not need to be changed. Current scheme is '$powerScheme'." 
}

#Do not sleep when closing lid on AC
PowerCfg /SETACVALUEINDEX $guid SUB_BUTTONS LIDACTION 000
Write-Host -ForegroundColor Green "No action when closing lid on AC."
1
задан Adam Ramadhan 27 July 2010 в 09:18
поделиться

3 ответа

Поскольку это домашнее задание, решение вы не получите. Однако вы на правильном пути. explode() его по разделителю. Вы можете перебирать их с помощью foreach и использовать empty() для проверки, установлены ли они. Вы можете получить доступ к сессиям как $_SESSION[$key]. Сохраните массив тех, которые совпадают.

2
ответ дан 2 September 2019 в 22:42
поделиться
function CheckSession($string){
    $all_sessions_exist = true; #this will change if one of the session keys does not exist
    $keys = explode(',', $string); #get an array of session keys
    foreach($keys as $key){
        if(isset($_SESSION[$key])) {
            if(!empty($_SESSION[$key])) 
                echo '$_SESSION['.$key.'] is set and contains "'.$_SESSION[$key].'".'; #existing non-empty session key
            else echo '$_SESSION['.$key.'] is set and is empty.' ;
        }else {
             echo '$_SESSION['.$key.'] is not set.'; #key does not exist
             $all_sessions_exist = false; #this will determine if all session exist
        }

        echo '<br />'; #formatting the output
    }

    return $all_sessions_exist; 
}
0
ответ дан 2 September 2019 в 22:42
поделиться

Просто очистите вашу функцию

function CheckSession($sessions)
{
    $session = explode(',',$sessions);
    $return = array();
    foreach ($session as $s)
    {
        $return[$s] = (isset($_SESSION[$s]) && !empty($_SESSION[$s]) ? true : false)
    }
    return $return;
}

$sessions['lefthand'] = 'apple';
$sessions['righthand'] = '';
$sessions['head'] = 'hat';
$sessions['cloth'] = '';
$sessions['pants'] = '';

И часть проверки

// here is the homework function
$Results = CheckSession('lefthand,righthand,head,cloth,pants');

Редактировать

//$value1= false; // Commented out so does not get set
$value2= false; //Still set to a bool

error_reporting(E_ALL);

empty($value1); // returns false but still strikes E_NOTICE ERROR
empty($value2); // returns false with no error

Обратите внимание, что пустой не вызывает ошибки, но этот пример повлияет на многие другие функции php .

0
ответ дан 2 September 2019 в 22:42
поделиться
Другие вопросы по тегам:

Похожие вопросы: