Redirect issue to new page

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


return $response->withRedirect(insert_page_here);

Possible to redirect back?

you can redirect anywhere…

Same page should be something like this.

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

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 … :frowning:

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’];

1 Like

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