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

移动端浏览器跳过PHP elseif语句,本地及桌面端运行正常

Troubleshooting Mobile Redirect Issue in PHP Login Flow

Hey there! Let's figure out why your login redirects work perfectly on desktop but keep falling into the final else branch on mobile. The core issue here is that none of your username matches are triggering on mobile—even when the user enters the correct credentials. Here are the most likely fixes to try:

1. Fix Case Sensitivity (The #1 Culprit)

Mobile keyboards love auto-capitalizing the first letter of input fields. If your user types Tom instead of tom, the strict == comparison will fail completely. To fix this:

  • Convert the submitted username to lowercase (or uppercase) before running your checks:
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $name = strtolower(trim($name)); // Trim catches accidental spaces too!
    
  • Then update your condition blocks to use lowercase usernames:
    if ($name == 'tom') {
        header("Location: ../admin.php?login=success");
        exit;
    } elseif ($name == 'dick') {
        header("Location: ../page_one.php?login=success");
        exit;
    }
    // ... rest of your elseif conditions
    

2. Strip Accidental Spaces

Mobile users often accidentally add leading/trailing spaces when typing (like harry instead of harry). The trim() function we added above will strip whitespace from both ends of the username string, ensuring these tiny typos don't break your matches.

3. Debug the Actual Submitted Value

To confirm exactly what's being sent from mobile, add a quick debug line right after fetching the $name variable:

$name = mysqli_real_escape_string($conn, $_POST['name']);
var_dump($name); // This will show you the exact value received from mobile
exit;

Test this on your mobile device—you might spot unexpected capitalization, spaces, or even special characters that are breaking the string comparison.

Quick Reminder About Security

I noticed you mentioned you plan to add prepared statements and password hashing later—excellent call! Storing plain-text passwords is a major security risk, so make that a priority once this redirect issue is fixed. Using password_hash() when creating user accounts and password_verify() during login will keep your users' data safe.

Give these fixes a go—case sensitivity and accidental spaces are the most common mobile input quirks that break strict string matches. Let me know if you hit any other snags!

内容的提问来源于stack exchange,提问作者L-plates

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:27:16