I am using Yii and the problem I am getting is with the Yii::app()->user->returnUrl. It always returns me to the index.php page.
How can I set its value to the page which requested the current page as I do not know from which page user has visited the current page?
You can use Yii::app()->request->urlReferrer to see where the user came from.
public function beforeAction()
{
Yii::app()->user->returnUrl = Yii::app()->request->urlReferrer;
return parent::beforeAction();
}
Be careful, if the user came from a 3rd party site, then this will redirect them away from your site.
For those using Yii2
Yii::$app->user->returnUrl = Yii::$app->request->referrer;
There is no such thing as: Yii::app()->user->urlReferrer
It should be: Yii::app()->request->urlReferrer or Yii::app()->request->requestUri (current page)
So try this:
Yii::app()->user->returnUrl = Yii::app()->user->urlReferrer;
Or this one (which I personally prefer):
Yii::app()->user->returnUrl = Yii::app()->request->requestUri;
I created a Yii extension to manage return URLs, you can find it here:
https://github.com/cornernote/yii-return-url#yii-returnurl
It's an improvement over the way Yii handles it because it stores the return url in the GET/POST data instead of the SESSION. This means that if you have multiple tabs open then each can have it's own return url.
You can use like this
$http = new CHttpRequest();
$referrer_url = $http->getUrlReferrer();
$this->redirect($referrer_url);
Hope this will help you
You can also do like that
Yii::app()->user->returnUrl = Yii::app()->request->urlReferrer;
$this->redirect(Yii::app()->user->returnUrl);
For those who still struggling with returning to previous page: You have to create a property in login model $referer, and at the time when you initialize your Login model you set this property to Yii::$app->request->referrer. You have to go this way because after you submit your form your referrer does change to current page, and since it is null, it return you to index action. So the way how to pass the return URL is to store it in the hidden field at the form, so when you submit the form you have this URL loaded and you can perform action to return $this->goBack($form->referer ? $form->referer : null).
Here is the code:
Login model:
public $referer;
Controller action:
$model = new Login();
$model->referer = Yii::$app->request->referrer;
...
if($model->load(Yii::$app->request->post()){
...
if($model->save()){
return $this->goBack((($model->referer) ? $model->referer : null));
}
}
View login.php:
<?= $form->field($model, 'referer')->hiddenInput()->label(false) ?>
Related
Im using the Laravel base Authentication, when unregistered user attempt to access a link, for example:
http://myapp.com/financial/?email=john#web.com
It is redirect to http://myapp.com/login because the user is not registered yet.
My question is: how do i get the url parameter email of the original link (http://myapp.com/financial/?email=john#web.com) of the user tried access in my login page?
Obs: im using Laravel 5.7
In your controller simply try like that:
$email = request()->input('email'); // null if not set
// do your things
// then redirect
https://laravel.com/docs/5.7/requests#accessing-the-request
If you want to get the URL after the redirect, you can use the referer header of your request. It will contain the address that was being accessed before the redirect.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer
The Referer request header contains the address of the previous web
page from which a link to the currently requested page was followed.
The Referer header allows servers to identify where people are
visiting them from and may use that data for analytics, logging, or
optimized caching, for example.
you can get it easily by accessing $_GET
$email="";
if(isset($_GET['email']) && !empty($_GET['email'])){
$email=$_GET['email'];
}
Laravel provides a function as well:
$email = Input::get('email');
you can use Request object like \Request::segment(1)
or using Input object like Input::get('email', false);
Look in App\Http\Middleware\Authenticate::redirectTo($request). You can customize this to suit your situation. (Laravel docs for Middleware) For example:
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
if ($request->has('email')) {
return route('login', ['email' => $request->email]);
} else {
return route('login');
}
}
}
I want to pass data to a page to which I redirect my visitor.
is it possible or another way to pass data to view in redirect method.
I have a contact page to which user input some fields and if form validation goes wrong then redirect user again to contact page with the data which he enter in contact form. when he redirect again to contact page then I want to show that data. Any good solution for this?
I use this function
redirect('/contact');
There are two possible ways, you can pass values while redirecting the user.
1. Using Sessions:
Use session to pass data while redirecting. There is a special method in CodeIgniter to do it called set_flashdata
$this->session->set_flashdata('in',1);
redirect("home/index");
Now you may get in your index controller like
function index()
{
$in = $this->session->flashdata('in');
if($in==1)
{
}
}
Remember this data will available only for redirect and lost on next
page request. If you need stable data then you can use URL with
parameter & GET $this->input->get('param1')
2. Using Get values
So in the controller you can have in one function :
$value=1;
redirect(base_url()."home/index/".$value);
And in the target function you can access the $value value like this :
$value= $this->uri->segment(3);
if(!is_numeric($value))
{
redirect();
}else{
if($value == 1){
}
}
I put segment(3) because in your example $value is after 2 dashes. But if you have for example this link structure: www.mydomain.com/subdomain/home/index/$value you'll have to use segment(4).
Hope it helps!
If you want pass data using 'Codeigniter Flashdata' concept. It's simple.
Step 1: Load session library at your controller class.
$this->load->library('session');
Step 2: Add flash data.
$this->session->set_flashdata('item','Test values');
Here, your redirection code. After redirection if you want that 'Flashdata value' use below code.
$this->session->flashdata('item');
Print the above code you will get the data.
You can do simply like this
$array = ['name' => 'abc', 'email' => 'abc#gmail.com'];
$this->session->set_flashdata('arrayData', $array);
// You can use in view like this
$array = $this->session->flashdata('arrayData');
echo $msg['name']; // Output : abc
I'd like to ask you how can I instead of $this->load->view('some_view.php') at the end of controller code, return user to page from where he invoked controller method? Simple return statement is not working.
ie.
public function someMethod($IDCustomer) {
$this->Some_modal->persist($IDCustomer);
// how to return to previous page instead of line after?
// i've used $this->load->view('someView.php');
}
This should help http://www.codeigniter.com/user_guide/libraries/user_agent.html
$this->load->library('user_agent');
if ($this->agent->is_referral())
{
echo $this->agent->referrer();
}
or straight PHP:
redirect($_SERVER['HTTP_REFERER']);
I've found answer on some thread.
In the page that you want to go back to you can do:
$this->session->set_userdata('referred_from', current_url());
Then redirect back to that page
$referred_from = $this->session->userdata('referred_from');
redirect($referred_from, 'refresh');
Tried return redirect()->to($_SERVER['HTTP_REFERER']); , this would work well.
In Codeigniter 4 You can use previous_url() function from url helper
find more https://codeigniter.com/user_guide/helpers/url_helper.html
I've tried header('location:'.$_SERVER['HTTP_REFERER']); and it's working quite well.
Just a one-liner plain old PHP code.
Use the REDIRECT_QUERY_STRING alternative of HTTP_REFERRER:
// set in session redirect back URL in your common is_logged_in function if user is not logged in
$CI->session->set_userdata('redirect_back', $_SERVER['REDIRECT_QUERY_STRING']);
// below code user after successful login in auth.php library
if($this->ci->session->userdata('redirect_back')){
$redirectBackUrl = $this->ci->session->userdata('redirect_back');
$this->ci->session->unset_userdata('redirect_back');
redirect(base_url() . $redirectBackUrl);
}
I have a problem with a redirect in Zend Framework 2.
when a user enters a url like http//mysite.test/lorem, he will be redirected to http//mysite.test/news/view/lorem-ipsum.
the code:
$redirecttable = $sm->get('Redirect\Model\RedirectTable');
$route = $redirecttable->getRedirectByRoute($this->params()->fromRoute('page'));
if($route) {
// $route->getToroute() returns "news/view/Lorem-Ipsum"
return $this->redirect()->toUrl( $route->getToroute() );
}
Am I making a stupid mistake here?
The toUrl method redirect to a complete url, but you're sending part of url. You can use $this->redirect()->toRoute($route,$params) for redirecting to a route. reference
If you want to use toUrl method, try this code:
return $this->redirect()->toUrl( $this->getRequest()->getUri()->toString().$route->getToroute() );
Use toRoute() instead of toUrl():
return $this->redirect()->toRoute($route->getToroute())
In Zend Framework I have an Action Helper that loads a login form on most pages. This happens in the preDispatch() method of the Helper and I want to setAction() on the form so that it posts back to the current URL.
What's the best way to access the current URL / route from within the Action Helper? Access the Request (via the Action Controller), then pull then getActionName() and getControllerName(), and concatenate them with baseURL()?
Is there a simpler way? (Set action requires the URI string as a parameter).
Thanks!
You can do as #Elie suggested. However, if you want to use ZF methods for this, you can have a look at this:
$request = Zend_Controller_Front::getInstance()->getRequest();
echo $request->getHeader('referer'); // referer's address
echo $request->getRequestUri(); // current address
I found that I didn't need to access the current URL / route from within the Action Helper. By leaving the form action blank, it automatically posts to the current URL. Perfect.
If I understand correctly, when the user logs in, you want to send them back to the page where they came from. The code I use to do this is:
// the user has come from a particular page - send them back
if($_SERVER['HTTP_REFERER']) {
$this->_redirect($_SERVER['HTTP_REFERER']);
} else {
// the user has come from the home page, or this page
$this->_redirect('/');
}
which is located in the login action (i.e. LoginController->loginAction()).