PHP Session question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dysfunct

    PHP Session question...

    Hey all.
    Trying to get sessions to work on my app. The issue is this. When the
    main page loads I check for a session variable uname to see if the
    user is logged in and display a "hello, username" message, or
    otherwise use "guest." Pretty straightforward . Then I have a login
    form that uses AJAX calls to log the user in. If the login info is
    correct, I set $_SESSION['uname'] to the username. However, if I then
    refresh the main page, the session info is gone. Here's a glimpse at
    what's going down....

    From index.php:
    <?php
    session_start() ;

    ?>
    ....
    <span>Hello, <?php if(isset($_SESS ION['uname'])){
    echo $_SESSION['uname'];
    }
    else {
    echo 'Guest';
    }?></span>

    From login.php:
    <?php

    $dbh=mysql_conn ect ...

    ...


    //do this on login SUCCESS:
    echo 'Login Successful';
    $_SESSION['uname'] = $_POST['un'];
    };


    ?>


    I can't use session_start() in login.php; it gives me an error saying
    http headers have already been sent.

    Any suggestions?
  • Jerry Stuckle

    #2
    Re: PHP Session question...

    dysfunct wrote:
    Hey all.
    Trying to get sessions to work on my app. The issue is this. When the
    main page loads I check for a session variable uname to see if the
    user is logged in and display a "hello, username" message, or
    otherwise use "guest." Pretty straightforward . Then I have a login
    form that uses AJAX calls to log the user in. If the login info is
    correct, I set $_SESSION['uname'] to the username. However, if I then
    refresh the main page, the session info is gone. Here's a glimpse at
    what's going down....
    >
    From index.php:
    <?php
    session_start() ;
    >
    ?>
    ...
    <span>Hello, <?php if(isset($_SESS ION['uname'])){
    echo $_SESSION['uname'];
    }
    else {
    echo 'Guest';
    }?></span>
    >
    From login.php:
    <?php
    >
    $dbh=mysql_conn ect ...
    >
    ...
    >
    >
    //do this on login SUCCESS:
    echo 'Login Successful';
    $_SESSION['uname'] = $_POST['un'];
    };
    >
    >
    ?>
    >
    >
    I can't use session_start() in login.php; it gives me an error saying
    http headers have already been sent.
    >
    Any suggestions?
    >
    You need session_start() at the beginning of EVERY page which uses sessions.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Rik Wasmus

      #3
      Re: PHP Session question...

      On Sat, 12 Apr 2008 03:48:21 +0200, Jerry Stuckle
      <jstucklex@attg lobal.netwrote:
      > I can't use session_start() in login.php; it gives me an error saying
      >http headers have already been sent.
      > Any suggestions?
      You need session_start() at the beginning of EVERY page which uses
      sessions.
      Indeed, and you should be able to start a session on every request where
      it's needed BEFORE generating any output. The error message will tell you
      where the output started, get your session_start() before that.
      --
      Rik Wasmus

      Comment

      Working...