SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Thread: Session Trouble

  1. #1
    SitePoint Zealot Ace02's Avatar
    Join Date
    Jun 2004
    Location
    SitePoint.com
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session Trouble

    Ok, so I made this site on my computer and I have php and mysql and all that stuff running on my comp. My site worked fine while I was just running it off my computer, but now that I've uploaded it I get these session errors in my admin panel:

    PHP Code:
    Warningsession_start(): Cannot send session cookie headers already sent by (output started at /home/gshade/public_html/pinball/control/index.php:2in /home/gshade/public_html/pinball/control/auth.php on line 8

    Warning
    session_start(): Cannot send session cache limiter headers already sent (output started at /home/gshade/public_html/pinball/control/index.php:2in /home/gshade/public_html/pinball/control/auth.php on line 8

    Warning
    Cannot modify header information headers already sent by (output started at /home/gshade/public_html/pinball/control/index.php:2in /home/gshade/public_html/pinball/control/auth.php on line 16 
    Any help anyone can give would be helpful
    ~("~) - Aloha!

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lets see the code....

  3. #3
    SitePoint Addict melchiorus's Avatar
    Join Date
    Jun 2004
    Location
    Indiana
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check line number two of index.php and make sure there aren't any whitespace characters or anything. When in doubt, put ob_start() right above the session_start() function.
    -Melchior (Stephen Craton)

  4. #4
    SitePoint Zealot Ace02's Avatar
    Join Date
    Jun 2004
    Location
    SitePoint.com
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reminder
    lets see the code....
    auth.php
    PHP Code:
    <html>
    <head></head>
    <link rel="stylesheet" href="style.css">
    <?
        
    // include this file in all of your admin pages
        
        // start the session
        
    session_start();
        
        
    // see if the session is authenticated
        
    if( !$_SESSION['auth'] )
        
    // this is where to go if the user is not logged in
        // the user's browser will be re-directed to this page
        // and the page that this is included in will not even
        // be in the history
            
    header"Location: login.php" );
    ?>
    </body>
    </html>
    index.php
    PHP Code:
    <?php
    include "auth.php";
    ?>
    <link rel="stylesheet" href="style.css">
    <center>
    <b><a href="addscore.php">Add a new score</a><br>
    <a href="changescores.php">Edit/Delete Scores</a>
    <a href="logoff.php">Log out</a></b><br><br><br>
    </center>
    login.php
    PHP Code:
    <link rel="stylesheet" href="style.css">
    <?
        
    // start the session
        
    session_start();

    include 
    "config.php";
    $db mysql_connect($db_host,$db_user,$db_pass);
    mysql_select_db ($db_name) or die ("Error! Could not connect to the databse");
        
        
    // if the from has been filled out
        
    if( isset($_POST['submit']) )
        {
            
    // find a matching user
        
    $result mysql_query("
                SELECT user, password
                FROM pinballpass
                WHERE user='
    $_POST[userName]'
                    AND password='
    $_POST[userPass]'");
                    echo 
    "Rows Found: ".mysql_num_rows($result)." - error ".mysql_errno().": ".mysql_error();
            
            
    // if a user is found
            
    if( $row mysql_fetch_array$result ) )
            {
                
    // authenticate the session
                
    $_SESSION['auth'] = true;
                
    // set the user name
                
    $_SESSION['uname'] = $row['user'];
                
    // where to go once logged in
                
    header"Location: index.php" );
            }
            
    // set the failed text
            
    else
                
    $caption "Login failed. Check your user name & password.";
        }
    ?>

    <html><head>
    <title>Login</title>
    </head>
    <body>

    <?
        
    // if the cation is set then diplay it
        
    if( isset($caption) ) echo $caption;
    ?>
    <center>
    <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST">

    User Name:<br>
    <input type="text" name="userName"><br>
    Password:<br>
    <input type="password" name="userPass"><br>
    <input type="submit" name="submit" value="Login">

    </form>
    </center>
    </body>
    </html>
    ~("~) - Aloha!

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this
    PHP Code:
    $_SESSION['auth'] = 1
    and on logout function
    PHP Code:
    $_SESSION['auth'] = 0

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    another thing......
    session_start() goes at the very beggining of the page....
    Code:
    <?php
    session_start();
    ?>
    <html> 
    <head></head>
    let your index.php looks like this
    Code:
       
    <?php
    session_start(); 
    if( !$_SESSION['auth'] ) 
    header( "Location: login.php" );
    ?>
    <html> 
    <head></head>
    now it must work
    cya

  7. #7
    SitePoint Zealot Ace02's Avatar
    Join Date
    Jun 2004
    Location
    SitePoint.com
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reminder
    try this
    PHP Code:
    $_SESSION['auth'] = 1
    and on logout function
    PHP Code:
    $_SESSION['auth'] = 0
    That didn't do anything... isnt that the same exact thing as using 1 for true?
    ~("~) - Aloha!

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    right code
    Code:
       
    <?php
    session_start(); 
    if( !$_SESSION['auth'] ) {
    header( "Location: login.php" );
    }
    ?>
    <html> 
    <head></head>
    happy coding

  9. #9
    SitePoint Zealot Ace02's Avatar
    Join Date
    Jun 2004
    Location
    SitePoint.com
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reminder
    right code
    Code:
       
    <?php
    session_start(); 
    if( !$_SESSION['auth'] ) {
    header( "Location: login.php" );
    }
    ?>
    <html> 
    <head></head>
    happy coding
    Ok, I got most the errors to go away buy putting the session start at the very top of the pages, you just confused me with what I quoted though... Where exactly would I put that?
    ~("~) - Aloha!

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    start of the page of index.php
    and do not include auth.php
    but put like i showed u

  11. #11
    SitePoint Zealot Ace02's Avatar
    Join Date
    Jun 2004
    Location
    SitePoint.com
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alrighty, I did that, and now I have no errors. I tried logging out, and since then I get this browser error on all my admin pages:

    Redirection limit for this URL exceeded. Unable to load the requested page. This may be caused by cookies that are blocked.
    ~("~) - Aloha!

  12. #12
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    internet options
    privacy
    advance
    accept coockies

  13. #13
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also post your logout code.....
    cheers

  14. #14
    SitePoint Zealot Ace02's Avatar
    Join Date
    Jun 2004
    Location
    SitePoint.com
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    logoff.php
    PHP Code:
    <?
        
    // exit if the user is not logged in
        
    include( "auth.php" );
        
        
    // unset the session variables
        
    unset( $_SESSION['auth']);
        
    // destroy the session
        
    session_destroy();
        
        
    // goto the login page
        
    header"Location: login.php" );
    ?>
    ~("~) - Aloha!

  15. #15
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wrong
    PHP Code:
    <? 
        
    // exit if the user is not logged in 
        
    include( "auth.php" ); 
     
        
    // unset the session variables 
        
    unset( $_SESSION['auth']); 
        
    // destroy the session 
        
    session_destroy(); 
     
        
    // goto the login page 
        
    header"Location: login.php" ); 
    ?>
    include dont need couse auth doesnt exsist anymore if u follow my code!!!
    put this into index.php
    PHP Code:
    <?php
    session_start
    ();
    if(!
    $_SESSION["auth"]) {
    header("Location: /login.php");
    }
    $act $_GET["act"];
     
    if(
    $act == 'logout') {
    logout();
    }
     
    function 
    logout() {
    $_SESSION["auth"] = false;
    }
    ?>
    // rest of index page
    now the href for logout
    Code:
    <a href="index.php?act=logout">Logout</a>
    now it have to work

  16. #16
    SitePoint Zealot Ace02's Avatar
    Join Date
    Jun 2004
    Location
    SitePoint.com
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I believe everything should work, but I still can't even access the login place, due to this:


    Redirection limit for this URL exceeded. Unable to load the requested page. This may be caused by cookies that are blocked.


    Maybe I should try shutting off the computer or something
    ~("~) - Aloha!

  17. #17
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    session_start
    ();
    include_once(
    "config.php");
    $act $_GET["act"];
     
    if(
    $act == 'logout') {
    logout();
    }
     
    if(!
    $_SESSION["auth"]) {
    login();
    }
     
    function 
    logout() {
    $_SESSION["auth"] = 0;
    }
     
    function 
    login() {
    $db mysql_connect($db_host,$db_user,$db_pass); 
    mysql_select_db ($db_name) or die ("Error! Could not connect to the databse"); 
     
        
    // if the from has been filled out 
        
    if( isset($_POST['submit']) ) 
        { 
            
    // find a matching user 
        
    $result mysql_query(
                SELECT user, password 
                FROM pinballpass 
                WHERE user='
    $_POST[userName]
                    AND password='
    $_POST[userPass]'"); 
                    echo 
    "Rows Found: ".mysql_num_rows($result)." - error ".mysql_errno().": ".mysql_error(); 
     
            
    // if a user is found 
            
    if( $row mysql_fetch_array$result ) ) 
            { 
                
    // authenticate the session 
                
    $_SESSION['auth'] = 1
                
    // set the user name 
                
    $_SESSION['uname'] = $row['user']; 
            } 
            
    // set the failed text 
            
    else 
                
    $caption "Login failed. Check your user name & password."
        } 
     
        
    // if the cation is set then diplay it 
        
    if( isset($caption) ) echo $caption
    }
    ?> 
    <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST"> 
     
    User Name:<br> 
    <input type="text" name="userName"><br> 
    Password:<br> 
    <input type="password" name="userPass"><br> 
    <input type="submit" name="submit" value="Login"> 
    </form><?php
    }
    ?> 
    // rest of index.php
    for logout
    Code:
    <a href="index.php?act=logout">Logout</a>
    put all this code to index.php and everything will work even your coockies r blocked


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
  •