2015-11-06 50 views
3

因此,我使用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> 
+1

* Hm ... *这些''“。$ user_name。”“包含空格,可能会被解释为额外的空格。尝试删除它们“'”。$ user_name。“'或''”。$ user_name。“''并检查错误/警告/通知http://php.net/manual/en/function.error-reporting .php和http://php.net/manual/en/mysqli.error.php所有查询。 –

+0

我刚刚尝试过,并没有改变这个问题。改变了两个''“。$ user_name。”''我找到了'$ sql'查询。 – radiocaf

+1

你是否使用错误报告,如果有,是否有任何回报?在打开PHP标记 (例如'<?php error_reporting(E_ALL);)后立即在文件顶部添加错误报告。 ini_set('display_errors',1);'然后你的代码的其余部分,看看它是否产生任何东西。 –

回答

4

使用OP后error reporting,正如我在评论提示:

“马上,将它添加到index.php页面和装载了我后:警告:在session_start():不能发送会话缓存限制器 - 已经发送了头,我也要让行动类似的一个,说会话cookie头到位会话缓存限制器 - radiocaf”

你的index.php文件(可能还有其他文件)被抛你的警告,因为你可能有你的HTML表单使用PHP,空间或cookie,甚至是BOM(字节顺序标记)。在BOM

你的文件的编码可能包含字节顺序标记,这往往是导致事业发送警告一个头。 UTF-8编码允许您将文件保存为“带”或“不带”字节顺序标记;您需要将它们保存为“不含BOM”。

这被视为输出,作为一个开放<?php标记之前是空格或饼干等

要检查文件的编码是什么,你可以检查里面的编码选项下的代码编辑器的选项。

其中之一是记事本++ https://notepad-plus-plus.org/也有其他人。

先放置你的PHP,然后放置你的表单,如果是这样的话。

请参阅以下的堆栈有关的警告:

此外,速战速决将是你的PHP文件的顶部使用ob_start();

即:

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

那么你的HTML

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

  • 然后剩下的你的PHP/SQL。

此外,作为评论原本说:

“这些'" . $user_name . "'包含空格,可能被理解为增加了额外的空间尝试取出'" .$user_name. "'或​​。”

+0

谢谢,现在我将研究这个问题,可能需要一段时间才能解决,但同时我编辑了问题以显示表单,因为它被写入到两个内容php文件中。这可能会帮助你建议我到哪里去错了。 – radiocaf

+1

@radiocaf不客气。我对我的回答做了一些更多的编辑,收集了关于为什么代码失败的更多信息/链接。它也可能是一个文件编码问题。请重新加载我的答案,以便查看添加的内容并全部阅读,特别是关于BOM,字节顺序标记的部分。 –

+0

从我所知道的,Dreamweaver设置为不包含BOM,但不知道如何单独检查每个文件,我不能肯定地说,但它的新文档首选项未设置为包含Unicode签名(BOM)。我已经添加了ob_start();到我的index.php文件找出解决问题的方法,因为index.php调用了其他文件,但没有任何改变,所以我正在通过文件在<?php下正确地添加它并查看它是否有效。 – radiocaf

1

我已经注意到在Login.php脚本中有一行代码 elseif(isset($ _POST [“login”])){但没有一个表单元素拥有 名称“login”,而不是提交按钮,你们认为 可能是一个问题?

这不是问题。如果提交按钮具有名称'登录',则它被发布为'登录',因此设置。

PHP的变化弗雷德在他的评论中建议我会遵循 - 尽管这些只会影响移动用户是没有意义的。更可能的是,这与会话如何被保存有关。

重定向到index.php是否适用于手机?如果是这样,你可以var_dump($ _ SESSION);在index.php的顶部,看看它在尝试登录后在移动设备上显示的内容?

+0

重定向(我假设你指的是'print'window.top.location.href ='index.php'“;'在Login.php文件中)起作用,但是然后人们可能会认为这可能不起作用,我只被重定向回index.php,因为表单操作是index.php。删除空格不起作用。 – radiocaf

+1

你有没有尝试在会话转储调试? –

相关问题