php登录适用于XAMPP但不适用于Web服务器?

时间:2014-02-09 19:35:44

标签: php mysql login xampp

我正在尝试向网站添加一个简单的登录,并让它在我的localhost XAMPP服务器上运行完美,但现在当我上传到我的网站时,我能够注册新用户,但是当我去登录页面时,它只是空白。

 <?php
$error = '';
if (isset($_POST['login'])) {
    session_start();
    $username = trim($_POST['username']);
    $password = trim($_POST['pwd']);
    // location to redirect on success
    $redirect = './adminArea_db.php';
    require_once('./includes/authenticate_pdo.inc.php');
}
?>
 <!DOCTYPE HTML>
 <html>
 <link href="css/screen.css" rel="stylesheet" media="screen" />
    <meta charset="utf-8">
    <title>Login</title>
</head>

 <body>
 <div id="main">
<?php
 if ($error) {
 echo "<p>$error</p>";
 } elseif (isset($_GET['expired'])) { 
?>
 <p>Your session has expired. Please log in again.</p>
 <?php } ?>
 <form id="form1" method="post" action="">
   <p>
    <label for="username">Username:</label>
    <input type="text" name="username" id="username" value="">
    </p>
     <p>
    <label for="pwd">Password:</label>
    <input type="password" name="pwd" id="pwd">
     </p>
     <p>
    <input name="login" type="submit" id="login" value="Log in">
   </p>
    </form>
   </div>
     </body> 
    </html>

有什么想法吗?

在我的error_log中发现了一些错误,不确定是否有用?

[11-Feb-2014 20:48:52 Europe/London] PHP Warning:  session_start()   [<ahref='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent by (output started at /home/georgepa/public_html/login_db.php:2) in /home/georgepa/public_html/login_db.php on line 6
[11-Feb-2014 20:48:52 Europe/London] PHP Warning:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home/georgepa/public_html/login_db.php:2) in /home/georgepa/public_html/login_db.php on line 6
[11-Feb-2014 20:48:52 Europe/London] PHP Warning:  session_regenerate_id() [<a href='function.session-regenerate-id'>function.session-regenerate-id</a>]: Cannot regenerate session id - headers already sent in /home/georgepa/public_html/includes/authenticate_pdo.inc.php on line 28
[11-Feb-2014 20:48:52 Europe/London] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/georgepa/public_html/login_db.php:2) in /home/georgepa/public_html/includes/authenticate_pdo.inc.php on line 29

认为我的authenticate_pdo.inc.php文件存在问题......

  <?php
   require_once('./includes/connection.inc.php');
   $conn = dbConnect();
    // get the username's details from the database
    $sql = 'SELECT salt, pwd FROM users WHERE username = :username';
    // prepare statement
     $stmt = $conn->prepare($sql);
     // bind the input parameter
     $stmt->bindParam(':username', $username, PDO::PARAM_STR);
     // bind the result, using a new variable for the password
      $stmt->bindColumn(1, $salt);
     $stmt->bindColumn(2, $storedPwd);
       try{
       $stmt->execute();
       $stmt->fetch();
      }catch(PDOException $e){
       exit('problem reading from table users'.$e->getMessage());
      }

       dbClose($conn) ;
       // encrypt the submitted password with the salt and compare with stored password
       if (sha1($password . $salt) == $storedPwd) {
        $_SESSION['authenticated'] = 'admin'; // role of authenticated user
         $_SESSION['username']=$username;
         // get the time the session started
         $_SESSION['start'] = time();
         session_regenerate_id();
         header("Location: $redirect");
         exit;
        } else {
        // if no match, prepare error message
         $error = 'Invalid username or password';
        } 
      ?>

2 个答案:

答案 0 :(得分:0)

session_start()以及session_regenerate_id()应放在php脚本的开头之前任何其他输出。

由于并非总是可以,您可以通过将ob_start()放在程序的开头来设置输出缓冲

阅读这些文章可能会有所帮助:

答案 1 :(得分:0)

删除<?php之前的所有空格。