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, ['*']);
Please provide the output of curl -I http://yourserver.com/path/to/
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:
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.
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
You dont have a content type header?
In the previous one you don't have a Location header.
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
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.
wow, I just realized that. Let me check my Nginx config file.
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
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.
You are right. It is working now. Thank you so much
You鈥檙e welcome.
Most helpful comment
Please provide the output of
curl -I http://yourserver.com/path/to/