Welcome Guest, Not a member yet? Register   Sign In
referrer solution (previous uri)

Hi All!

I have seen a lot of people using http_referer for redirecting to a previous page. But I have also read that this is not reliable, as it is not always set and can easily be injected with fake information by the client.

Needing a solution I have come up with this.

I have extended the CI_URI class by creating a MY_Uri.php

Copied the _reindex_segments method form the original class and added it to MY_Uri.php
public function _reindex_segments() {
    // Call the the referer method
    array_unshift($this->segments, NULL);
    array_unshift($this->rsegments, NULL);

private function set_referer() {
    // Set the base url
    if (isset($_SERVER['HTTP_HOST'])) {
        $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
        $base_url .= '://'. $_SERVER['HTTP_HOST'];
        $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);

    // Set the current url
    $url = rtrim($base_url,'/').$this->uri_string.'.html';
    // Check if referer has been set, if so, set it as previous url
    // If it has not been set it means this is the first page call
    if(array_key_exists('referer', $_SESSION)) {
        // Check that the current url is not the same as the previously set referer
        // We do this incase the current page is refreshed
        if($url !== $_SESSION['referer']) {
            $_SESSION['prev_url'] = $_SESSION['referer'];
    // Set the referer url
    $_SESSION['referrer'] = $url;

Now if I want to return to the previous page I can just call
Is this a solid solution, or does anybody have a better and or more secure solution?


Theme © iAndrew 2016 - Forum software by © MyBB