Re: Login page error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The error comes from having output before the session_start(). This means that anything before the <? would be output. Even a single empty space.

Janet


Humani Power wrote:

Hi! Im trying to make a login page. I have searched for examples that makes
me check the user name with a database, and the one that suits better is
this code.

<?
session_start(); // start session.
?>
<!-- header tags, edit to match your own, or include template header file. -->
<html>
<head>
<title>Login</title>
<head>
<body>
<?
if(!isset($username) | !isset($password)) {
// escape from php mode.
?>
<form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?".
$QUERY_STRING;}?>" method="POST">
<p align="center">Members only. Please login to access this document.</p>
<table align="center" border="0">
<tr>
 <th>
Username:
 </th>
 <th>
<input type="text" name="username">
 </th>
</tr>
<tr>
 <th>
Password:
 </th>
 <th>
<input type="password" name="password">
 </th>
</tr>
<tr>
 <th colspan="2" align="right">
<input type="submit" value="Login">
</form>
 </th>
</tr>
</table>
</body>
</html>
<?
exit();
}

// If all is well so far.

session_register("username");
session_register("password"); // register username and password as
session variables.

// Here you would check the supplied username and password against
your database to see if they exist.
// For example, a MySQL Query, your method may differ.

$sql = mysql_query("SELECT password FROM user_table WHERE username =
'$username'");
$fetch_em = mysql_fetch_array($sql);
$numrows = mysql_num_rows($sql);

if($numrows != "0" & $password == $fetch_em["password"]) {
$valid_user = 1;
}
else {
$valid_user = 0;
}

// If the username exists and pass is correct, don't pop up the login
code again.
// If info can't be found or verified....

if (!($valid_user))
{
session_unset();   // Unset session variables.
session_destroy(); // End Session we created earlier.
// escape from php mode.
?>
<form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?".
$QUERY_STRING;}?>" method="POST">
<p align="center">Incorrect login information, please try again. You
must login to access this document.</p>
<table align="center" border="0">
<tr>
 <th>
Username:
 </th>
 <th>
<input type="text" name="username">
 </th>
</tr>
<tr>
 <th>
Password:
 </th>
 <th>
<input type="password" name="password">
 </th>
</tr>
<tr>
 <th colspan="2" align="right">
<input type="submit" value="Login">
</form>
 </th>
</tr>
</table>
</body>
</html>
<?
exit();
}
?>


After this, I have only included on a file that has this code

<?php
include_once 'connection/login.php';
?>


But when I try to see it in a browser I got the error


*Warning*:  session_start() [function.session-start
<http://localhost/apache2-default/function.session-start>]: Cannot
send session cache limiter - headers already sent (output started at
/var/www/apache2-default/visual_imag.php:2) in
*/var/www/apache2-default/connection/login.php* on line *2

I have searched for possible answers, and all I have found is that I
should not send any output before the session_start(); But in this
code the session_start(); output is before anything else.
can you give me a tip?

Thanks in advance.
Yamil
*



--
Janet Valade -- janet.valade.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux