Keep displaying previous URL to a page in Lithium php framework

Here is the php code to cache the previous access URL (for example an URL of a search request) so that you want to display it again whenever the users go back to the page.

Activate Session in Lithium

// file: app/config/bootstrap.php
// uncomment this
require __DIR__ . '/bootstrap/session.php';

// file: app/config/bootstrap/session.php
use lithium\storage\Session;
Session::config(array(
	'default' => array('adapter' => 'Php')
));

The below code can be put in, for example, your controllers for redirecting to the previous saved URL.

        $protocol = strtolower($this->request->env('SERVER_PROTOCOL'));
        $protocol = substr($protocol, 0, strpos($protocol, "/")) . "://";
        $url = $protocol . $this->request->env('HTTP_HOST')
                         .$this->request->env('REQUEST_URI');
        $referer = $this->request->env('HTTP_REFERER');
        $baseURL = $protocol . $this->request->env('HTTP_HOST') . "/" . $this->request->url;
        if ($url == $baseURL || substr($referer, 0, strlen($baseURL)) != $baseURL) {
            $searchURL = Session::read('Files.url', array());
            if ($searchURL != '' && $url != $searchURL) {
                $this->redirect($searchURL);
            }
        } else {
            Session::write('Files.url', $url, array());
        }

Have fun then!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s