# Redirect issue to new page

**URL:** https://discourse.slimframework.com/t/redirect-issue-to-new-page/390
**Category:** Questions
**Created:** [June 10, 2016, 5:10pm UTC](https://discourse.slimframework.com/t/redirect-issue-to-new-page/390 "2016-06-10T17:10:54Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![somasish](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/somasish/32/129_2.png) [@somasish](https://discourse.slimframework.com/u/somasish)
#### Post date: [June 10, 2016, 5:10pm UTC](https://discourse.slimframework.com/t/redirect-issue-to-new-page/390/1 "2016-06-10T17:10:54Z")

</div>

Hi,  
i am struggling to redirect a login flow.  
i have read and tried a lot many ways in all possible forums but found no real solution. Below is the code please have a look.

```
$app->post('/signin', function (Request $request, Response $response) use($app){
$email_id = $request->getParam('email');
       $pass_id = $request->getParam('password');
        $response_json = "";
        if($email_id !='abc@gmail.com' && $pass_id != 'pass') // Ignore the Condition. Please check the Else Block
            {
                $response_json = array("error"=>1); //Username and Password Doesnt Match
   				 $response->withStatus(200);
     			 $response->withHeader('Content-type', 'application/json');
      			$response->getBody()->write(json_encode($response_json));
      			return $response;
            }
        else
            {
				$response_json = array("error"=>2 ); // Username and Password Match
				return $response->withRedirect('/home.php', 301);
				die();
 				// $app->redirect('home');
   				// return $response->withStatus(301)->withHeader('Location', '/home.php');
   				// die();
			      /*header("Location: http://www.abc.com/thankyou.php?msg=1");
			              die();*/
    
            }
});

$app->get('/', function (Request $request, Response $response) {
    return $this->view->render($response, '/home.php');
})->setName('home');
$app->run();

```

Please let me know where i have gone wrong.  
Thanks in Advance

---

<div class="post-metadata">

### Author: ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)
#### Post date: [June 10, 2016, 5:28pm UTC](https://discourse.slimframework.com/t/redirect-issue-to-new-page/390/2 "2016-06-10T17:28:35Z")

</div>

```php

return $response->withRedirect(insert_page_here);

```

---

<div class="post-metadata">

### Author: ![johan](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@johan](https://discourse.slimframework.com/u/johan)
#### Post date: [June 10, 2016, 5:32pm UTC](https://discourse.slimframework.com/t/redirect-issue-to-new-page/390/3 "2016-06-10T17:32:58Z")

</div>

Possible to redirect `back`?

---

<div class="post-metadata">

### Author: ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)
#### Post date: [June 10, 2016, 5:33pm UTC](https://discourse.slimframework.com/t/redirect-issue-to-new-page/390/4 "2016-06-10T17:33:57Z")

</div>

you can redirect anywhere…

Same page should be something like this.

```auto
return $response->withRedirect((string)$request->getUri());

```

---

<div class="post-metadata">

### Author: ![somasish](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/somasish/32/129_2.png) [@somasish](https://discourse.slimframework.com/u/somasish)
#### Post date: [June 10, 2016, 10:59pm UTC](https://discourse.slimframework.com/t/redirect-issue-to-new-page/390/5 "2016-06-10T22:59:04Z")

</div>

Hi,  
Thanks for the reply but i tried all possible way of using the URi Interface but still its not redirecting same above code if-else block with few try

> > if($email\_id !=‘abc@gmail.com’ && $pass\_id != ‘pass’) // Ignore the Condition. Please check the Else Block  
> > {  
> > $response\_json = array(“error”=\>1); //Username and Password Doesnt Match
> > 
> > ```
> > }
> > else
> > {	$response_json = array("error"=>2 ); // Username and Password Match
> > /*return $response->withRedirect('/home.php', 301);
> > die();*/
> > // $app->redirect('home'); 
> > // return $response->withStatus(301)->withHeader('Location', '/home.php');
> > // die();
> > /*header("Location: http://www.abc.com/thankyou.php?msg=1");
> > die();*/
> > 
> > ```
> 
> ```
> $uri = new Uri('http://www.abc.com/home.php'); // **its a new page redirection**
> $request = (new Request())->withMethod('GET')->withUri($uri)->withHeader('Accept', 'application/json');
> 
> ```
> 
> > ```
> > /* $response->withUri(UriInterface $uri, $preserveHost = false);*/
> > 
> > ```
> 
> ```
> return $response->withRedirect((string)$request->getUri());
> 
> ```
> 
> > ```
> > }
> > 
> > ```

Tried all possible way without any success … ☹

---

<div class="post-metadata">

### Author: ![herman](https://avatars.discourse-cdn.com/v4/letter/h/eada6e/32.png) [@herman](https://discourse.slimframework.com/u/herman)
#### Post date: [June 11, 2016, 2:27am UTC](https://discourse.slimframework.com/t/redirect-issue-to-new-page/390/6 "2016-06-11T02:27:13Z")

</div>

your code look like return Json values though AJAX, so you need to redirect in your Javascript instead of in PHP Code.

in php code:

> $json = array();  
> $json[‘redirect’] = $response-\>withRedirect(‘/home.php’, 301);  
> echo json\_encode($json);

in in your html page you just redirect it.

> window.location = json[‘redirect’];

---

<div class="post-metadata">

### Author: ![somasish](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/somasish/32/129_2.png) [@somasish](https://discourse.slimframework.com/u/somasish)
#### Post date: [June 11, 2016, 10:39pm UTC](https://discourse.slimframework.com/t/redirect-issue-to-new-page/390/7 "2016-06-11T22:39:08Z")

</div>

Thanks Herman,  
yeah i realised because of AJAX it was not happening. Handling AJAX properly gave me the perfect solution.
