flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.61k stars 408 forks source link

/redirect wont work on live server? #298

Closed svidlak closed 5 years ago

svidlak commented 7 years ago

Hi, title says it all. Got the necessary htaccess configurations (from the website) and everything else works as expected. any idea?

mikecao commented 7 years ago

Do you mean the method Flight::redirect() doesn't work? Does it throw an error?

svidlak commented 7 years ago

yes, the method flight::redirect(), nope, no errors, just echo's the path inside the brackets

mikecao commented 7 years ago

All the redirect does it print the URL and sends a 303 response. You need to check the network and see what response you're getting. If you're using Chrome, open Developer Tools -> Network.

svidlak commented 7 years ago

https://snag.gy/CSqM5d.jpg

once the page reaches 'register_part2' route it should be flight:redirect('/register') but the redirect won't fire

mikecao commented 7 years ago

The status code says 200, which is not a redirect. What does your route code look like?

svidlak commented 7 years ago

http://pastebin.com/7te6UbFB

the render is a form with a welcome message, but instead it prints out '/register' (treats flight::redirect as an echo of some sort)

mikecao commented 7 years ago

The redirect does print out the url it's redirecting too, but it also sends a 303 and a Location header so the browser does the redirect. I don't know why it's missing from your response headers. It could be some issue with some server setup. Are you using the latest version?

JhoanSteve commented 7 years ago

Can be redirected with post parameters

jamesmallred commented 7 years ago

I'm running into this error as well. I am using MAMP as my local server and all of the overrides have been allowed. I'll render a .php file with a form, with an action attribute set to "", in a GET route. Whenever the form is submitted I attempt to use a POST request to redirect the user to the correct page after the data is handled. The POST route fires correctly but doesn't redirect the user to the specified route. It prints the path on the page and does not render the .php file specified for the GET route of the redirect path.

svidlak commented 7 years ago

still no answer on this 1? what a shame

jimlei commented 7 years ago

@svidlak do you have any more info on the environment? The redirect method is quite simple so it sounds like it must be an environment problem.

@jamesmallred same as above, just tried a fresh windows 8.1 install (VM) with wamp and Flight 1.3.2, redirect works fine

# .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
# index.php
<?php

require 'vendor/autoload.php';

Flight::route('/', function(){
    echo 'hello world!';
});

Flight::route('/redirect', function(){
    Flight::redirect('/redirect_target');
});

Flight::route('/redirect_target', function(){
    echo 'You were redirected!';
});

Flight::start();
jamesmallred commented 7 years ago

@jimlei Thanks for your response. I no longer code using Flight and php. I've found that JavaScript using the Angular and Express frameworks work better for me.

niconerd-zz commented 6 years ago

@jimlei I was experiencing the same issue, simply adding an exit; after the redirect line makes it work for me. Maybe that gives you a clue on the cause of the issue?

@svidlak give it a try! Hope it helps you too...

paxperscientiam commented 3 years ago

I've found that it is necessary to call exit after the redirect. Is this the way it's supposed to work? If so, perhaps an update to docs is in order? If not, perhaps there is a bug?

EDIT: This is a clear enough explanation for my purposes, but docs should probably be updated for redirects. https://github.com/mikecao/flight/issues/341#issuecomment-338485668

zpvini commented 3 years ago

I was having trouble with this too. My code was:

\Flight::redirect('/login', 401);

My goal was to redirect user to login when session expired (unauthorized), which worked fine in localhost but not on live server - the redirect didn't work, just echoed the route on screen.

I've updated Flight to latest version which still works in localhost but instead of echoing the route started giving HTTP ERROR 401 on live server. Apparently you shouldn't redirect a web page with status 401, so using the default redirect status code worked just fine.

Wasted one hour on this one, thought it was a server misconfiguration. Later found out on stackoverflow that this isn't supported by browsers.

Documenting this here so if someone stumbles on the same error while trying to redirect from flight with status code 401, just keep the default status code.

CodeAlDente commented 3 years ago

@zpvini For redirections only status codes starting with a 3... are used, see https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_redirection

You are right, in the official docs of FlightPHP the following is mentioned:

Flight::redirect('/new/location', 401);

ref: https://flightphp.com/learn#redirects

I'm very sure @mikecao wanted to use status code 301 ("Moved Permanently") here as this is widely used to redirect to a "new location". So just kind of a typo.

I think the best way to handle your scenario is by redirecting with status code 302 ("Found") to your login page.

paxperscientiam commented 3 years ago

To showcase what @niconerd means ....

If one is sure that they always want to exit immediately after executing Flight::redirect(...), then one could define an "after" hook for redirects.

Flight::after("redirect", function () {
    exit;
});