9

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?

1

1 Answer 1

18

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

Sign up to request clarification or add additional context in comments.

1 Comment

good code..flash message level also include this logic(like error,warning,notice etc) for improving

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.