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 11 of 11
  1. #1
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts

    Session Start and check login

    Hi, I want to add "Welcome Back $user" to my index.php page. It's not working I get the following error.

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/rodgameo/public_html/projects/dmv/index.php:1) in /home/rodgameo/public_html/projects/dmv/index.php on line 35
    I figure this is because SESSION_START needs to be on line one. But it still isn't working.

    This is the very top of my page.

    PHP Code:
    <?php
    mysql_connect
    ('localhost''user''pass') or die(mysql_error());
    mysql_select_db('database') or die(mysql_error());
    ?>
    and this is in the script in the spot where I want to check.

    PHP Code:
        <li>
       <?php
       session_start
    ();
    if(!empty(
    $_SESSION["user_id"])){
      
    $who mysql_query("SELECT username FROM users WHERE id='".$_SESSION['user_id']."'");
      
    $row mysql_fetch_array($who);
    echo 
    "<b>Welcome Back ".$row['username']."!</b>";
    }


    ?>
                            </li>
    How can I get it to check the session and pull the username from the database both?
    Last edited by masterofollies; 02-27-2009 at 03:08 AM.

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    It's because of the html output before the call to session_start().

    Move session_start() to the very top of the file.

  • #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    I tried and it still didn't fix it.
    I put it right after the <?php

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Are you including this file into a file that has previous output as well?
    The same rules apply, session_start needs to be called prior to any output to the browser, otherwise you'll need to use output buffering which I don't really recommend for these types of purposes.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    No include files, straight html other then this part.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    When you removed the <li>, did you also remove the whitespace before the <?php as well? The whitespace is also considered output.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    No the <li> is in the body. This is what the top looked like.

    PHP Code:
    <?php
    session_start
    (); 
    mysql_connect('localhost''user''pass') or die(mysql_error());
    mysql_select_db('database') or die(mysql_error());
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>East Coast Entrepreneurs</title>
    <link href="style.css" rel="stylesheet" type="text/css" media="screen" />
    </head>
    <body>

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Ok yeah, and this code here:
    PHP Code:
        <li> 
       <?php 
       session_start
    (); 
    if(!empty(
    $_SESSION["user_id"])){ 
      
    $who mysql_query("SELECT username FROM users WHERE id='".$_SESSION['user_id']."'"); 
      
    $row mysql_fetch_array($who); 
    echo 
    "<b>Welcome Back ".$row['username']."!</b>"



    ?> 
                            </li>
    Is being included further into the script you just posted correct? In that case, you don't need the session_start() in the code I just posted since it already exists from the parent page scope.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Yeah its about halfway down the script in the menu bar. So just don't use session_start and it should be golden?

  • #10
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,026
    Thanks
    2
    Thanked 315 Times in 307 Posts
    You are working in index.php and from the error - output started at /home/rodgameo/public_html/projects/dmv/index.php:1

    If there is nothing in your source code before the <?php tag, your file is probably saved in UTF-8 encoding and the BOM (Byte Order Mark) characters are the content being output on line 1.

    Save your file as ANSII/ASCII or if you must save it as UTF-8 encoding, save it without the BOM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #11
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    I was wondering what those were. I accidently changed it and didn't know what it used to be so I thought UTF-8 would be a good choice, but I guess not. I changed to ANSI.


  •  

    Posting Permissions

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