flash_set, flash_get PHP?

2 Cevap php

so i got told in this question: http://stackoverflow.com/questions/2291413/php-javascript-passing-message-to-another-page

flash_set ve flash_get kullanmak için, denilen kavram "flaş Raylar" .. Şimdi bu fonksiyonu php bunları kullanabilir? Gerçekten php kütüphanesi sitede onları bulmak, çok emin değil im edebilirsiniz ..

2 Cevap

Eğer $ _SESSION sonraki sayfa isteği yanıp istediğiniz mesajları saklayabilirsiniz. Ben yöntemler raylar çalışmak tam olarak nasıl bilmiyorum ama umarım bu iki fonksiyon kullanım olabilir:

function flash_get()
{
    // If there are any messages in the queue
    if(isset($_SESSION['flashMessages']))
    {
        // Fetch the message queue
        $messages = $_SESSION['flashMessages'];

        // Empty out the message queue
        unset($_SESSION['flashMessages']);

        return $messages;
    }

    // No messages so just return an empty array
    return array();
}

function flash_set($message)
{
    // If the queue is currently empty we need to create an array
    if(!isset($_SESSION['flashMessages'])) {
        $_SESSION['flashMessages'] = array();
    }

    // Fetch the current list of messages and append the new one to the end
    $messages = $_SESSION['flashMessages'];
    $messages[] = $message;

    // Store the message queue back in the session
    $_SESSION['flashMessages'] = $messages;
}

Sadece () depolamak ve flash_get istediğiniz mesaj ile flash_set () diyorsun, sana bu diziyi geri vermek ve bir sonraki sayfa isteği üzerine kuyruğunu floş olacaktır.

Eğer çalışmak bu yöntemleri için her sayfa isteği ile session_start() çağrı emin olmak için de gerekir.

Bunu başarmak için PHP hiçbir yerleşik bir işlevi var, ama (raylar üzerinde esinlenerek) CakePHP gibi bazı çerçeveler kullanırsanız, oldukça basit bulacaksınız:

// In the controller
$this->Session->setFlash('message to flash');

// In the view
$session->flash();

Ben diğer bazı çerçeveler bu örtülü bahis.