lithium\storage\Session::check PHP Method

check() public static method

Checks if a session key is set in any adapter, or if a particular adapter configuration is specified (via 'name' in $options), only that configuration is checked.
public static check ( string $key, array $options = [] ) : boolean
$key string The session key to check.
$options array Optional parameters that this method accepts.
return boolean
    public static function check($key, array $options = array())
    {
        $defaults = array('name' => null, 'strategies' => true);
        $options += $defaults;
        $methods = array();
        if ($name = $options['name']) {
            $methods = array($name => static::adapter($name)->check($key, $options));
        } else {
            foreach (static::$_configurations as $name => $config) {
                if ($method = static::adapter($name)->check($key, $options)) {
                    $methods[$name] = $method;
                }
            }
        }
        $params = compact('key', 'options');
        $result = false;
        foreach ($methods as $name => $method) {
            $settings = static::_config($name);
            $filters = $settings['filters'];
            $result = static::_filter(__FUNCTION__, $params, $method, $filters) || $result;
        }
        if ($options['strategies']) {
            $options += array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__);
            return static::applyStrategies(__FUNCTION__, $name, $result, $options);
        }
        return $result;
    }

Usage Example

Example #1
0
 public function trychar($char)
 {
     // do we have an active game session?
     if (!Session::check("game_id", array('name' => 'default'))) {
         return $this->redirect('games::index');
     }
     // state: 0 = in-game, 1 = won, 2 = lost, 3 = already won, 4 = already lost
     $game = Games::find('first', array('conditions' => array('id' => Session::read('game_id'))));
     $word = Words::find('first', array('conditions' => array('id' => $game->word_id)));
     $resp = array("success" => 0, "wrong_tries_left" => $game->max_wrong_tries - $game->wrong_tries, "placeholders" => Games::getPlaceholders(), "image" => Games::getImage(), "message" => "", "state" => 0, "word" => "");
     // game already won
     if ($game->state == 1) {
         $resp['message'] = "You already won!";
         $resp['state'] = 3;
         return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
         // game already lost
     } elseif ($game->state == 2) {
         $resp['message'] = "You already lost!";
         $resp['state'] = 4;
         return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
     }
     // ist der gesendete buchstabe erlaubt?
     if (!Games::isValidChar($char)) {
         $resp['success'] = 0;
         $resp['message'] = "Non-valid char used.";
         return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
         // falls der gesendete buchstabe schonmal gesendet wurde
     } elseif (stripos($game->input_text, $char) !== false) {
         $resp['success'] = 0;
         $resp['message'] = "Char was already used.";
         return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
         // buchstabe ist valid und wurde nicht schon gesendet
     } else {
         $game->input_text .= $char;
         $game->save();
         $resp['success'] = 1;
         $resp['placeholders'] = Games::getPlaceholders();
         // buchstabe kommt in gesuchtem wort vor
         if (!(stripos($word->value, $char) === false) && stripos($word->value, $char) >= 0) {
             $resp['image'] = Games::getImage();
             // alle zeichen erraten, gewonnen
             // WIN!
             if (stripos(Games::getPlaceholders(), self::PLACEHOLDER) === false) {
                 $game->state = 1;
                 // won
                 $game->save();
                 $resp['state'] = 1;
                 // won
                 $resp['message'] = "You won! Play a new game?";
                 $resp['wrong_tries_left'] = $game->max_wrong_tries - $game->wrong_tries;
                 return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
                 // buchstabe stimmt, aber noch nicht alles erraten
             } else {
                 $resp['wrong_tries_left'] = $game->max_wrong_tries - $game->wrong_tries;
                 $resp['message'] = "Nice! Guess the next char!";
                 return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
             }
             // buchstabe kommt NICHT vor
         } else {
             $game->wrong_tries++;
             $game->save();
             $resp['image'] = Games::getImage();
             // hat der benutzer die maximale anzahl versuche überschritten?
             // LOOSE!
             if ($game->wrong_tries >= $game->max_wrong_tries) {
                 $resp['wrong_tries_left'] = 0;
                 $resp['message'] = "Out of tries. New Game?";
                 $resp['state'] = 2;
                 // lost
                 $game->state = 2;
                 // lost
                 $resp['word'] = $word->value;
                 $game->save();
                 return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
                 // falscher buchstaben eingegeben, benutzer hat weiteren versuch
             } else {
                 $resp['wrong_tries_left'] = $game->max_wrong_tries - $game->wrong_tries;
                 $resp['message'] = "Nope. Try again!";
                 return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
             }
         }
     }
     return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false));
 }
All Usage Examples Of lithium\storage\Session::check