Help needed. session_start()

Discussion in 'PHP' started by Josh-H, Apr 9, 2007.

  1. #1
    Hello.

    I have got a problem.

    here is the error:
    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/designer/public_html/login.php:5) in /home/designer/public_html/login.php on line 45
    PHP:
    Here is the code in login.php
    <?php
    session_start(); //allows session
    include "config.php";
    echo "<center>";
    
    if($logged[id]){
    //welcomes the member
    echo "Welcome $logged[username]<br><br>";
    //shows the user menu
    echo "
    - <a href='editprofile.php'>Edit Profile</a><br>
    - <a href='changepassword.php'>Change Password</a><br>
    - <a href='members.php'>Members</a><br>
    - <a href='logout.php?logout'>Logout</a>";
    }
    else
    //if there trying to login
    if(isset($_GET['login'])){
    //removes sql injections from the data
    $username= htmlspecialchars(addslashes($_POST[username])); 
    //encrypts the password
    $password = sha1(md5(md5(sha1(md5(sha1(sha1(md5($_POST[password]))))))));
    //gets the username data from the members database
    $uinfo = mysql_query("SELECT * FROM `members` WHERE `username` = '$username'") or die(mysql_error()); 
    //see if the user exists
    $checkuser = mysql_num_rows($uinfo);
    //if user name not found in database error
    if($checkuser == '0')
    {
    echo "Username not found";
    }
    else
    {
    //fetch the sql
    $udata = mysql_fetch_array($uinfo);
    //checks see if the account is verified
    if($udata[userlevel] == 0) { 
    echo "This account had not been verified.";
    }
    //if it is continue
    else
    //if the db password and the logged in password are the same login
    if($udata[password] == $password) {
    $query = mysql_query("SELECT * FROM `members` WHERE `username` = '$username'") or die(mysql_error()); 
    //fetchs the sql
    $user = mysql_fetch_array($query);
    //sets the logged session
    $_SESSION['id'] = "$user[id]";
    $_SESSION['password'] = "$user[password]";
    
    echo "You are now logged in, Please wait. . .";
    //redirects them
    echo "<meta http-equiv='Refresh' content='2; URL=login.php'/>";
    }
    //wrong password
    else
    {
    echo "Incorrect username or password!"; 
    }
    }
    }
    else
    {
    //If not the above show the login form
    echo "<form action='login.php?login' method='post'>
    <fieldset style='width: 350'>
    <table width='312'>
      <tr>
        <td width='120'>Username:</td>
        <td width='180'><input type='text' name='username' size='30' maxlength='25'></td>
      </tr>
      <tr>
        <td>Password:</td>
        <td><input type='password' name='password' size='30' maxlength='25'></td>
      </tr>
        <tr>
        <td colspan='2'><center><input type='submit' value='Login'></center></td>
      </tr>
    </table>
    </fieldset>
    </form>";
    }
    echo "<center>";
    ?> 
    PHP:
    Any ideas?

    Rep awarded for all attempts.

    At the moment I have just turned error reporting off as I don't think its causing the script to malfunction..
     
    Josh-H, Apr 9, 2007 IP
  2. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #2
    may be any white space before calling the session_start() function, or any 'echo' there. try remove them. Hope it will work.
     
    srobona, Apr 9, 2007 IP
  3. stugs

    stugs Peon

    Messages:
    157
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Is login.php being called via an include?

    What srobona is saying is correct. The session_start has to be the very first thing returned by your code. If you have any whitespaces at the top of any of your files, you'll need to remove them.
     
    stugs, Apr 9, 2007 IP