PHP登录不会工作,并且没有显示错误

时间:2012-05-25 13:35:33

标签: php sql

我一直在努力解决这个错误。

我创建了一个个人资料网站,如果我运行它,那么登录并没有问题可以重定向到个人资料页面。

但是很快我上传它,当我按下登录时没有任何事情发生。我只是得到一个空页...但是如果我打开页面源我可以看到它读取我的init.inc.php文件。但只不过评论代码被引用.. 由于我不会得到任何错误信息,我真的不知道这是什么问题。

aaa希望你能得到我想说的话:)。

这是我的登录页面

<?php 
 include 'core/inc/init.inc.php';
 if ($_SERVER['HTTP_HOST'] != 'localhost') // running in remote server
    $server = 'pixeltouch2.mysql.domeneshop.no';  
    else // running locally
    $server = 'pixeltouch2.mysql.domeneshop.no';

mysql_connect($server, "LOGIN", "pass") or die(mysql_error());
  mysql_select_db("pixeltouch2") or die(mysql_error()) ;


$errors = array();

if (isset($_POST['username'], $_POST['password'])){
    if(empty($_POST['username'])){
        $errors[] = 'The username cannot be empty.';    
    }
        if(empty($_POST['password'])){
        $errors[] = 'The password cannot be empty.';
    }
//Log in
    if (valid_credentials($_POST['username'], $_POST['password']) == false ){
        $errors[] = 'Username / Password incorrect.';
    }
        if (empty($errors)){
            $_SESSION['username'] = htmlentities($_POST['username']);
            $_SESSION['uid'] = fetch_user_id($_SESSION['username']);

            header("Location: profile.php?uid=" . $_SESSION['uid']);
                die();
                echo $_SESSION['uid'];
        }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Pixeltouch</title> 

<link rel="stylesheet" type="text/css" href="ext/style.css" />

</head> 
<body> 
<div id="page-wrap"> 
<div id="main-content"> 
<br/>
<?php include_once('template/head.inc.php');
?>
<div id="menu"> 

<?php include_once('template/nav.inc.php');
?> 

</div> 
<!-- SKRIVBOX-->

<div>
<?php 
if(empty($errors) == false){
?>
<ul>

<?php

foreach ($errors as $error) {
    echo "<li>$error</li>";
}

?>
</ul>

<?php

}else{
echo 'Need an account ? <a href="register.php">Register here</a>'; 
}

?>



</div>
<br/>
<form method="post" action="" >
<p>
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo  htmlentities($_POST['username']); ?>" />
</p>
<p>
<label for="password">Password: </label>
<input type="password" name="password"  id="password" />
</p>
<p>
<input type="submit" value="Login" />
</p>

</form>


<!-- SKRIVBOX END-->
</div> 

<?php include_once('template/foot.inc.php');
?>
</div>
</body>
</html>

我的init.inc.php文件

     <?php
     session_start();

/*
     mysql_connect("pixeltouch2.mysql.domeneshop.no", "LOGIN", "PASS") or die(mysql_error()) ; 
     mysql_select_db("pixeltouch2") or die(mysql_error()) ;
     */

     if($_SERVER['HTTP_HOST'] != 'pixeltouch2.mysql.domeneshop.no')// running in remote server
        $server = 'pixeltouch2.mysql.domeneshop.no';  
        else // running locally
        $server = 'pixeltouch2.mysql.domeneshop.no';

    mysql_connect($server, "login", "pass") or die(mysql_error());
      mysql_select_db("pixeltouch2") or die(mysql_error()) ;

     $path = dirname(__FILE__);
     include("{$path}/user.inc.php");

     ?>


                                        <!--Registration/Login (START)-->
     <?php 


     $exceptions = array('register', 'login', 'user_list', 'profile', 'edit_profile', 'upload');

     $page = substr(end(explode('/', $_SERVER['SCRIPT_NAME'])), 0, -4);

     if(in_array($page, $exceptions) == false){
        if (isset($_SESSION['username']) == false){
        header('Location: login.php');
        die();
        }
      }
     ?>

                                        <!--Registration/Login (END)--> 


                                            <!--User Profile (START)-->
     <?php
     $_SESSION['uid'] = 1;


     ?>

                                            <!--User Profile (END)-->

错误日志

Warning: Cannot modify header information - headers already sent by (output started at /home/3/p/pixeltouch/www/book/core/inc/init.inc.php:2) in /home/3/p/pixeltouch/www/book/login.php on line 32

When I look @ line 32 I its my  header("Location: profile.php?uid=" . $_SESSION['uid']);
                die();
                echo $_SESSION['uid'];

所以关于会话的东西,还有另一种编写代码的方法吗?

1 个答案:

答案 0 :(得分:1)

错误就是说它就在那里。您已经将输出发送到浏览器,并且通过说“输出”,这意味着发送到浏览器的任何不是http标头的内容。

请查看您的文件:

  • 在标题位置声明之前的echo / print语句。
  • 您的<?php标记之前的空格或换行符(即使在包含的文件中)。
  • 使用页面开头的字节顺序标记(BOM)。

所以......看看我看到的代码示例(+符号表示空格)

++++<?php
      session_start();

您需要更正该用途

<?php
  session_start();
// No spaces at the beginning

甚至在这部分

 // You close the php tag
 ?>++++++++++
 ++++<!--Registration/Login (START)-->+++++++
 <?php 
 $exceptions = array('register', 'login', 'user_list', 'profile', 'edit_profile', 'upload');

您不需要这样做,而且您正在发送HTML评论。

为了避免此类问题,您始终可以使用output buffering来确保在标头之前没有发送输出。