SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Location
    God's Country (Middlesbrough to everyone else)
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session Won't Start

    I have gone over this code for the last hour and can't see anything wrong with it but I'm getting these error messages at the bottom of my page displaying this:

    Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/stefinal/public_html/Login.php:9) in /home/stefinal/public_html/Login.php on line 43

    Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/stefinal/public_html/Login.php:9) in /home/stefinal/public_html/Login.php on line 43


    <?php

    session_start();

    if (isset($HTTP_POST_VARS['loginname']) && isset($HTTP_POST_VARS ['password']))
    {
    $loginname = $HTTP_POST_VARS['loginname'];
    $password = $HTTP_POST_VARS['password'];

    $sql = 'select * from user where login_name='$loginname' and password='$password'";
    $result = mysql_query($sql, $dbh);

    if (mysql_num_rows($result) >0 )
    {
    $HTTP_SESSION_VARS['valid_user'] = $loginname;
    }
    }
    ?>

    Here is the form to go with it:

    <form method="post" action="bands.php">;
    <table width="93%" border="0">
    <tr>
    <td width="43%"><strong>User Name/strong></td>
    <td width="57%"><input type="text" name="loginname"></td>
    </tr>
    <tr>
    <td><strong>Password/strong></td>
    <td><input type="password" name="password"></td>
    </tr>
    <tr>
    <td><inpurt type="submit" value="Log in"></td>
    </tr>
    </table>

    I thought it would be something to do with registering the session variables but then the if statement does that if the login name and passwords are correct so I have no idea what the problem is.

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this the full file? It states line 43 but it doesn't have that many lines.
    Also you use $dbh but it's not defined anywhere in this file.
    Can you post the entire file as is?

    (on a side note you have <inpurt type="submit" value="Log in"> .. should be input )

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Location
    God's Country (Middlesbrough to everyone else)
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I copied and pasted that from an older text file I since corrected that error. The database is connected to from connect.inc.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>DIYMusic::Band Login</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#000000" text="#CCCCCC" link="#666666" vlink="#666666">
    <?php
    include('connect.inc');
    ?>
    <table width="74%" border="0">
    <tr>
    <td width="43%" valign="top"><img src="Login%20Side%20Bar.jpg" width="290" height="500" border="0" usemap="#Map1"></td>
    <td width="57%" align="center" valign="top">
    <p align="justify"><strong>If you have not already registered <a href="Register.php">Register Here</a>, otherwise enter a user name and password. After doing this click the login button.</strong></p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <form method="post" action="bands.php">
    <table width="93%" border="0">
    <tr>
    <td width="43%"><strong>User Name/strong></td>
    <td width="57%"><input type="text" name="loginname"></td>
    </tr>
    <tr>
    <td><strong>Password/strong></td>
    <td><input type="password" name="password"></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" value="Login"></td>
    </tr>

    </table>
    </td>
    </tr>
    </table>
    </form>
    <?php

    session_start();

    if (isset($HTTP_POST_VARS['loginname']) && isset($HTTP_POST_VARS ['password']))
    {
    $loginname = $HTTP_POST_VARS['loginname'];
    $password = $HTTP_POST_VARS['password'];

    $sql = "select * from user where login_name='$loginname' and password='$password'";
    $result = mysql_query($sql, $dbh);

    if (mysql_num_rows($result) >0 )
    {
    $_SESSION['valid_user'] = $loginname;
    }
    }
    ?>

    <map name="Map1" id="Map1">
    <area shape="rect" coords="95,136,197,173" href="index.htm" alt="Go Back to Welcome Page">
    <area shape="rect" coords="93,178,196,214" href="Register.php" alt="Go to Registration Page">
    </map>
    </body>
    </html>

  4. #4
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    session_start() must come before any output, where output is anything sent to the browser (including white space). You have 40 odd lines of html being outputted before it

    The simplest 'quick fix' would be to move <?php session_start(); ?> at the very top of the file before anything is outputted.

  5. #5
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to place session_start() before anything else like this...

    PHP Code:
    <?php session_start(); ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>DIYMusic::Band Login</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#000000" text="#CCCCCC" link="#666666" vlink="#666666">
    <?php
    include('connect.inc');
    ?>
    <table width="74%" border="0">
    <tr>
    <td width="43%" valign="top"><img src="Login%20Side%20Bar.jpg" width="290" height="500" border="0" usemap="#Map1"></td>
    <td width="57%" align="center" valign="top"> 
    <p align="justify"><strong>If you have not already registered <a href="Register.php">Register Here</a>, otherwise enter a user name and password. After doing this click the login button.</strong></p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <form method="post" action="bands.php"> 
    <table width="93%" border="0">
    <tr>
    <td width="43%"><strong>User Name:</strong></td>
    <td width="57%"><input type="text" name="loginname"></td>
    </tr>
    <tr>
    <td><strong>Password:</strong></td>
    <td><input type="password" name="password"></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" value="Login"></td>
    </tr>

    </table>
    </td>
    </tr>
    </table>
    </form>
    <?php

    if (isset($HTTP_POST_VARS['loginname']) && isset($HTTP_POST_VARS ['password']))
    {
    $loginname $HTTP_POST_VARS['loginname'];
    $password $HTTP_POST_VARS['password'];

    $sql "select * from user where login_name='$loginname' and password='$password'";
    $result mysql_query($sql$dbh);

    if (
    mysql_num_rows($result) >)
    {
    $_SESSION['valid_user'] = $loginname;
    }
    }
    ?>

    <map name="Map1" id="Map1">
    <area shape="rect" coords="95,136,197,173" href="index.htm" alt="Go Back to Welcome Page">
    <area shape="rect" coords="93,178,196,214" href="Register.php" alt="Go to Registration Page">
    </map>
    </body>
    </html>

  6. #6
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got there just before me markl999

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Location
    God's Country (Middlesbrough to everyone else)
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doh. This is my first go at building a site with php, you can't tell can you?

    Thanks alot.

  8. #8
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by UOT_Ste
    Doh. This is my first go at building a site with php, you can't tell can you?

    Thanks alot.
    Don't be stupid, I have been coding in PHP for 3 years and still make stupid mistakes, probably says more about me though

  9. #9
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Everyone gets hits by that one at some point. At least you've passed it now.
    Next up .. undefined index errors ..

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Location
    God's Country (Middlesbrough to everyone else)
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One question (I was going to say last but it won't be). My next page is going to be my user page. Does the session just continue automaticly onto the next page once the username and password are validated?

  11. #11
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, sort of, session vars are available to all pages that use sessions, that is those that call session_start().
    If you don't call session_start() then you can't access the session vars.

  12. #12
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Location
    God's Country (Middlesbrough to everyone else)
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all your help.

    One last thing. The page loads without any errors as such when I enter my username and password, but I have done an if statement for the user page and it always displays the incorrect username or password option, even if I enter a valid username and password.

    ///////////////////////login.php

    <?php
    session_start();
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>DIYMusic::Band Login</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#000000" text="#CCCCCC" link="#666666" vlink="#666666">
    <?php
    include('connect.inc');
    ?>
    <table width="74%" border="0">
    <tr>
    <td width="43%" valign="top"><img src="Login%20Side%20Bar.jpg" width="290" height="500" border="0" usemap="#Map1"></td>
    <td width="57%" align="center" valign="top">
    <p align="justify"><strong>If you have not already registered access the registration page from the link on the left hand side, otherwise login below.</strong></p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <form method="post" action="bands.php">
    <table width="93%" border="0">
    <tr>
    <td width="43%"><strong>User Name/strong></td>
    <td width="57%"><input type="text" name="loginname"></td>
    </tr>
    <tr>
    <td><strong>Password/strong></td>
    <td><input type="password" name="password"></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" value="Login"></td>
    </tr>

    </table>
    </td>
    </tr>
    </table>
    </form>
    <?php

    if (isset($HTTP_POST_VARS['loginname']) && isset($HTTP_POST_VARS ['password']))
    {
    $loginname = $HTTP_POST_VARS['loginname'];
    $password = $HTTP_POST_VARS['password'];

    $sql = "select * from user where login_name='$loginname' and password='$password'";
    $result = mysql_query($sql, $dbh);

    if (mysql_num_rows($result) >0 )
    {
    $_SESSION['valid_user'] = $loginname;
    }
    }
    ?>

    <map name="Map1" id="Map1">
    <area shape="rect" coords="95,136,197,173" href="index.htm" alt="Go Back to Welcome Page">
    <area shape="rect" coords="93,178,196,214" href="Register.php" alt="Go to Registration Page">
    </map>
    </body>
    </html>

    /////////////////////////bands.php

    <?php
    session_start()
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Welcome to DIYMusic.com</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#000000" text="#CCCCCC" link="#666666" vlink="#666666">
    <?php
    if(isset($_SESSION['valid_user']))
    {
    echo '<table width="83%" border="0">';

    echo'<tr><td width="53%" height="305" valign="top"><img src="Register%20Side%20Bar.jpg" width="290" height="500" border="0" usemap="#Map"></td>
    <td width="53%" valign="top"><p>Hello '.$_SESSION['valid_user'].', welcome to the backstage area!</p></td></tr>';

    echo'</table>';

    echo'<map name="Map">
    <area shape="rect" coords="97,138,198,172" href="index.htm" alt="Go Back to Welcome Page">';
    echo'<area shape="rect" coords="95,180,196,213" href="Login.php" alt="Go Back to Login">';
    }
    else
    {
    echo'<table width="83%" border="0">';
    echo'<tr><td width="53%" height="305" valign="top"><img src="Register%20Side%20Bar.jpg" width="290" height="500" border="0" usemap="#Map"></td>
    <td width="53%" valign="top"><p>You are not logged in, this is either because your username or password was entered incorrectly or you have opened this page without attempting to log in. Go back to the login page and try again.</p></td></tr>';

    echo'</table>';

    echo'<map name="Map">
    <area shape="rect" coords="97,138,198,172" href="index.htm" alt="Go Back to Welcome Page">
    <area shape="rect" coords="95,180,196,213" href="Login.php" alt="Go Back to Login">';

    }

    ?>

    </map>
    </body>
    </html>

    It will probably be something simple again.

  13. #13
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your login.php page posts to bands.php, but all your login validation code is on login.php, so it will never be reached.
    Change (in login.php):
    <form method="post" action="bands.php">
    to:
    <form method="post" action="login.php">

    and after this line:
    $_SESSION['valid_user'] = $loginname;

    add:
    header("Location: bands.php");
    exit();

  14. #14
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Location
    God's Country (Middlesbrough to everyone else)
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got this error message: :'(

    Warning: Cannot modify header information - headers already sent by (output started at /home/stefinal/public_html/Login.php:12) in /home/stefinal/public_html/Login.php on line 58

  15. #15
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah my bad, i didn't get the error myself but i can see why you would. You really want to restructure the login page so that all the login stuff comes before any ouput, something like:
    PHP Code:
    <?php
    session_start
    ();
    include(
    'connect.inc');
    if (isset(
    $_POST['loginname']) && isset($_POST['password'])){
        
    $sql "select * from user where login_name='".$_POST['loginname']."'
            and password='"
    .$_POST['password']."'";
        
    $result mysql_query($sql$dbh);
        if (
    mysql_num_rows($result) >) {
            
    $_SESSION['valid_user'] = $_POST['loginname'];
            
    header("Location: bands.php");
            exit;
        }
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>DIYMusic::Band Login</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#000000" text="#CCCCCC" link="#666666" vlink="#666666">
    <table width="74%" border="0">
    <tr>
    <td width="43%" valign="top"><img src="Login%20Side%20Bar.jpg" width="290" height="500" border="0" usemap="#Map1"></td>
    <td width="57%" align="center" valign="top">
    <p align="justify"><strong>If you have not already registered access the registration page from the link on the left hand side, otherwise login below.</strong></p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <form method="post" action="login.php">
    <table width="93%" border="0">
    <tr>
    <td width="43%"><strong>User Name:</strong></td>
    <td width="57%"><input type="text" name="loginname"></td>
    </tr>
    <tr>
    <td><strong>Password:</strong></td>
    <td><input type="password" name="password"></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" value="Login"></td>
    </tr>

    </table>
    </td>
    </tr>
    </table>
    </form>
    <map name="Map1" id="Map1">
    <area shape="rect" coords="95,136,197,173" href="index.htm" alt="Go Back to Welcome Page">
    <area shape="rect" coords="93,178,196,214" href="Register.php" alt="Go to Registration Page">
    </map>
    </body>
    </html>

  16. #16
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Location
    God's Country (Middlesbrough to everyone else)
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK thanks, I'll bear this in mind in future.

  17. #17
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Location
    God's Country (Middlesbrough to everyone else)
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now it's telling me the mysql_num_rows() is not a valid argument.

  18. #18
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You did remember to put your mysql_connect() line in there?
    I was lazy and left out the mysql_error() calls that should be there and will tell you what's wrong, but i suspect you've forgotten to connect to the db *shrug*

  19. #19
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Location
    God's Country (Middlesbrough to everyone else)
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No hadn't forgotten, it was just it had a problem with the header function and said it was down to the connect.inc file so I took it out and manually put the code in, must be something wrong with it, I'll try and work it out on my own anyway, thanks for all your help today.


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
  •