drfb52000 2011-06-08 10:54
浏览 22
已采纳

php中的Session问题

Code:

<?php
include ("conf/conf.php");
include ("classes/dbhelper.php");
$conf = new Dbconf();
$dbURL = $conf->get_databaseURL();
$dbUName = $conf->get_databaseUName();
$dbPword = $conf->get_databasePWord();
$dbName = $conf->get_databaseName();
$nameOfDbWithWorkers = $conf->get_nameOfDbWithWorkers();

if($_POST['auth']){
    $login = trim(($_POST['login']));
    $pass = trim($_POST['pass']);  

    $dbHelp = new DbHelper($dbURL, $dbUName, $dbPword, $dbName, $nameOfDbWithWorkers);
    $userType = $dbHelp->getUser($login, $pass);

    switch ($userType){
    case "admin":
        startSession($login, $pass);
        header("Location: adminpage.php");
        break;  
    case "user": 
        startSession($login, $pass);
        header("Location: operatorpage.php");
        break;
    case "nomatch": 
    echo 
    '<html> 
    <body> 
    <table width=100% height=100% border=0 cellpadding=0 cellspacing=0> 
        <tr valign="center" align="center">
            <td> 
                <form action="authorize.php" method="post">
                Логин:<input type="text" name="login"><br>
                Пароль:<input type="password" name="pass"><br>
                <input type="submit" name="auth">
                <font color = red> Неправильный пароль или логин</font>  
            </td>
        </tr>
    </table>

    </form>
    </body> 
    </html>';
        break;
    }       
}
else{
    header("Location: index.php");
}
    function startSession($login, $pass){
        session_start();
        $_SESSION['login'] = $login;
        $_SESSION['pass'] = $pass;
        $_SESSION['usr_id'] = md5(crypt($login,$pass));
    }

When I enter right login and pass information, i have a next errors.

Warning: Cannot modify header information - headers already sent by (output started at Z:\home\ecl.ru\www\classes\dbhelper.php:24) in Z:\home\ecl.ru\www\authorize.php on line 20

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at Z:\home\ecl.ru\www\classes\dbhelper.php:24) in Z:\home\ecl.ru\www\authorize.php on line 53

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at Z:\home\ecl.ru\www\classes\dbhelper.php:24) in Z:\home\ecl.ru\www\authorize.php on line 53

How to solve this?

  • 写回答

6条回答 默认 最新

  • duanmu1736 2011-06-08 11:13
    关注

    There is something outputted at you file dphelper.php, as the notice says:

    Warning: Cannot modify header information - headers already sent by (output started at Z:\home\ecl.ru\www\classes\dbhelper.php:24) in Z:\home\ecl.ru\www\authorize.php on line 20
    

    As headers are sent before any kind of output, your session_start() comes in too late. One suggestion, as people already stated, is to put session_start() on top of the file, another one (could be implemented together with the first one) - to look into dbhelper.php and make sure it does not output anything (from the name of the file - it should not, anyway). And, when I say "output", it is not necessarily echo / print / etc, it could also be white space after the closing php tag at the end of the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写
  • ¥15 graphpad prism 三因素重复测定报错