Welcome Guest, Not a member yet? Register   Sign In
redirect to previous page?
#1

[eluser]Arun Joshi[/eluser]
Can I maintain the previous page url in CI?

ie,
Now am at

Code:
http://localhost/technoneeds/posts/category/3


I click a link on this page which redirected to page

Code:
http://localhost/technoneeds/posts/editpost/29


In this page if the user have no essential credential for editing a post, I want to redirect him to the previous page ,ie http://localhost/technoneeds/posts/category/3.

Is there any CI method for this?
#2

[eluser]Dam1an[/eluser]
This is not built into the core (AFAIK) but is easy to add yourself
There is a library which does that here (but I've never used it, so can't recommend it)

I personally just set the uri_string using uri_string() (part of the URI helper) into the userdata. I can then just redirect to that value when I need to
#3

[eluser]Evil Wizard[/eluser]
If you extend the main CI controller then you could store the referer in the session
Code:
$this->session->set_userdata('refered_from', $_SERVER['HTTP_REFERER']);
and then after you have checked the users credentials and found them without the privileges to edit the post you can
Code:
redirect($this->session->userdata('refered_from');
hope that helps
#4

[eluser]tison[/eluser]
Yes, thank you Dam1an. Your method worked great for redirecting or returning to the previous page, or last page. (think i got all the keywords there for the next searchers).

In any case, I added this code to my controllers:
Code:
$this->session->set_userdata('previous_page', uri_string());

And saved the previous page to my page's form, then after saving redirected users (in my second controller):

Code:
$previous_page = $this->session->userdata('previous_page');
if(isset($previous_page) && $previous_page != ''){
    redirect(site_url($previous_page));
}
else $this->index($msg);

** We had to check $previous_page != '' as in the version I'm running, unset_userdata doesn't remove the item, it sets it to empty string ***
#5

[eluser]costicanu[/eluser]
Evil Wizard your idea works. Dam1an that uri_string is very strange when pressing "Submit" button. Strange behaviour, instead of editPage/verify.html (last visited page), it's editPage/edit/img/0.png , so, use $this->session->set_userdata('refered_from', $_SERVER['HTTP_REFERER']);
#6

[eluser]InsiteFX[/eluser]
Did you read the date's of the posts?

InsiteFX
#7

[eluser]costicanu[/eluser]
I don't care about dates. I had that problem and Evil Wizard had the solution!
#8

[eluser]InsiteFX[/eluser]
Ok, and what happen's $_SERVER['HTTP_REFERER']); returns NULL? 404 PAGE NOT FOUND!

Here is the correct way to code it!
Code:
if (isset($_SERVER['HTTP_REFERER']))
{
    $this->session->set_userdata('prev_url', $_SERVER['HTTP_REFERER']);
}
else
{
    $this->session->set_userdata('prev_url', base_url());
}

InsiteFX
#9

[eluser]Unknown[/eluser]
Just put this in the controller function

Code:
redirect($_SERVER['HTTP_REFERER']);
#10

[eluser]35mm[/eluser]
[quote author="stenquist" date="1380487785"]Just put this in the controller function

Code:
redirect($_SERVER['HTTP_REFERER']);
[/quote]
And for users that block or don't have headers set? It would be best to use a cookie/flash session so that any part of your system that needs it can use it - fr example to return to previous page after user logs in etc.




Theme © iAndrew 2016 - Forum software by © MyBB