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条)

报告相同问题?

悬赏问题

  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧