1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

session problem

Discussion in 'PHP' started by Lpspider, Nov 8, 2006.

  1. #1
    Okay I've been trying to get the sessions to work but this is what I get:


    Here's the error I get:

    
    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/www/html/webdev/uploads/index.php:6) in /var/www/html/webdev/uploads/index.php on line 7
    
    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/html/webdev/uploads/index.php:6) in /var/www/html/webdev/uploads/index.php on line 7
    You left the username or password field blank.
    
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/html/webdev/uploads/index.php on line 59
    Welcome
    PHP:
    Here's the code:
    
    <?php
    session_start(); 
    
       echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
       echo "<head><title> {$title} </title>";
    
       echo '<style type="text/css">
    
             #forms { position:absolute;top:75px;left:200px;align:right;padding-bottom:5px;}
             #text { position:absolute; top:75px; left:125px; align:left;} 
             .text2 { padding-top:8px;} 
             .forms2 { padding-bottom:6px;} 
             #login {position:absolute;top:100px;left:75%;} 
             .nav{position:absolute; top:75px; left:10%; right:10%; width:80%;background-color:#0099FF;text-align:right;a}
             .input{width:100px;} </style>
              </head><body>';
       
    
    $logout = '<a href="logout.php">Logout</a>';
    
       echo '<div class="nav"><a href="index.php">Home</a> |', $logout  , ' | <a href="register.php">Register</a> | <a href="links.php">Links</a> | <a href="faq.php">FAQ</a></div>';
    
    
       echo "<div id=\"login\"><form action=\"index.php\" method=\"post\">
             Login:<br>
             Username: <input type=\"text\" name=\"username\" cols=\"60\" class=\"input\"><br>
             Password: <input type=\"password\" name=\"password\" cols=\"60\" class=\"input\"><br>
             <input type=\"submit\"></form></div>";
    
    
    
    
    mysql_connect(localhost,username,pass);
    
    
    //check to see if sessions are set if not set them 
    if(!isset($_SESSION['user_name']) || !isset($_SESSION['user_password'])){ 
    if(!empty($_POST['username']) || !empty($_POST['userpassword'])){ 
    $_SESSION['username'] = $_POST['username']; 
    $_SESSION['password'] = $_POST['password']; 
    }
    else{ 
    echo'You left the username or password field blank.'; 
    } 
    } 
    
    $sql = "SELECT * FROM user WHERE User = '$_SESSION[username]' && Password='$_SESSION[password]'"; 
    $result = mysql_query($sql); 
    $row = mysql_fetch_array($result); 
    //compare sessions to those in table 
    //Incorrect username and password 
    if($_SESSION['username'] != $row['username'] && $_SESSION['password'] != $row['password']){ 
    $_SESSION = array(); 
    if (isset($_COOKIE[session_name()])) { 
    setcookie(session_name(), '', time()-42000, '/'); 
    }
     
    @session_destroy(); 
    echo'Incorrect Username or password'; 
    } 
    //correct username and password 
    else{ 
    echo'Welcome '.$_SESSION['username']; 
    } 
    
    
    
    
        echo '</body></html>';
    
    
    
    
    ?>
    
    
    PHP:
    Basically what I need the session to remember the username/password that it calls from the database for the user to log in.
     
    Lpspider, Nov 8, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You are trying to set the session after you output something to the browser. This cannot be done unless you use something called output buffering.
     
    mad4, Nov 8, 2006 IP
  3. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #3
    1. Delete everything before "<?php" (spaces, tabs, new lines...)
    2. You don't need sessions in this case...
     
    SoKickIt, Nov 8, 2006 IP
  4. Lpspider

    Lpspider Well-Known Member

    Messages:
    2,216
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    160
    #4
    1. Might be my problem. I have a space before <?php

    2. I know. I have it working without sessions - I want to add the ability to change data in the database - and I need sessions for that. ;)
     
    Lpspider, Nov 8, 2006 IP
  5. Lpspider

    Lpspider Well-Known Member

    Messages:
    2,216
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    160
    #5
    Okay, I got rid of the dead space. Now I get this error:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/html/webdev/uploads/index.php on line 50
    Welcome 
    
    PHP:
     
    Lpspider, Nov 9, 2006 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Replace this
    
    $result = mysql_query($sql); 
    
    PHP:
    with this and see what error you get
    
    $result = mysql_query($sql) OR die( mysql_error() );
    
    PHP:
     
    nico_swd, Nov 9, 2006 IP
  7. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #7
    Replace:

    $sql = "SELECT * FROM user WHERE User = '$_SESSION[username]' && Password='$_SESSION[password]'"; 
    Code (markup):
    with:

    $sql = "SELECT * FROM user WHERE User = '" . mysql_real_escape_string($_SESSION[username]) . "' AND Password='" . mysql_real_escape_string($_SESSION[password]) . "'"; 
    Code (markup):
     
    SoKickIt, Nov 9, 2006 IP
  8. Lpspider

    Lpspider Well-Known Member

    Messages:
    2,216
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    160
    #8
    Okay, I did some of the things recommeneded. The error result I get is that there is no database selected. But there should be.

    <?php
    session_start(); 
    
    mysql_connect(localhost,username,pass);
    
       echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
       echo "<head><title> {$title} </title>";
    
       echo '<style type="text/css">
    
             #forms { position:absolute;top:75px;left:200px;align:right;padding-bottom:5px;}
             #text { position:absolute; top:75px; left:125px; align:left;} 
             .text2 { padding-top:8px;} 
             .forms2 { padding-bottom:6px;} 
             #login {position:absolute;top:100px;left:75%;} 
             .nav{position:absolute; top:75px; left:10%; right:10%; width:80%;background-color:#0099FF;text-align:right;a}
             .input{width:100px;} </style>
              </head><body>';
       
    
    $logout = '<a href="logout.php">Logout</a>';
    
    //menu
       echo '<div class="nav"><a href="index.php">Home</a> |', $logout  , ' | <a href="register.php">Register</a> | <a href="links.php">Links</a> | <a href="faq.php">FAQ</a></div>';
    
    //login form
       echo '<div id="login"><form action="index.php" method="post">
             Login:<br>
             Username: <input type="text" name="username" cols="60" class="input"><br>
             Password: <input type="password" name="password" cols="60" class="input"><br>
             <input type="submit"></form></div>';
    
    
    
    $connection = mysql_connect("localhost", "username", "pass");
    
    if (!connection)
    {
    die('Could not connect to database:' . mysql_error());
    }
    
    
    //check to see if sessions are set if not set them 
    if(!isset($_SESSION['username']) or !isset($_SESSION['password'])){ 
    if(!empty($_POST['username']) or !empty($_POST['password'])){ 
    $_SESSION['username'] = $_POST['username']; 
    $_SESSION['password'] = $_POST['password']; 
    }
    else{ 
    echo'You left the username or password field blank.'; 
    } 
    } 
    
    
    $sql = "SELECT User, Password FROM user WHERE User = '$_SESSION[username]' AND Password='$_SESSION[password]'"; 
    $result = mysql_query($sql) OR die( mysql_error() ); 
    $row = mysql_fetch_array($result, MYSQL_NUM); 
    //compare sessions to those in table 
    //Incorrect username and password 
    if($_SESSION['username'] != $row['username'] AND $_SESSION['password'] != $row['password']){ 
    $_SESSION = array(); 
    if (isset($_COOKIE[session_name()])) { 
    setcookie(session_name(), '', time()-42000, '/'); 
    }
     
    @session_destroy(); 
    echo'Incorrect Username or password'; 
    } 
    //correct username and password 
    else{ 
    echo'Welcome '.$_SESSION['username']; 
    } 
    
    
    
    
        echo '</body></html>';
    
    
    
    
    ?>
    
    
    
    
    
    PHP:
     
    Lpspider, Nov 10, 2006 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    I don't see where you select a database. I see you connecting to it twice, but not actally selecting a database.

    
    mysql_select_db($dbname);
    
    PHP:
     
    nico_swd, Nov 10, 2006 IP
    Lpspider likes this.
  10. Lpspider

    Lpspider Well-Known Member

    Messages:
    2,216
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    160
    #10
    Ah. Of course.

    What I did was modify this code from existing stuff I had... must have edited it out in the process I guess. Thanks.
     
    Lpspider, Nov 10, 2006 IP
  11. Lpspider

    Lpspider Well-Known Member

    Messages:
    2,216
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    160
    #11
    Here is the slightly updated code. However, it still spits out an error.

    <?php
    session_start(); 
    
       echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
       echo "<head><title> {$title} </title>";
    
       echo '<style type="text/css">
    
             #forms { position:absolute;top:75px;left:200px;align:right;padding-bottom:5px;}
             #text { position:absolute; top:75px; left:125px; align:left;} 
             .text2 { padding-top:8px;} 
             .forms2 { padding-bottom:6px;} 
             #login {position:absolute;top:100px;left:75%;} 
             .nav{position:absolute; top:75px; left:10%; right:10%; width:80%;background-color:#0099FF;text-align:right;a}
             .input{width:100px;} </style>
              </head><body>';
       
    
    $logout = '<a href="logout.php">Logout</a>';
    
    //menu
       echo '<div class="nav"><a href="index.php">Home</a> |', $logout  , ' | <a href="register.php">Register</a> | <a href="links.php">Links</a> | <a href="faq.php">FAQ</a></div>';
    
    //login form
       echo '<div id="login"><form action="index.php" method="post">
             Login:<br>
             Username: <input type="text" name="username" cols="60" class="input"><br>
             Password: <input type="password" name="password" cols="60" class="input"><br>
             <input type="submit"></form></div>';
    
    $connection = mysql_connect("localhost", "username", "password");
    
    if (!connection)
    {
    die('Could not connect to database:' . mysql_error());
    }
    mysql_select_db("database", $connection); 
    
    //check to see if sessions are set if not set them 
    if(!isset($_SESSION['username']) or !isset($_SESSION['password'])){ 
    if(!empty($_POST['username']) or !empty($_POST['password'])){ 
    $_SESSION['username'] = $_POST['username']; 
    $_SESSION['password'] = $_POST['password']; 
    }
    else{ 
    echo'You left the username or password field blank.'; 
    } 
    } 
    
    
    $sql = "SELECT User, Password FROM user WHERE User = '$_SESSION[username]' AND Password='$_SESSION[password]'"; 
    $result = mysql_query($sql) OR die( mysql_error() ); 
    $row = mysql_fetch_array($result, MYSQL_NUM); 
    //compare sessions to those in table 
    //Incorrect username and password 
    if($_SESSION['username'] != $row['username'] AND $_SESSION['password'] != $row['password']){ 
    $_SESSION = array(); 
    if (isset($_COOKIE[session_name()])) { 
    setcookie(session_name(), '', time()-42000, '/'); 
    }
     
    @session_destroy(); 
    echo'Incorrect Username or password'; 
    } 
    //correct username and password 
    else{ 
    echo'Welcome '.$_SESSION['username']; 
    } 
    
        echo '</body></html>';
    
    ?>
    
    
    PHP:
    Error:

    Warning: mysql_connect(): Access denied for user: 'username@localhost' (Using password: YES) in /var/www/localhost/htdocs/webdev/uploads/index.php on line 31
    
    Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /var/www/localhost/htdocs/webdev/uploads/index.php on line 37
    No Database Selected
    PHP:
     
    Lpspider, Nov 14, 2006 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    You have to replace username and password with your actual username and password for the database access.

    
    mysql_connect("localhost", "username", "password");
    
    PHP:
     
    nico_swd, Nov 14, 2006 IP
  13. Lpspider

    Lpspider Well-Known Member

    Messages:
    2,216
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    160
    #13
    ^ I know. I did. I just changed them for this post.
     
    Lpspider, Nov 14, 2006 IP
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #14
    The error you're gettig says the login details are wrong.
     
    nico_swd, Nov 15, 2006 IP
  15. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #15
    You can't echo any content to the browser before you set the cookie. You are echoing right at the top of the script.
     
    mad4, Nov 15, 2006 IP
  16. Lpspider

    Lpspider Well-Known Member

    Messages:
    2,216
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    160
    #16
    ugh. *sigh* Still not getting it. I get errors whenever I do it any other way. Any suggestions?
     
    Lpspider, Nov 26, 2006 IP
  17. Luke

    Luke Peon

    Messages:
    111
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Just to test, replace

    $connection = mysql_connect("localhost", "username", "password");
    if (!connection){die('Could not connect to database:' . mysql_error());}
    mysql_select_db("database", $connection); 
    PHP:
    with

    
    $db=mysql_connect("localhost","yourusername","yourpassword");
    mysql_select_db("yourdatabasename", $db);
    
    PHP:
    and see if it works, if so, check your database base is definately correct and that the log in name and password you are using definately has access to said database.
     
    Luke, Nov 27, 2006 IP
  18. Lpspider

    Lpspider Well-Known Member

    Messages:
    2,216
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    160
    #18
    The reason I was getting database connection errors is that I recently had a server move. It will connect now, though it still doesn't login/work with sessions.

    Here's what I have:
    <?php
    session_start(); 
    
    
    
    $db=mysql_connect("localhost","username","password");
    mysql_select_db("database", $db);
    
    
    
    //check to see if sessions are set if not set them 
    if(!isset($_SESSION['username']) or !isset($_SESSION['password'])){ 
    if(!empty($_POST['username']) or !empty($_POST['password'])){ 
    $_SESSION['username'] = $_POST['username']; 
    $_SESSION['password'] = $_POST['password']; 
    }
    else{ 
    echo'You left the username or password field blank.'; 
    } 
    }
    
    
    $_POST['username'] = $_SESSION['username']; 
    $_POST['password'] = $_SESSION['password']; 
    
    $sql = "SELECT User, Password FROM user WHERE User = '$_SESSION[username]' AND Password ='$_SESSION[password]'"; 
    $result = mysql_query($sql) OR die( mysql_error() ); 
    $row = mysql_fetch_array($result, MYSQL_NUM); 
    //compare sessions to those in table 
    //Incorrect username and password 
    if($_SESSION['username'] !== $row['username'] AND $_SESSION['password'] !== $row['password']){ 
    $_SESSION = array(); 
    if (isset($_COOKIE[session_name()])) { 
    setcookie(session_name(), '', time()-42000, '/'); 
    }
     
    @session_destroy(); 
    echo'Incorrect Username or password'; 
    
    //debug info
    echo $_SESSION['username'] ."and ". $_SESSION['password'];
    
    } 
    
    //correct username and password 
    else{ 
    echo'Welcome '. $_SESSION['username']; 
    } 
    
    
    
    
       echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
       echo '<head><title> {$title} </title>';
    
       echo '<style type="text/css">
    
             #forms { position:absolute;top:75px;left:200px;align:right;padding-bottom:5px;}
             #text { position:absolute; top:75px; left:125px; align:left;} 
             .text2 { padding-top:8px;} 
             .forms2 { padding-bottom:6px;} 
             #login {position:absolute;top:100px;left:75%;} 
             .nav{position:absolute; top:75px; left:10%; right:10%; width:80%;background-color:#0099FF;text-align:right;a}
             .input{width:100px;} </style>
              </head><body>';
       
    
    $logout = '<a href="logout.php">Logout</a>';
    
    //menu
       echo '<div class="nav"><a href="index.php">Home</a> |', $logout  , ' | <a href="register.php">Register</a> | <a href="links.php">Links</a> | <a href="faq.php">FAQ</a></div>';
    
    //login form
       echo '<div id="login"><form action="index.php" method="post">
             Login:<br>
             Username: <input type="text" name="username" cols="60" class="input"><br>
             Password: <input type="password" name="password" cols="60" class="input"><br>
             <input type="submit"></form></div>';
    
    
    
    
        echo '</body></html>';
    
    ?>
    
    PHP:
    Basically it spits out "welcome," and nothing. If I enter username and password (which is should call to database/compare with sessions) it says "" and "". It should say "useranme" and "password".

    What am I missing? Advice/guidance is appreciated.
     
    Lpspider, Nov 28, 2006 IP
  19. Lpspider

    Lpspider Well-Known Member

    Messages:
    2,216
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    160
    #19
    ...Anyone???
     
    Lpspider, Dec 4, 2006 IP
  20. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #20
    remove $_SESSION = array(); your emptying the session array which by the way, is always an array, the above line is never needed.
     
    krakjoe, Dec 4, 2006 IP