Redirect to the previous page when logging in/out
Redirect to the previous page when logging in/out

--- a/include/controller/Controller_Login.class.php
+++ b/include/controller/Controller_Login.class.php
@@ -77,7 +77,10 @@
 		if (!empty($_SESSION['gitphpuser'])) {
 			$user = $this->userList->GetUser($_SESSION['gitphpuser']);
 			if ($user) {
-				$this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true);
+				if (!empty($this->params['redirect']))
+					$this->headers[] = 'Location: ' . $this->params['redirect'];
+				else
+					$this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true);
 				$this->loginSuccess = true;
 			} else {
 				unset($_SESSION['gitphpuser']);
@@ -88,7 +91,10 @@
 			$user = $this->userList->GetUser($this->params['username']);
 			if ($user && ($this->params['password'] === $user->GetPassword())) {
 				$_SESSION['gitphpuser'] = $user->GetUsername();
-				$this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true);
+				if (!empty($this->params['redirect']))
+					$this->headers[] = 'Location: ' . $this->params['redirect'];
+				else
+					$this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true);
 				$this->loginSuccess = true;
 			} else {
 				$this->loginSuccess = false;
@@ -103,6 +109,11 @@
 	{
 		if ($this->loginSuccess === false) {
 			$this->tpl->assign('loginerror', 'Invalid username or password');
+		}
+		if (!empty($this->params['redirect'])) {
+			$this->tpl->assign('redirect', $this->params['redirect']);
+		} else if (!empty($_SERVER['HTTP_REFERER'])) {
+			$this->tpl->assign('redirect', $_SERVER['HTTP_REFERER']);
 		}
 	}
 

--- a/include/controller/Controller_Logout.class.php
+++ b/include/controller/Controller_Logout.class.php
@@ -54,7 +54,11 @@
 		if (!empty($_SESSION['gitphpuser'])) {
 			unset($_SESSION['gitphpuser']);
 		}
-		$this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true);
+		if (!empty($_SERVER['HTTP_REFERER'])) {
+			$this->headers[] = 'Location: ' . $_SERVER['HTTP_REFERER'];
+		} else {
+			$this->headers[] = 'Location: ' . $this->router->GetUrl(array(), true);
+		}
 	}
 
 	/**

--- a/include/router/Router.class.php
+++ b/include/router/Router.class.php
@@ -486,6 +486,8 @@
 					$controller->SetParam('username', $_POST['username']);
 				if (!empty($_POST['password']))
 					$controller->SetParam('password', $_POST['password']);
+				if (!empty($_POST['redirect']))
+					$controller->SetParam('redirect', $_POST['redirect']);
 				break;
 
 			case 'logout':

--- a/templates/login.tpl
+++ b/templates/login.tpl
@@ -33,6 +33,9 @@
     <td><input type="submit" value="Login" /></td>
   </tr>
 </table>
+{if $redirect}
+<input type="hidden" name="redirect" value="{$redirect|escape}" />
+{/if}
 </form>
 {/block}
 

comments