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);
johan
June 10, 2016, 5:32pm
3
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 âŚ
herman
June 11, 2016, 2:27am
6
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.