Problem with Session in php
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>开发者_如何学Python;
</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?
You need to start the session before you do anything. Read the docs.
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.
You can work around this by enabling output buffering, that way the headers will always be sent before the data.
Start your session before using headers
Looking at your code, there isn't any content obviously being sent before session start, however, you have various functions calls. Do any of them output content?
Is that the complete file? Even a line return before <?php
will count as content sent to the browser....
Just put session_start();
on top before your includes so when you call your startSession
function anywhere in your code it will just set any values you assign to $_SESSION.
精彩评论