You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改Session后出现PHP Cookie错误及页面重定向异常求助

Troubleshooting Infinite Redirect After Logging Into Your User Dashboard

Hey Jenny, let's work through this infinite redirect issue together—it's a classic pitfall with authentication flows, and since you narrowed it down to your recently added code, we can focus our checks there. Here are the most actionable fixes to try:

1. Audit Your Redirect Logic in Auth Guards/Middleware

The #1 culprit here is a loop between your login route and dashboard route. Double-check any code that handles access control:

  • If you're using a backend middleware (like Express.js), make sure it's not incorrectly redirecting authenticated users back to the login page, or unauthenticated users to the dashboard (and vice versa). For example, watch out for typos in redirect paths:
    // Wrong example that triggers a loop
    app.get('/dashboard', (req, res) => {
      if (!req.isAuthenticated()) {
        res.redirect('/dashboard'); // Oops—sends unauthenticated users straight back to dashboard
      } else {
        res.render('dashboard');
      }
    });
    
  • On the frontend (Vue/React/Angular), check your route guards. If your dashboard route redirects to login when unauthenticated, but your login route redirects to dashboard when authenticated, ensure the authentication check itself isn't broken.

2. Verify Your User Authentication State Check

Chances are, the code that verifies if a user is logged in is returning an incorrect value:

  • Check if your isAuthenticated (or similar) function is properly reading the auth token/session from cookies. Maybe you're checking for a cookie that's no longer being set by your new code, or parsing it incorrectly.
  • If you're using server-side sessions, confirm the server is correctly persisting the user session after login. A quick test: after logging in, check if your session store (like Redis or Express Session) has an entry for the user.

3. Trace the Redirect Chain with Browser DevTools

This will show you exactly where the loop is happening:

  1. Open Firefox DevTools (F12) and go to the Network tab.
  2. Check the Preserve log option (so logs don't clear between redirects).
  3. Try logging in again. You'll see a repeating sequence of requests (e.g., /login → /dashboard → /login).
  4. Look at the response headers for each request—specifically the Location header—to see which endpoint is triggering the redirect. This will point you to the exact code block causing the issue.

Even if you cleared cookies, your new code might be setting them incorrectly:

  • Ensure the cookie's Path is set to / (so it's accessible across all routes). If it's set to /login only, the dashboard route won't see it, and the server will treat the user as unauthenticated.
  • Check for SameSite or HttpOnly settings that might be preventing the cookie from being sent to the server on subsequent requests.

5. Roll Back Recent Code Incrementally

Since you know the issue started after adding new code, try rolling back changes bit by bit:

  • Start with the auth-related code you added (login handlers, dashboard route protection, user state management).
  • After each rollback, test the login flow to see if the redirect loop stops. This will pinpoint exactly which line or function is causing the problem.

内容的提问来源于stack exchange,提问作者Jenny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:36:46