Warning: session_start() [function.session-start]: NEED HELP FAST!

Discussion in 'PHP' started by healingoasis, Jun 16, 2011.

  1. #1
    I am more than willing to pay for this answer as I have already pulled my hair out trying to fix it myself lol.

    You can see the error here

    This is the code (lines 1-26):

    <?
    include("database.php");
    include("form.php");

    class Session
    {
    var $username; //Username given on sign-up
    var $userid; //Random value generated on current login
    var $userlevel; //The level to which the user pertains
    var $time; //Time user was last active (page loaded)
    var $logged_in; //True if user is logged in, false otherwise
    var $userinfo = array(); //The array holding all user info
    var $url; //The page url current being viewed
    var $referrer; //Last recorded site page viewed
    var $ip; //Remote IP address

    function Session(){
    $this->ip = $_SERVER["REMOTE_ADDR"];
    $this->time = time();
    $this->startSession();
    }

    function startSession(){
    global $database;
    session_start();

    Please let me know if you can help me with this

    Regards,
    H.
     
    healingoasis, Jun 16, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Means that something was already outputted to the browser. Can you post the entire contents of session.php?

    Use the php or code tags so that it is formatted properly on the forum.
     
    jestep, Jun 16, 2011 IP
  3. healingoasis

    healingoasis Peon

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <?
    include("database.php");
    include("form.php");
    
    class Session
    {
       var $username;            //Username given on sign-up
       var $userid;              //Random value generated on current login
       var $userlevel;           //The level to which the user pertains
       var $time;                //Time user was last active (page loaded)
       var $logged_in;           //True if user is logged in, false otherwise
       var $userinfo = array();  //The array holding all user info
       var $url;                 //The page url current being viewed
       var $referrer;            //Last recorded site page viewed
       var $ip;                  //Remote IP address  
    
       function Session(){
          $this->ip = $_SERVER["REMOTE_ADDR"];
          $this->time = time();
          $this->startSession();
       }
    
       function startSession(){
          global $database;  
          session_start();   
    	  
          /* Determine if user is logged in */
          $this->logged_in = $this->checkLogin();
    
          /* Set referrer page */
          if(isset($_SESSION['url'])){
             $this->referrer = $_SESSION['url'];
          }else{
             $this->referrer = "/";
          }
    
          /* Set current url */
          $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
       }
    
       function checkLogin(){
          global $database; 
          /* Check if user has been remembered */
          if(isset($_COOKIE['cookname'])){
             $this->username = $_SESSION['username'] = $_COOKIE['cookname'];
          }
    
          if(isset($_SESSION['username'])){
             if($database->confirmUserName($_SESSION['username']) != 0){
                unset($_SESSION['username']);
                return false;
             }
    
             $this->userinfo  = $database->getUserInfo($_SESSION['username']);
             $this->username  = $this->userinfo['username'];
             return true;
          }
          else{
             return false;
          }
       }
    
       function login($subuser, $subpass, $subremember){
          global $database, $form;  
    		
    	  unset($form->errors);
    	  /* Checks if this IP address is currently blocked*/	
          $result = $database->confirmIPAddress($this->ip);
    
          if($result == 1){
             $error_type = "access";
             $form->setError($error_type, "Access denied for ".TIME_PERIOD." minutes");
          } 
    
    	  /* Return if form errors exist */
          if($form->num_errors > 0){
             return false;
          }
    	  
    	  $error_type = "attempt";
    
    	  /* Username and password error checking */
          if(!$subuser || !$subpass || strlen($subuser = trim($subuser)) == 0){
             $form->setError($error_type, "Username or password not entered");
          }
    	  
          if($form->num_errors > 0){
             return false;
          }
    
          /* Checks that username is in database and password is correct */
          $subuser = stripslashes($subuser);
          $result = $database->confirmUserPass($subuser, $subpass);
    
          if($result == 1){
             $form->setError($error_type, "Invalid username or password.");
    		 $database->addLoginAttempt($this->ip);
          }
    	  
          if($form->num_errors > 0){
             return false;
          }
    
          /* Username and password correct, register session variables */
          $this->userinfo  = $database->getUserInfo($subuser);
          $this->username  = $_SESSION['username'] = $this->userinfo['username'];
    
          
          /* Null login attempts */
    	  $database->clearLoginAttempts($this->ip);
    
    	  if($subremember){
             setcookie("cookname", $this->username, time()+COOKIE_EXPIRE, COOKIE_PATH);
          }
    
          /* Login completed successfully */
          return true;
       }
    
       function logout(){
          global $database;  
    
          if(isset($_COOKIE['cookname'])){
             setcookie("cookname", "", time()-COOKIE_EXPIRE, COOKIE_PATH);
          }
    
          unset($_SESSION['username']);
    
          $this->logged_in = false;
          
       }
    };
    
    
    /* Initialize session object */
    $session = new Session;
    
    /* Initialize form object */
    $form = new Form;
    
    ?>
    
    Code (markup):
    Thanks in advance.
    H.
     
    healingoasis, Jun 16, 2011 IP
  4. mzonas

    mzonas Well-Known Member

    Messages:
    721
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    160
    #4
    Try putting

    
    include("form.php");
    
    Code (markup):
    below the:

    
    class Session
    {
       var $username;            //Username given on sign-up
       var $userid;              //Random value generated on current login
       var $userlevel;           //The level to which the user pertains
       var $time;                //Time user was last active (page loaded)
       var $logged_in;           //True if user is logged in, false otherwise
       var $userinfo = array();  //The array holding all user info
       var $url;                 //The page url current being viewed
       var $referrer;            //Last recorded site page viewed
       var $ip;                  //Remote IP address  
    
       function Session(){
          $this->ip = $_SERVER["REMOTE_ADDR"];
          $this->time = time();
          $this->startSession();
       }
    
       function startSession(){
          global $database;  
          session_start();   
    	  
          /* Determine if user is logged in */
          $this->logged_in = $this->checkLogin();
    
          /* Set referrer page */
          if(isset($_SESSION['url'])){
             $this->referrer = $_SESSION['url'];
          }else{
             $this->referrer = "/";
          }
    
          /* Set current url */
          $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
       }
    
       function checkLogin(){
          global $database; 
          /* Check if user has been remembered */
          if(isset($_COOKIE['cookname'])){
             $this->username = $_SESSION['username'] = $_COOKIE['cookname'];
          }
    
          if(isset($_SESSION['username'])){
             if($database->confirmUserName($_SESSION['username']) != 0){
                unset($_SESSION['username']);
                return false;
             }
    
             $this->userinfo  = $database->getUserInfo($_SESSION['username']);
             $this->username  = $this->userinfo['username'];
             return true;
          }
          else{
             return false;
          }
       }
    
       function login($subuser, $subpass, $subremember){
          global $database, $form;  
    		
    	  unset($form->errors);
    	  /* Checks if this IP address is currently blocked*/	
          $result = $database->confirmIPAddress($this->ip);
    
          if($result == 1){
             $error_type = "access";
             $form->setError($error_type, "Access denied for ".TIME_PERIOD." minutes");
          } 
    
    	  /* Return if form errors exist */
          if($form->num_errors > 0){
             return false;
          }
    	  
    	  $error_type = "attempt";
    
    	  /* Username and password error checking */
          if(!$subuser || !$subpass || strlen($subuser = trim($subuser)) == 0){
             $form->setError($error_type, "Username or password not entered");
          }
    	  
          if($form->num_errors > 0){
             return false;
          }
    
          /* Checks that username is in database and password is correct */
          $subuser = stripslashes($subuser);
          $result = $database->confirmUserPass($subuser, $subpass);
    
          if($result == 1){
             $form->setError($error_type, "Invalid username or password.");
    		 $database->addLoginAttempt($this->ip);
          }
    	  
          if($form->num_errors > 0){
             return false;
          }
    
          /* Username and password correct, register session variables */
          $this->userinfo  = $database->getUserInfo($subuser);
          $this->username  = $_SESSION['username'] = $this->userinfo['username'];
    
          
          /* Null login attempts */
    	  $database->clearLoginAttempts($this->ip);
    
    	  if($subremember){
             setcookie("cookname", $this->username, time()+COOKIE_EXPIRE, COOKIE_PATH);
          }
    
          /* Login completed successfully */
          return true;
       }
    
       function logout(){
          global $database;  
    
          if(isset($_COOKIE['cookname'])){
             setcookie("cookname", "", time()-COOKIE_EXPIRE, COOKIE_PATH);
          }
    
          unset($_SESSION['username']);
    
          $this->logged_in = false;
          
       }
    }
    
    Code (markup):
    And you don't need a semicolon while closing function/class.
     
    mzonas, Jun 16, 2011 IP
  5. healingoasis

    healingoasis Peon

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks, it no longer gives the previous errors but now says:

    Fatal error: Cannot redeclare class Form in /home/alsabah/public_html/4dvd.com/include/form.php on line 3
     
    healingoasis, Jun 16, 2011 IP
  6. mzonas

    mzonas Well-Known Member

    Messages:
    721
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    160
    #6
    Paste form.php as well as database.php (you can obviously hide all passwords and connection details of course) codes if you can. This would let us know what is inside them.
     
    mzonas, Jun 16, 2011 IP
  7. healingoasis

    healingoasis Peon

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Moved both, still getting header error:

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/alsabah/public_html/4dvd.com/include/session.php:1) in /home/alsabah/public_html/4dvd.com/include/session.php on line 22

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/alsabah/public_html/4dvd.com/include/session.php:1) in /home/alsabah/public_html/4dvd.com/include/session.php on line 22
     
    healingoasis, Jun 16, 2011 IP
  8. mzonas

    mzonas Well-Known Member

    Messages:
    721
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    160
    #8
    I reckon that you use session_start();

    What you need to do is to make sure that this code is the first one. THe thing here is that you cannot output any text in front of session_start(); code.
     
    mzonas, Jun 16, 2011 IP
  9. healingoasis

    healingoasis Peon

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I tried to move session start to the begining of the code, but this just left me with a blank page on load.
     
    healingoasis, Jun 16, 2011 IP
  10. tks

    tks Well-Known Member

    Messages:
    89
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    108
    #10
    Try this
    
    if(!isset($_SESSION)) session_start();
    if(!ob_start("ob_gzhandler")) ob_start();
    
    PHP:
     
    tks, Jun 18, 2011 IP
  11. healingoasis

    healingoasis Peon

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Where should I add these codes?
     
    healingoasis, Jun 21, 2011 IP
  12. dakshhmehta

    dakshhmehta Active Member

    Messages:
    220
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    85
    #12
    Try to remove the session_start() from the form.php and database.php if its there.

    Or if its require on both/any file(s), then remove it from class function.

    hope it helps..
     
    dakshhmehta, Jun 21, 2011 IP
  13. healingoasis

    healingoasis Peon

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I still cannot seem to get it lol. Would a coder please contact me and let me know how much it would cost me for them to fix it.

    Thanks in advance,
    H.
     
    healingoasis, Jun 21, 2011 IP