SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sessions and header problem

    I'm new to php and have been trying to teach myself for the last few months. I had a site completely finished through a test url and everything including php pages that talked to my mysql database worked perfectly. Yesterday I put the site on it's intended url and now my back end admin pages are not working. I imported the original database and changed all the needed names for my connection page. What appears to be the problem is that session_start is conflicting with header commands. These are the errors that I get.

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/64/7074664/html/pages/logver.php:1) in /home/content/64/7074664/html/pages/logver.php on line 28

    Warning: Cannot modify header information - headers already sent by (output started at /home/content/64/7074664/html/pages/logver.php:1) in /home/content/64/7074664/html/pages/logver.php on line 30

    and this is my code for logver.php:

    PHP Code:
    <?php
        
    include "connect.php";
        echo 
    "<div class='thanks'>";
        
    $user $_POST['myusername'];
        
    $pass $_POST['mypassword'];
        
        if(
    $user&&$pass)
        {
        
    mysql_connect($hostname$username,$password )or die("can't connect");
        
    mysql_select_db($dbname) or die("Couldn't find DB");
        
        
    $sql "SELECT * FROM users WHERE `username`='".$user."'";
        
    $result mysql_query($sql);
        
    $numrows mysql_num_rows($result);
        
        if (
    $numrows!=0){
            while(
    $row mysql_fetch_assoc($result))
            {
                
    $dbuser $row['username'];
                
    $dbpass $row['password'];
                }
                if(
    $user==$dbuser&&$pass==$dbpass){
                    
    session_start();
                    
    session_register('varUser');
                    
    header ("Location: admin_page.php");
                    }else{
                        include 
    "login.php";
                        echo 
    "That password is incorrect";
                        }
            }else{
                include 
    "login.php";
            die (
    "That user doesn't exist");
            }
        }
        else{
            include 
    "login.php";
            die (
    "Please enter username and password");
        }
        echo 
    "</div>";
    ?>
    I do have a small css style that proceeds this, but when I tried to take it out it actually led to more confusion (which I may have to fix on another page later), so it's left in for now.

    As I understand my session_start may need to be at the top of the page? I've tried this but it hasn't helped me any. Besides I don't see how I can start the session before I've entered the username and password?

    Why might this work perfectly on my test site and not on the actual site?

    Any help here would be much appreciated.
    Thanks

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,077
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    What version of php are you using? session_register() was depreciated as of version 5.3.x

    session_start needs to be in the first line after the <?php as it stands the line

    PHP Code:
    echo "<div class='thanks'>"
    is output which is causing the headers to be sent
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a good question. I have session_register in logver.php and
    PHP Code:
    if(session_is_registered('varUser')) 
    in admin_page.php. So those would definitely be causing problems.

    So first off, should I start_session() before I even include connect.php?

    How do I update session_register('varUser') ? I've seen some talk about using $_SESSION but how do I apply that to my code?

    And last should my code on admin_page.php:

    PHP Code:
    if(session_is_registered('varUser')) 
    be changed to this

    PHP Code:
    if(isset($_SESSION['varUser'])) 
    thanks for your help. I'm starting to understand some basic things about php, but I'm still very much perplexed by it.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I didn't answer your question. Truth is, I don't know exactly which version of php I'm working with. The database I'm working with is 5.0 but I don't think that helps. I would assume that my original files were an older version, and this newer one is the up to date version, how would I designate though?

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You maybe able to glean something form this quick example.
    PHP Code:
    <?php
    if('POST' === $_SERVER['REQUEST_METHOD']){
      
    #connect to server
      
    $con mysql_connect('host''user''pass');
      
    #select database
      
    mysql_select_db('schema'$con);
      
    #if the user supplied a username and password
      
    if(!empty($_POST['username']) && !empty($_POST['password'])){
        
    #find matching user
        
    $res mysql_query(sprintf(
          
    "SELECT * FROM user WHERE username = '%s' AND password ='%s' LIMIT 1",
          
    mysql_real_escape_string($_POST['username']),
          
    mysql_real_escape_string($_POST['password'])
        ));
        
    #if we found a user
        
    if(=== mysql_num_rows($res)){
          
    session_start();
          
    #save the user info in the session
          
    $_SESSION['user'] = mysql_fetch_assoc($res);
          
    #send to admin section
          
    header('Location: admin_page.php');
          exit;
        }
      }
    }
    #send back to login form
    header('Location: login.php');
    exit;
    I tried to comment where appropriate.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome!

    It works perfectly! Thanks for all of your help.

  7. #7
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    220
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    if u have written thousands lines of code and came out with header already sent problem, reading through and debugging isn't quite an option, u can try a quick fix with output buffering, ob_start() at the beginning of ur script.

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2010
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you wrote thousands of lines of code which you have troubles debugging and you have such a bug, I'd say that having the "headers already sent" message the least of your problems.


Bookmarks

Posting Permissions

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