Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Session problem in PHP-Mysql login page please help.Below are errors

    I am getting the below mentioned errors when i perform the login by giving correct password and username

    Warning : Cannot send session cookie-headers already sent by(output started at /var/www/html/BNBU/weblogin.php:13) in /var/www/html/BNBU/weblogin. on line 87

    Warning : cannot send session cache limiter-headers already sent by(output started at /var/www/html/BNBU/weblogin.php:13) in /var/www/html/BNBU/weblogin. on line 87

    Warning : Cannot add header information- headers already sent by(output started at /var/www/html/BNBU/weblogin.php:13) in /var/www/html/BNBU/weblogin. on line 87

    My loginform code is 'weblogin.php'

    Here I have a table in mysql named webadmin (username,password)

    <html>
    <head>
    <title>Webmaster Login Form</title>
    </head>
    <body>
    <br>
    <br>
    <br>
    <br>
    <form action="weblogin.php" method="post">
    <table width=225 border=0 align="center">
    <tr>
    <td width="219" bgcolor="#999999">
    <p align="center"><font color="white"><span style="font-size:12pt;"><b>LOGIN</b></span></font></p>
    </td>
    </tr>
    <tr>
    <td width="219">
    <table width=220 border=0 align="center">
    <tr>
    <span style="font-size:10pt;"><b>Username:</b></span>
    <input type="text" name="username"/>
    </tr>
    <tr>
    <span style="font-size:10pt;"><b>Password:&nbsp;</b></span>
    <input type="password" name="password"/>
    </tr>
    <tr>
    <span style="font-size:12pt;"><b><a href="forgot.html" target="_blank">Forgot Password</a></b></span>
    <input type="submit" name="submit" value="Login" onMouseOver =" this.style.background='lemonchiffon';this.style.color='brown';" onMouseOut =" this.style.background='#78B4FF';this.style.color='darkblue';"/>
    <input type="hidden" name="submitted" value="TRUE" />
    </tr>

    </table>
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    <?php

    // Check if the form has been submitted.
    if (isset($_POST['submitted']))
    {
    mysql_connect("localhost","root") or die("Error in Connection:".mysql_error());
    mysql_select_db("BNBU") or die(mysql_error()); // Connect to the db.
    $errors = array(); // Initialize error array.

    // Check for an username.
    if (empty($_POST['username']))
    {
    $errors[] = 'Enter your username.';
    echo "<br>";
    }
    else
    {
    $e = $_POST['username'];
    }
    // Check for a password.
    if (empty($_POST['password']))
    {
    $errors[] = 'Enter your password.';
    echo "<br>";
    }
    else
    {
    $p = $_POST['password'];
    }

    if (empty($errors))
    {
    // If everything's OK.
    // Retrieve the username and password for that username/password combination.
    $query = "SELECT username,password FROM webadmin WHERE username='$e' AND password='$p'";
    $result = mysql_query ($query); // Run the query.
    $row = mysql_fetch_array($result); // Return a record, if applicable.

    if ($row)
    {
    // A record was pulled from the database.
    // Set the session data & redirect.
    session_start();
    $_SESSION['username'] = $row[0];
    $_SESSION['password'] = $row[1];

    // Redirect the user to the loggedin.php page.
    // Start defining the URL.
    $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
    // Check for a trailing slash.
    if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') )
    {
    $url = substr ($url, 0, -1); // Chop off the slash.
    }
    // Add the page.
    $url .= '/adminpage.html';
    header("Location: $url");
    exit(); // Quit the script.
    }
    else
    { // No record matched the query.
    $errors[] = 'The username and password entered do not match.'; // Public message.
    //$errors[] = mysql_error() . '<br /><br />Query: ' . $query; // Debugging message.
    }

    } // End of if (empty($errors)) IF.

    mysql_close(); // Close the database connection.

    } else { // Form has not been submitted.

    $errors = NULL;

    } // End of the main Submit conditional.

    // Begin the page now.

    if (!empty($errors))
    {
    // Print any error messages.
    echo '<b>Error!</b><br/>';
    foreach ($errors as $msg)
    {
    // Print each error.
    echo " - $msg<br />\n";
    }
    echo '<p>Please try again.</p>';
    }
    ?>

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You are initiating the session after output has been sent.

    The line that contains:

    PHP Code:
    session_start(); 
    It needs to be moved to the top of the PHP file. Make sure there are no empty lines or characters before that PHP block.

    "Note: If you are using cookie-based sessions, you must call session_start() before anything is outputted to the browser." (The default is cookie-based sessions)

    http://us3.php.net/manual/en/function.session-start.php

  • #3
    New Coder
    Join Date
    Jan 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Thanks but one doubt

    if i put session_start(); before the connection query then how to get the values of row[0] and row[1] (see code above)

  • #4
    New to the CF scene
    Join Date
    Jan 2007
    Location
    Lahore, Pakistan
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Move the php code in the beginning or move it to another page. You can run mysql query before starting session but you can't send any output, usually that means you can't use echo or print() anything on screen.

  • #5
    New Coder
    Join Date
    Jan 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Thanks

    But i got some more problems on further proceedings.please visit and help me.My problems are posted much above this.

    Here is the link:
    PHP-mysql coding help needed,login problem,automatic email problem,display problem


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •