Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter like flashdata in core php

Tags:

php

session

is there any way to create flash session data like in codeigniter,
i want to create it in core php.

I don't want to use GET method, passing variable with url makes problem in my application.
so, how can i do this?

like image 822
Kartik it Avatar asked Aug 28 '12 05:08

Kartik it


People also ask

How to display flashdata in CodeIgniter?

Retrieve Flashdata$this->session->flashdata('item'); If you do not pass any argument, then you can get an array with the same function.

How set multiple Flashdata in codeigniter?

codeigniter session set flashdata $this->session->set_flashdata('message', 'Message you want to set'); Here 'message' is identifier for access data in view. You can Set more than one message by just changing identifier.


1 Answers

Its pretty easy to create a flash message class with PHP sessions.

class FlashMessage {

    public static function render() {
        if (!isset($_SESSION['messages'])) {
            return null;
        }
        $messages = $_SESSION['messages'];
        unset($_SESSION['messages']);
        return implode('<br/>', $messages);
    }

    public static function add($message) {
        if (!isset($_SESSION['messages'])) {
            $_SESSION['messages'] = array();
        }
        $_SESSION['messages'][] = $message;
    }

}

Make sure you are calling session_start() first. Then you can add messages using FlashMessage::add('...');

Then if you redirect, you can render the messages next time you render a page echo FlashMessage::render(). Which will also clear the messages.

See http://php.net/manual/en/features.sessions.php

like image 94
Petah Avatar answered Oct 18 '22 03:10

Petah