Slim icon indicating copy to clipboard operation
Slim copied to clipboard

Slim Redirect Displays Headers Only

Open UchennaPrudensia opened this issue 8 years ago • 12 comments

Slim Redirect method returns a 301 status code and nothing more. I will like to redirect to a login page if a user is not signed in. but I keep getting a header that looks like this :

HTTP/1.1 301 $s Server: nginx/1.13.7 Date: Thu, 14 Dec 2017 15:56:53 GMT Transfer-Encoding: chunked Connection: keep-alive : /login:

I expect that the redirect method will call '\login' route which will then display my login.html page. I have attached a copy of my server.php file below. When I execute curl -D- localhost/login, it works. What am I doing wrong?

$slim->get('/', function(Request $req, Response $res) use ($provider): Response{ $sessionId = $req->getCookieParams()['session'] ?? ' '; if(!$provider->hasSession($sessionId)) { return $res->withRedirect('/login'); }

//Login Callback

$slim->get('/login', function(Request $req, Response $res): Response { $res->getBody()->write(file_get_contents('templates/login.html')); return $res->withHeader('Content-Type', 'text/html;charset=utf8'); });

$adapter = new \Packt\Chp6\Http\SlimAdapterServer($slim);

//Login Route $app->route('/', $adapter, ['*']); $app->route('/login', $adapter, ['*']);

server.php.zip

UchennaPrudensia avatar Dec 14 '17 16:12 UchennaPrudensia

Please provide the output of curl -I http://yourserver.com/path/to/

akrabat avatar Dec 14 '17 16:12 akrabat

Thank you. This is the output I get...

HTTP/1.1 301 $s Server: nginx/1.13.7 Date: Thu, 14 Dec 2017 16:26:04 GMT Connection: keep-alive : /login:

UchennaPrudensia avatar Dec 14 '17 16:12 UchennaPrudensia

That makes no sense as the status line is wrong and you're missing a header name.

How about curl -v http://yourserver.com/path/to/ ? Also show me the curl command itself.

akrabat avatar Dec 14 '17 16:12 akrabat

Ahh Yes you are right when I curl -I http://localhost/login, I get this output:

HTTP/1.1 200 $s Server: nginx/1.13.7 Date: Thu, 14 Dec 2017 16:30:31 GMT Connection: keep-alive : text/html;charset=utf8:

curl command is curl -I http://localhost/login

UchennaPrudensia avatar Dec 14 '17 16:12 UchennaPrudensia

You dont have a content type header? In the previous one you don't have a Location header.

geggleto avatar Dec 14 '17 16:12 geggleto

thank you. Where do I set the content type header and location header... I did curl -v http://localhost/login and I got this output:

Trying ::1... TCP_NODELAY set Trying 127.0.0.1... TCP_NODELAY set Connected to localhost (127.0.0.1) port 80 (#0) GET /login HTTP/1.1 Host: localhost User-Agent: curl/7.54.0 Accept: /

HTTP/1.1 200 $s Server: nginx/1.13.7 Date: Thu, 14 Dec 2017 16:45:40 GMT Transfer-Encoding: chunked Connection: keep-alive : text/html;charset=utf8:

Chat Application: Login
Login
class="form-control">
class="form-control">

UchennaPrudensia avatar Dec 14 '17 16:12 UchennaPrudensia

I'm going to guess that it's a problem somewhere in your NGINX configuration. Slim and PHP sets a default header for Content-Type.

geggleto avatar Dec 14 '17 16:12 geggleto

wow, I just realized that. Let me check my Nginx config file.

UchennaPrudensia avatar Dec 14 '17 17:12 UchennaPrudensia

I fixed the header issue. My output for curl -D- http://localhost is

HTTP/1.1 302 Found Server: nginx/1.13.7 Date: Fri, 15 Dec 2017 05:13:37 GMT Transfer-Encoding: chunked Connection: keep-alive Location: /login

The content-type is not showing up at all. When I curl -D- http://localhost/login (file that I want to be redirected to...) I get the following output:

HTTP/1.1 200 OK Server: nginx/1.13.7 Date: Fri, 15 Dec 2017 05:21:32 GMT Content-Type: text/html;charset=utf8 Transfer-Encoding: chunked Connection: keep-alive

UchennaPrudensia avatar Dec 15 '17 05:12 UchennaPrudensia

I would have expected that redirection to work. You don't need a Content-Type header for a 302, but you do need a Location.

akrabat avatar Dec 15 '17 07:12 akrabat

You are right. It is working now. Thank you so much

UchennaPrudensia avatar Dec 15 '17 13:12 UchennaPrudensia

You’re welcome.

akrabat avatar Dec 15 '17 13:12 akrabat