Tutorials

Article Content:codeigniter redirect back to the previous page

codeigniter redirect back to the previous page

codeigniter redirect back to the previous page.

The biggest benefit from working as a freelancer web developer that you upgrade your knowledge every single day.

Clients take this task on their hands, so your turn to use the right technology on the right side.

This benefits may be big, medium or a simple task, but at all you will be refreshed.

For this I build this blog, to share knowledge with other developers even if it wasn't much bigger, the target here to benefit someone.

Ok, let’s go back to work, in this tutorial you will learn how to come back to the previous page you have been in but before this let me explain this case to understand why you want this tutorial.

Suppose you have a multi-language website ,as an example let's say it was e-commerce website, the default language was English ,but Visitor from India want to read product specifications with Indian language without leaving the product specifications page, for this case and same cases we use redirect back.

 

How to do this ?

We use  

$_SERVER['HTTP_REFERER']

which Returns the complete URL of the current page (not reliable because not all user-agents support it) and pass it to header() to redirect to the previous page, to use it follow these steps : 

Step one put this function in your custom helper

 



if ( ! function_exists('redirect_back'))
{
    function redirect_back()
    {
        if(isset($_SERVER['HTTP_REFERER']))
        {
            header('Location: '.$_SERVER['HTTP_REFERER']);
        }
        else
        {
            header('Location: http://'.$_SERVER['SERVER_NAME']);
        }
        exit;
    }

Step two: call this function in the right place, let’s assume it will be after delete record like this example

// Crud controller

Function create()
Function Update()
Function Read()

Function Delete()
{
   $this->load->model('mod1');

   // Pass the $id to the Delete() method
   $this->crud_model->row_delete($id);


   redirect_back();  
}

Hope it will help, don’t hesitate to ask questions



  • rod
    using the < back arrow in a datadriven codeigniter website under development, results is data entered in a form listing items for sale being totally lost. It sounds like you may know a solution. Is there a way to contact you.
    November 24, 2016
    Reply
    • admin
      [email protected]
      Hi, rod yes you can, contact me with Facebook chat but i will be available in Friday night. send me a messages there and when i will be a available i will contact you thanks
      November 24, 2016
      Reply
      • rod
        [email protected]
        I will not be here on Friday night. Contact me 24/7 anytime. Thank You
        November 24, 2016
        Reply

Leave a Reply

Your email address will not be published.


Notify me of followup comments via e-mail.
You can also Subscribe without commenting.