簡體   English   中英

登錄適用於桌面但不適用於移動設備

[英]Login works on desktop but not mobile?

所以我使用php-login-minimal在我幾乎完整的網站上處理登錄。

登錄系統在桌面上完美運行,但在平板電腦或移動設備上,它就像是在工作並登錄我,但最終我最終在同一頁面要求我登錄。

我不明白為什么它可以在桌面上工作但不能在移動設備上工作。 網頁是為兩者加載的頁面,因為我使用響應式設計來縮放內容以適應正在使用的任何屏幕,但登錄系統不會返回錯誤或任何可以幫助我的內容。

我在Login.php腳本中注意到有一行代碼elseif (isset($_POST["login"])) {但是沒有一個表單元素的名稱為“login”而不是提交按鈕,你們認為這可能是一個問題嗎?

我還在考慮調整代碼一點來指定URL中的登錄(www.example.com/index?login)並查看是否有效,但我不想更改代碼,因為我沒有完全了解這一切。

謝謝你們的幫助!

我的登錄表格

<form method="post" action="index.php" name="loginForm" id="loginForm">
      <label for="login_input_username">Username</label>
      <input id="login_input_username" class="login_input" type="text" name="user_name" required /><span class="linebreak"></span>
      <label for="login_input_password">Password</label>
      <input id="login_input_password" class="login_input" type="password" name="user_password" autocomplete="off" required /><span class="linebreak"></span>

      <span class="loginregister"><input type="submit"  name="login" value="Log in" /></span></form>

登錄代碼
的index.php

<?php

if (version_compare(PHP_VERSION, '5.3.7', '<')) {
    exit("Sorry, Simple PHP Login does not run on a PHP version smaller than 5.3.7 !");
} else if (version_compare(PHP_VERSION, '5.5.0', '<')) {
    // if you are using PHP 5.3 or PHP 5.4 you have to include the password_api_compatibility_library.php
    // (this library adds the PHP 5.5 password hashing functions to older versions of PHP)
    require_once("libraries/password_compatibility_library.php");
}

// include the configs / constants for the database connection
require_once("config/db.php");

// load the login class
require_once("classes/Login.php");

// create a login object. when this object is created, it will do all login/logout stuff automatically
// so this single line handles the entire login process. in consequence, you can simply ...
$login = new Login();

// ... ask if we are logged in here:
if ($login->isUserLoggedIn() == true) {
    // the user is logged in. you can do whatever you want here.
    // for demonstration purposes, we simply show the "you are logged in" view.
    include("views/logged_in.php");

} else {
    // the user is not logged in. you can do whatever you want here.
    // for demonstration purposes, we simply show the "you are not logged in" view.
    include("views/not_logged_in.php");
}

類/ login.php中

<?php

/**
 * Class login
 * handles the user's login and logout process
 */
class Login
{
    /**
     * @var object The database connection
     */
    private $db_connection = null;
    /**
     * @var array Collection of error messages
     */
    public $errors = array();
    /**
     * @var array Collection of success / neutral messages
     */
    public $messages = array();

    /**
     * the function "__construct()" automatically starts whenever an object of this class is created,
     * you know, when you do "$login = new Login();"
     */
    public function __construct()
    {
        // create/read session, absolutely necessary
        session_start();

        // check the possible login actions:
        // if user tried to log out (happen when user clicks logout button)
        if (isset($_GET["logout"])) {
            $this->doLogout();
        }
        // login via post data (if user just submitted a login form)
        elseif (isset($_POST["login"])) {
            $this->dologinWithPostData();
        }
    }

    /**
     * log in with post data
     */
    private function dologinWithPostData()
    {
        // check login form contents
        if (empty($_POST['user_name'])) {
            $this->errors[] = "Username field was empty.";
        } elseif (empty($_POST['user_password'])) {
            $this->errors[] = "Password field was empty.";
        } elseif (!empty($_POST['user_name']) && !empty($_POST['user_password'])) {

            // create a database connection, using the constants from config/db.php (which we loaded in index.php)
            $this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

            // change character set to utf8 and check it
            if (!$this->db_connection->set_charset("utf8")) {
                $this->errors[] = $this->db_connection->error;
            }

            // if no connection errors (= working database connection)
            if (!$this->db_connection->connect_errno) {

                // escape the POST stuff
                $user_name = $this->db_connection->real_escape_string($_POST['user_name']);

                // database query, getting all the info of the selected user (allows login via email address in the
                // username field)
                $sql = "SELECT user_name, user_email, user_password_hash
                        FROM users
                        WHERE user_name = '" . $user_name . "' OR user_email = '" . $user_name . "';";
                $result_of_login_check = $this->db_connection->query($sql);

                // if this user exists
                if ($result_of_login_check->num_rows == 1) {

                    // get result row (as an object)
                    $result_row = $result_of_login_check->fetch_object();

                    // using PHP 5.5's password_verify() function to check if the provided password fits
                    // the hash of that user's password
                    if (password_verify($_POST['user_password'], $result_row->user_password_hash)) {

                        // write user data into PHP SESSION (a file on your server)
                        $_SESSION['user_name'] = $result_row->user_name;
                        $_SESSION['user_email'] = $result_row->user_email;
                        $_SESSION['user_login_status'] = 1;
                        print "<script type=\"text/javascript\">";
                        print "window.top.location.href='index.php'";
                        print "</script>";
                        exit;

                    } else {
                        $this->errors[] = "Wrong password. Try again.";
                    }
                } else {
                    $this->errors[] = "This user does not exist.";
                }
            } else {
                $this->errors[] = "Database connection problem.";
            }
        }
    }

    /**
     * perform the logout
     */
    public function doLogout()
    {
        // delete the session of the user
        $_SESSION = array();
        session_destroy();
        // return a little feeedback message
        $this->messages[] = "You have been logged out.";

    }

    /**
     * simply return the current state of the user's login
     * @return boolean user's login status
     */
    public function isUserLoggedIn()
    {
        if (isset($_SESSION['user_login_status']) AND $_SESSION['user_login_status'] == 1) {
            return true;
        }
        // default return
        return false;
    }
}

not_logged_in.php文件(logged_in.php類似,只是表單無法從display:none更改,因為用於執行此操作的鏈接更改為注銷鏈接:

<?php
// show potential errors / feedback (from login object)
if (isset($login)) {
    if ($login->errors) {
        foreach ($login->errors as $error) {
            echo $error;
        }
    }
    if ($login->messages) {
        foreach ($login->messages as $message) {
            echo $message;
        }
    }
}
?>

<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="styles/main.css" rel="stylesheet" type="text/css">
<meta name="viewport" content="device-width, initial-scale=1, maximum-scale=1">
<script type="text/javascript">
function showForm(){
    document.getElementById('login').style.display = "block";
}

function hideForm(){
    document.getElementById('login').style.display = "none";
}
</script>
</head>

<body>
<header>
<div class="logo" id="logo">
<a href="#">Website Title</a>
</div>
<?php include("navigation.php"); ?>
</header>
<div id="login" class="login" style="display:none">
<div id="forms" class="forms">
<form method="post" action="index.php" name="loginForm" id="loginForm">
      <label for="login_input_username">Username</label>
      <input id="login_input_username" class="login_input" type="text" name="user_name" required /><span class="linebreak"></span>
      <label for="login_input_password">Password</label>
      <input id="login_input_password" class="login_input" type="password" name="user_password" autocomplete="off" required /><span class="linebreak"></span>

      <span class="loginregister"><input type="submit"  name="login" value="Log in" /></span></form><form action="#"><span class="loginregister"><input type="submit" value="Register"></span></form>


</div>
</div>

OP使用錯誤報告后 ,正如我在評論中建議的那樣:

“立即將它添加到index.php頁面並加載后我得到了:警告:session_start():無法發送會話緩存限制器 - 已發送的標頭,我也在移動設備上獲得類似的會話cookie標頭到位會話緩存限制器。 - radiocaf“

您的index.php文件(可能還有其他文件)會向您發出警告,因為您可能在PHP或空格,cookie或甚至BOM(字節順序標記)之上放置了HTML表單。

您的文件編碼可能包含字節順序標記,這通常是標頭發送警告的主要原因。 UTF-8編碼允許您將文件保存為“帶”或“沒有”字節順序標記; 你需要將它們保存為“無BOM”。

這被視為輸出,開頭<?php標簽之前的空格,或cookie等。

要檢查文件的編碼是什么,可以在編碼選項下檢查代碼編輯器的選項。

其中一個是Notepad ++ https://notepad-plus-plus.org/ ,還有其他一些。

首先放置PHP,然后放置表單(如果是這種情況)。

有關該警告,請參閱Stack上的以下內容:

另外,快速解決方法是使用ob_start(); 在PHP文件的頂部。

即:

<?php 
ob_start();
// rest of your PHP
?>

那你的HTML

要么

<?php 
ob_start();
?>
  • 那你的HTML

  • 然后是PHP / SQL的其余部分。

另外,正如評論中最初所述:

“這些'" . $user_name . "' '" . $user_name . "' '" . $user_name . "'包含空格,可以解釋為添加額外的空格。嘗試刪除它們'" .$user_name. "' '" .$user_name. "''".$user_name."'

我在Login.php腳本中注意到有一行代碼elseif(isset($ _ POST [“login”])){但是沒有一個表單元素的名稱為“login”而不是提交按鈕,你們認為這可能是一個問題嗎?

這不是問題。 如果提交按鈕的名稱為“登錄”,則會將其發布為“登錄”,因此已設置。

弗雷德建議我在他的評論中推薦PHP改變 - 盡管這些只會影響移動用戶是沒有意義的。 更有可能的是,這與會話的保存方式有關。

重定向到index.php是否適用於移動設備? 如果是這樣,你可以var_dump($ _ SESSION); 在index.php的頂部,在您嘗試登錄后查看它在移動設備上的內容?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM