redirect to previous page - php

so here i am with "maybe another stupid question"
so here i want to ask how to redirect to previous page if the case is somehow like this:
i have view:
a. home_view b. about_view c. contact_view
then i have login view that (now currently after i login it'll redirect me into the home_view)
I want if i access login_view from about_view then the redirect function will redirect me to about_view, or if i access login_view from contact_view then the redirect function will redirect me to contact_view and so on. How to make redirect function to the previous page(not a single page) ? Maybe some tricks using php? or codeigniter itself?

It's simple when you have login and you go to login_view just set default redirect to about_view or contact_view when had login.
But if you want to redirect to previous page you have to trick with session must set session ex : $SESSION['last_view'] = $yourlastview;. $yourlastview can be set when you in about_view or contact_view or another view and when you go to login_view just check that $SESSION['last_view'] and redirect to that view.
Regard's
Jefri

You can use query string to solve this problem.
Like when you call login script from about us page access url as login.php?page=1 where page=1 for aboutus page similarly you can page=2 for contact us page. At login page you can get the redirect page by the query string can redirect to the respective after successfully login using header function.
e.g. Header('Location:about.php');

just use redirect($_SERVER['HTTP_REFERER'],'refresh')

This works.
$this->load->library('user_agent');
redirect($this->agent->referrer());

Related

Codeigniter Redirect to desired url after user login

I have a website with Codeigniter 3 framework.
There is a login page as www.example.com/login
If author login to the panel they are redirecting to main page www.example.com/author/feed.
There are many pages in Author controller such as my-profile, edit-profile, reset-password etc.
If author wants to access www.example.com/author/my-profile they are redirected to www.example.com/login, after login they are redirected to to main page www.example.com/author/feed.
But I want them to redirect to www.example.com/author/my-profile.
What function I need to use in my code??
You can use the helper function redirect(). You can learn more here.
// after login
redirect('/author/my-profile');
You need to save your desired route in session before redirecting to login page like this:
$this->session->set_userdata('page', base_url('my-profile'));
When a user logs in, you can check if page is stored in session, otherwise redirect to base url:
$pageLink = base_url();
if ($this->session->userdata('page')) {
$pageLink = $this->session->userdata('page');
$this->session->unset_userdata('page');
}
redirect($pageLink);

PHP action logout alternative URL fix

I have created a PHP password protected site with log-in page. I am using the following link for logging out.
<a href="admin.php?action=logout"?>Log out</a>
The problem is it redirects back to the index.php, but instead of showing my URL as EXAMPLE.com it shows my URL as EXAMPLE.com/index.php, which is undesirable. How can I accomplish a log-out and go back to the clean, desired url.
After logout redirect using the following command:
header("Location:http://www.example.com/login.php");
You can also do it by javascript in client side by using:
window.location='http://example.com/login.php'
In logout page you just add this redirect at the end of code
header("Location:http://www.example.com/");
This will redirect to the same index page with url example.com, but not show /index in url
Don't add
header("Location:http://www.example.com/index");
It will show example.com/index in url

Javascript location reload without get perimeter

How can I make "location.reload()" on php file without get perimeter ?
Here is an example..
Let's say I have PHP, and at the right top, I have logout button.
When user press that button, PHP will automatically re-direct to "index.php?logout" and after that, i will logout (unset cookie and other) and return ( javascript location.reload() ) to main page (login page)
Problem that I have is, when php return to login page after logout, then url is still "index.php?logout", so when user login back with "index.php?logout" page, it will automatically logout (because of ?logout perimeter on url)..
So how can I tackle this problem ? Anybody got any idea ?
I hope you all understand what i'm trying to tell you all about..
Thanks for reading this..
use self.location.reload instead of location.reload. Because location.reload reloads the current page. You can simple use
self.location.reload = index.php
Or you can remove query paramter from your url like
var url = 'index.php?logout'
url=url.split("?")[0];
location.reload =url
location.reload() reloads the current page with the post data,you should use window.location.href in case of login/logout
When user press that button, PHP will automatically re-direct to "index.php?logout" and >after that, i will logout (unset cookie and other) and return ( javascript location.reload() >) to main page (login page)
all these can be implemented in server side.
// LOGOUT
if(isset($_GET['logout'])){
//logout code
}
header('location: www.yoursite.com/login') //or whatever the url is
exit;
you re-direct to login page and save page url that request for login that is index.php?logout for re-direct back you must check perimeter logout if exist remove it from return url for example
//replace parent::$Patch with your root url like "http://localhost/myapp/"
if(!parent::$Patch.'admin/login.php'=='http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']){//check if you are in login.php page didn't redirect to login.php
if(!#header('Location:'.parent::$Patch.'admin/login.php?url='.rawurlencode(str_replace("logout","",'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])))){
//if header error echo javaScript code for re-direct
?>
<!DOCTYPE HTML>
<html>
<head>
<script language="javascript">
var LoadP = <?php print "'".parent::$Patch.'admin/login.php?url='.rawurlencode(str_replace("logout","",'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']))."'"?>;
self.location = LoadP;
</script></head></html>
<?php
}
exit();
}
}
this code re-direct user to login page and keep return url of page. if logout exist inside of return url remove it
i hope it can help you

Symfony execute Action and stay on page

I want to execute a Action and stay on the same page.
I created the link to eh action like this:
<?php echo(link_to('Add to Watchlist', 'housing/addToWatchlist')) ?>
which executes this action with a redirect
echo('ADDING TO THA WATCHLIST');
$referrer = $request->getReferer();
return $this->redirect($referrer);
as suggested here: symfony link to change language and stay on the page
This solution works but unnecessary reloads the page, which may be necessary to change the language but not to add an item to a Watchlist.
Without reloading the page you need to use Javascript and an Ajax approach.
http://www.symfony-project.org/jobeet/1_4/Propel/en/18
Also check out link_to_remote.

Can I redirect from a view in cakephp

I have a button on my home page which leads to a signup/login page. The signup/login page is just a page in views/pages/signup.ctp and as such doesn't have an action associated with it.
I want it so if a user is already logged in when they're on the homepage and they click my signup/login button, instead of taking them to the signup/login page it redirects them to another action. But since the signip/login page doesn't have an action I don't know where to do the check and redirect. Is there a way of checking if a persons logged in in the signup.ctp view? and redirect them from there?
Thats probably miles away from reality but I'm very confused. Any help appreciated. Cheers :)
Create an action for signup... and you can do the verification there. I don't think it's a good practice to put redirects in views. You can place the action in the Homepage controller for example...
Even if it was pretty to put the redirect in the view, you would need to set a variable (in an action) to check if user is logged in or not. :)
you can put a different link for the button in your home page..
i.e. depending on the user's status (logged in or not), you will choose which link to associate with the button..
I don't know if you're using the Auth component..
if so, in your home page's template you can do something like:
<?php
if ($session.read('Auth.User')) {
// the user is logged in
// put the link to the other action
}
else {
// the user isn't logged in
// put the link to the signup/login page
}
?>
this way, you will not need to redirect the user...
hope this helps..
good luck with your development...
Do this in your ExampleController for the specific view like "example.ctp"
public function example(){
if ($session.read('Auth.User')) {
this->redirect(['action' => 'exampleview']);
}
}
Hope that it will solve your purpose.
You can use
header ("Location: ".$this->Html->url('/'));
You can also use
header ("Location: ".$this->Html->url(array('controller'=>'Home','action'=>'index')));
for using controller and action in cakephp

Categories