header/session start question

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

    header/session start question

    Hi.

    I am having a few header problems at the moment with a login page. I
    don't have my headers at the top of the page where I've learned I need
    to have them. However, I also know I'm supposed to have the
    session_start() ; at the top of the page as well. So when you have two
    things that need to be the first which do you put first? And if I put
    one before the other will that cause problems?

    Below is a copy of the php from my page. It is the header("Locatio n:
    ". parts at the bottom in the registering session variables section
    that causes the error. Would I just lift out the header locations
    parts and put them to the top?

    Thanks for any help

    John




    <?php
    session_start() ;
    // *** Validate request to login to this site.
    //session_start() ;


    // Report all PHP errors (bitwise 63 may be used in PHP 3)
    error_reporting (E_ALL);
    ?>

    <?php require_once('C onnections/conn_newland.ph p'); ?>



    <?php

    // *** Validate request to login to this site.


    $loginFormActio n = $_SERVER['PHP_SELF'];
    if (isset($accessc heck)) {
    $GLOBALS['PrevUrl'] = $accesscheck;
    session_registe r('PrevUrl');
    }

    if (isset($_POST['username'])) {
    $loginUsername= $_POST['username'];
    $password=$_POS T['pwd'];
    $MM_fldUserAuth orization = "userGroup" ;
    $MM_redirectLog inSuccess = "index.php" ;
    $MM_redirectLog inFailed = "login_failed.p hp";
    $MM_redirecttoR eferrer = true;
    mysql_select_db ($database_conn _newland, $conn_newland);

    $LoginRS__query =sprintf("SELEC T username, pwd, userGroup FROM
    tbl_users WHERE username='%s' AND pwd='%s'",
    get_magic_quote s_gpc() ? $loginUsername :
    addslashes($log inUsername), get_magic_quote s_gpc() ? $password :
    addslashes($pas sword));

    $LoginRS = mysql_query($Lo ginRS__query, $conn_newland) or
    die(mysql_error ());
    $loginFoundUser = mysql_num_rows( $LoginRS);
    if ($loginFoundUse r) {

    $loginStrGroup = mysql_result($L oginRS,0,'userG roup');

    //declare two session variables and assign them
    $GLOBALS['MM_Username'] = $loginUsername;
    $GLOBALS['MM_UserGroup'] = $loginStrGroup;

    //register the session variables
    session_registe r("MM_Username" );
    session_registe r("MM_UserGroup ");

    if (isset($_SESSIO N['PrevUrl']) && true) {
    $MM_redirectLog inSuccess = $_SESSION['PrevUrl'];
    }
    header("Locatio n: ". $MM_redirectLog inSuccess );
    }
    else {
    header("Locatio n: ". $MM_redirectLog inFailed );
    }
    }
    ?>



  • Janwillem Borleffs

    #2
    Re: header/session start question

    John wrote:[color=blue]
    > I am having a few header problems at the moment with a login page. I
    > don't have my headers at the top of the page where I've learned I need
    > to have them. However, I also know I'm supposed to have the
    > session_start() ; at the top of the page as well. So when you have two
    > things that need to be the first which do you put first? And if I put
    > one before the other will that cause problems?
    >[/color]

    What header problems do you have? Starting with a call to session_start() ,
    followed by a header() call shouldn't cause any problems.

    Even several header() calls do not cause errors, because headers are send
    only when content is displayed or at the end of the script (whatever comes
    first):

    <?php

    // header not send
    header("X-Header: Foo");
    header("X-Header-Description: Bar");

    // headers are send
    print "Hello";

    // Error because the headers are already sent
    header("Locatio n: http://example.com/");
    ?>

    Or:

    <?php

    // header not send
    header("X-Header: Foo");
    header("X-Header-Description: Bar");
    header("Locatio n: http://example.com/");

    // No more lines of code, headers will be send
    ?>

    Even the above examples would work when you instruct PHP to wait sending the
    output through output buffering (see http://www.php.net/ob_start).


    JW



    Comment

    • John

      #3
      Re: header/session start question

      On Sun, 24 Oct 2004 09:22:26 +0200, A strange species called
      "Janwillem Borleffs" <jw@jwscripts.c om> wrote:
      [color=blue]
      >What header problems do you have? Starting with a call to session_start() ,
      >followed by a header() call shouldn't cause any problems.
      >
      >Even several header() calls do not cause errors, because headers are send
      >only when content is displayed or at the end of the script (whatever comes
      >first):
      >
      ><?php
      >
      > // header not send
      > header("X-Header: Foo");
      > header("X-Header-Description: Bar");
      >
      > // headers are send
      > print "Hello";
      >
      > // Error because the headers are already sent
      > header("Locatio n: http://example.com/");
      >?>
      >
      >Or:
      >
      ><?php
      >
      > // header not send
      > header("X-Header: Foo");
      > header("X-Header-Description: Bar");
      > header("Locatio n: http://example.com/");
      >
      > // No more lines of code, headers will be send
      >?>
      >
      >Even the above examples would work when you instruct PHP to wait sending the
      >output through output buffering (see http://www.php.net/ob_start).
      >
      >
      >JW[/color]

      Hi.

      I receive the following error:

      Warning: Cannot modify header information - headers already sent by
      (output started at /home/xxxxx/public_html/newland/login.php:11) in
      /home/xxxxx/public_html/newland/login.php on line 55

      This is line 55 to 61 below:

      header("Locatio n: ". $MM_redirectLog inSuccess );
      }
      else {
      header("Locatio n: ". $MM_redirectLog inFailed );
      }
      }
      ?>

      Do I just move that to the top then after the session start?

      John





      <?php
      session_start() ;
      // *** Validate request to login to this site.
      //session_start() ;


      // Report all PHP errors (bitwise 63 may be used in PHP 3)
      error_reporting (E_ALL);
      ?>

      <?php require_once('C onnections/conn_newland.ph p'); ?>



      <?php

      // *** Validate request to login to this site.


      $loginFormActio n = $_SERVER['PHP_SELF'];
      if (isset($accessc heck)) {
      $GLOBALS['PrevUrl'] = $accesscheck;
      session_registe r('PrevUrl');
      }

      if (isset($_POST['username'])) {
      $loginUsername= $_POST['username'];
      $password=$_POS T['pwd'];
      $MM_fldUserAuth orization = "userGroup" ;
      $MM_redirectLog inSuccess = "index.php" ;
      $MM_redirectLog inFailed = "login_failed.p hp";
      $MM_redirecttoR eferrer = true;
      mysql_select_db ($database_conn _newland, $conn_newland);

      $LoginRS__query =sprintf("SELEC T username, pwd, userGroup FROM
      tbl_users WHERE username='%s' AND pwd='%s'",
      get_magic_quote s_gpc() ? $loginUsername :
      addslashes($log inUsername), get_magic_quote s_gpc() ? $password :
      addslashes($pas sword));

      $LoginRS = mysql_query($Lo ginRS__query, $conn_newland) or
      die(mysql_error ());
      $loginFoundUser = mysql_num_rows( $LoginRS);
      if ($loginFoundUse r) {

      $loginStrGroup = mysql_result($L oginRS,0,'userG roup');

      //declare two session variables and assign them
      $GLOBALS['MM_Username'] = $loginUsername;
      $GLOBALS['MM_UserGroup'] = $loginStrGroup;

      //register the session variables
      session_registe r("MM_Username" );
      session_registe r("MM_UserGroup ");

      if (isset($_SESSIO N['PrevUrl']) && true) {
      $MM_redirectLog inSuccess = $_SESSION['PrevUrl'];
      }
      header("Locatio n: ". $MM_redirectLog inSuccess );
      }
      else {
      header("Locatio n: ". $MM_redirectLog inFailed );
      }
      }
      ?>


      Comment

      • Theo

        #4
        Re: header/session start question

        John <dog@tanion.com > wrote in
        news:ho2on0hnem gml4nfuppfst1ct 8fu53hk8c@4ax.c om:
        [color=blue]
        > On Sun, 24 Oct 2004 09:22:26 +0200, A strange species called
        > "Janwillem Borleffs" <jw@jwscripts.c om> wrote:
        >[color=green]
        >>What header problems do you have? Starting with a call to
        >>session_start (), followed by a header() call shouldn't cause any
        >>problems.
        >>
        >>Even several header() calls do not cause errors, because headers are
        >>send only when content is displayed or at the end of the script
        >>(whatever comes first):
        >>
        >><?php
        >>
        >> // header not send
        >> header("X-Header: Foo");
        >> header("X-Header-Description: Bar");
        >>
        >> // headers are send
        >> print "Hello";
        >>
        >> // Error because the headers are already sent
        >> header("Locatio n: http://example.com/");
        >>?>
        >>
        >>Or:
        >>
        >><?php
        >>
        >> // header not send
        >> header("X-Header: Foo");
        >> header("X-Header-Description: Bar");
        >> header("Locatio n: http://example.com/");
        >>
        >> // No more lines of code, headers will be send
        >>?>
        >>
        >>Even the above examples would work when you instruct PHP to wait
        >>sending the output through output buffering (see
        >>http://www.php.net/ob_start).
        >>
        >>
        >>JW[/color]
        >
        > Hi.
        >
        > I receive the following error:
        >
        > Warning: Cannot modify header information - headers already sent by
        > (output started at /home/xxxxx/public_html/newland/login.php:11) in
        > /home/xxxxx/public_html/newland/login.php on line 55
        >
        > This is line 55 to 61 below:
        >
        > header("Locatio n: ". $MM_redirectLog inSuccess );
        > }
        > else {
        > header("Locatio n: ". $MM_redirectLog inFailed );
        > }
        > }
        > ?>
        >
        > Do I just move that to the top then after the session start?
        >
        > John
        >
        >
        >
        >
        >
        > <?php
        > session_start() ;
        > // *** Validate request to login to this site.
        > //session_start() ;
        >
        >
        > // Report all PHP errors (bitwise 63 may be used in PHP 3)
        > error_reporting (E_ALL);
        > ?>
        >
        > <?php require_once('C onnections/conn_newland.ph p'); ?>
        >
        >
        >
        > <?php
        >
        > // *** Validate request to login to this site.
        >
        >
        > $loginFormActio n = $_SERVER['PHP_SELF'];
        > if (isset($accessc heck)) {
        > $GLOBALS['PrevUrl'] = $accesscheck;
        > session_registe r('PrevUrl');
        > }
        >
        > if (isset($_POST['username'])) {
        > $loginUsername= $_POST['username'];
        > $password=$_POS T['pwd'];
        > $MM_fldUserAuth orization = "userGroup" ;
        > $MM_redirectLog inSuccess = "index.php" ;
        > $MM_redirectLog inFailed = "login_failed.p hp";
        > $MM_redirecttoR eferrer = true;
        > mysql_select_db ($database_conn _newland, $conn_newland);
        >
        > $LoginRS__query =sprintf("SELEC T username, pwd, userGroup FROM
        > tbl_users WHERE username='%s' AND pwd='%s'",
        > get_magic_quote s_gpc() ? $loginUsername :
        > addslashes($log inUsername), get_magic_quote s_gpc() ? $password :
        > addslashes($pas sword));
        >
        > $LoginRS = mysql_query($Lo ginRS__query, $conn_newland) or
        > die(mysql_error ());
        > $loginFoundUser = mysql_num_rows( $LoginRS);
        > if ($loginFoundUse r) {
        >
        > $loginStrGroup = mysql_result($L oginRS,0,'userG roup');
        >
        > //declare two session variables and assign them
        > $GLOBALS['MM_Username'] = $loginUsername;
        > $GLOBALS['MM_UserGroup'] = $loginStrGroup;
        >
        > //register the session variables
        > session_registe r("MM_Username" );
        > session_registe r("MM_UserGroup ");
        >
        > if (isset($_SESSIO N['PrevUrl']) && true) {
        > $MM_redirectLog inSuccess = $_SESSION['PrevUrl'];
        > }
        > header("Locatio n: ". $MM_redirectLog inSuccess );
        > }
        > else {
        > header("Locatio n: ". $MM_redirectLog inFailed );
        > }
        > }
        > ?>
        >
        >[/color]

        whats in your require_once file?

        Comment

        • John

          #5
          Re: header/session start question

          On Sun, 24 Oct 2004 20:39:13 -0000, A strange species called Theo
          <invalid@noemai l.com> wrote:
          [color=blue]
          >John <dog@tanion.com > wrote in
          >news:ho2on0hne mgml4nfuppfst1c t8fu53hk8c@4ax. com:
          >[color=green]
          >> On Sun, 24 Oct 2004 09:22:26 +0200, A strange species called
          >> "Janwillem Borleffs" <jw@jwscripts.c om> wrote:
          >>[color=darkred]
          >>>What header problems do you have? Starting with a call to
          >>>session_star t(), followed by a header() call shouldn't cause any
          >>>problems.
          >>>
          >>>Even several header() calls do not cause errors, because headers are
          >>>send only when content is displayed or at the end of the script
          >>>(whatever comes first):
          >>>
          >>><?php
          >>>
          >>> // header not send
          >>> header("X-Header: Foo");
          >>> header("X-Header-Description: Bar");
          >>>
          >>> // headers are send
          >>> print "Hello";
          >>>
          >>> // Error because the headers are already sent
          >>> header("Locatio n: http://example.com/");
          >>>?>
          >>>
          >>>Or:
          >>>
          >>><?php
          >>>
          >>> // header not send
          >>> header("X-Header: Foo");
          >>> header("X-Header-Description: Bar");
          >>> header("Locatio n: http://example.com/");
          >>>
          >>> // No more lines of code, headers will be send
          >>>?>
          >>>
          >>>Even the above examples would work when you instruct PHP to wait
          >>>sending the output through output buffering (see
          >>>http://www.php.net/ob_start).
          >>>
          >>>
          >>>JW[/color]
          >>
          >> Hi.
          >>
          >> I receive the following error:
          >>
          >> Warning: Cannot modify header information - headers already sent by
          >> (output started at /home/xxxxx/public_html/newland/login.php:11) in
          >> /home/xxxxx/public_html/newland/login.php on line 55
          >>
          >> This is line 55 to 61 below:
          >>
          >> header("Locatio n: ". $MM_redirectLog inSuccess );
          >> }
          >> else {
          >> header("Locatio n: ". $MM_redirectLog inFailed );
          >> }
          >> }
          >> ?>
          >>
          >> Do I just move that to the top then after the session start?
          >>
          >> John
          >>
          >>
          >>
          >>
          >>
          >> <?php
          >> session_start() ;
          >> // *** Validate request to login to this site.
          >> //session_start() ;
          >>
          >>
          >> // Report all PHP errors (bitwise 63 may be used in PHP 3)
          >> error_reporting (E_ALL);
          >> ?>
          >>
          >> <?php require_once('C onnections/conn_newland.ph p'); ?>
          >>
          >>
          >>
          >> <?php
          >>
          >> // *** Validate request to login to this site.
          >>
          >>
          >> $loginFormActio n = $_SERVER['PHP_SELF'];
          >> if (isset($accessc heck)) {
          >> $GLOBALS['PrevUrl'] = $accesscheck;
          >> session_registe r('PrevUrl');
          >> }
          >>
          >> if (isset($_POST['username'])) {
          >> $loginUsername= $_POST['username'];
          >> $password=$_POS T['pwd'];
          >> $MM_fldUserAuth orization = "userGroup" ;
          >> $MM_redirectLog inSuccess = "index.php" ;
          >> $MM_redirectLog inFailed = "login_failed.p hp";
          >> $MM_redirecttoR eferrer = true;
          >> mysql_select_db ($database_conn _newland, $conn_newland);
          >>
          >> $LoginRS__query =sprintf("SELEC T username, pwd, userGroup FROM
          >> tbl_users WHERE username='%s' AND pwd='%s'",
          >> get_magic_quote s_gpc() ? $loginUsername :
          >> addslashes($log inUsername), get_magic_quote s_gpc() ? $password :
          >> addslashes($pas sword));
          >>
          >> $LoginRS = mysql_query($Lo ginRS__query, $conn_newland) or
          >> die(mysql_error ());
          >> $loginFoundUser = mysql_num_rows( $LoginRS);
          >> if ($loginFoundUse r) {
          >>
          >> $loginStrGroup = mysql_result($L oginRS,0,'userG roup');
          >>
          >> //declare two session variables and assign them
          >> $GLOBALS['MM_Username'] = $loginUsername;
          >> $GLOBALS['MM_UserGroup'] = $loginStrGroup;
          >>
          >> //register the session variables
          >> session_registe r("MM_Username" );
          >> session_registe r("MM_UserGroup ");
          >>
          >> if (isset($_SESSIO N['PrevUrl']) && true) {
          >> $MM_redirectLog inSuccess = $_SESSION['PrevUrl'];
          >> }
          >> header("Locatio n: ". $MM_redirectLog inSuccess );
          >> }
          >> else {
          >> header("Locatio n: ". $MM_redirectLog inFailed );
          >> }
          >> }
          >> ?>
          >>
          >>[/color]
          >
          >whats in your require_once file?[/color]

          That contains a database with a few different tables. One of the
          tables has all the usernames and passwords for people who register.
          This current page that I'm having these problems with is the login
          page.

          Cheers

          John


          Comment

          • Theo

            #6
            Re: header/session start question

            John <quak@duck.co m> wrote in news:r83pn091af pila6kb4kg8uj6k 4bcajijcl@
            4ax.com:
            [color=blue]
            > That contains a database with a few different tables. One of the
            > tables has all the usernames and passwords for people who register.
            > This current page that I'm having these problems with is the login
            > page.
            >
            > Cheers
            >
            > John[/color]

            unless Im mistaken the line requiring that file is the one it doesnt
            like. So maybe something in it is causing the problem.

            I use something similar... a correct login redirects to the next page,
            and its in a function call. But for it to work the call must be made
            before there are any print statements or html code of any kind (not
            incluind sprintf, which doesnt output anything). Also, try putting an
            exit() after each header, just in case.

            Comment

            • René

              #7
              Re: header/session start question

              I would say the errors are the spaces you introduced and these were output
              (Marked with "HERE >" in your code). Once a character is send, which is
              HTML, no header can be sent anymore, hence the error when you send the
              Location header. You should remove any text outside the PHP tags also in the
              required file.


              --
              René
              comunica2.com
              Web Hosting, design and translations

              [color=blue]
              > <?php
              > session_start() ;
              > // *** Validate request to login to this site.
              > //session_start() ;
              >
              >
              > // Report all PHP errors (bitwise 63 may be used in PHP 3)
              > error_reporting (E_ALL);
              > ?>[/color]
              HERE >[color=blue]
              > <?php require_once('C onnections/conn_newland.ph p'); ?>[/color]
              HERE >
              HERE >
              HERE >[color=blue]
              > <?php
              >
              > // *** Validate request to login to this site.
              >
              >
              > $loginFormActio n = $_SERVER['PHP_SELF'];
              > if (isset($accessc heck)) {
              > $GLOBALS['PrevUrl'] = $accesscheck;
              > session_registe r('PrevUrl');
              > }
              >
              > if (isset($_POST['username'])) {
              > $loginUsername= $_POST['username'];
              > $password=$_POS T['pwd'];
              > $MM_fldUserAuth orization = "userGroup" ;
              > $MM_redirectLog inSuccess = "index.php" ;
              > $MM_redirectLog inFailed = "login_failed.p hp";
              > $MM_redirecttoR eferrer = true;
              > mysql_select_db ($database_conn _newland, $conn_newland);
              >
              > $LoginRS__query =sprintf("SELEC T username, pwd, userGroup FROM
              > tbl_users WHERE username='%s' AND pwd='%s'",
              > get_magic_quote s_gpc() ? $loginUsername :
              > addslashes($log inUsername), get_magic_quote s_gpc() ? $password :
              > addslashes($pas sword));
              >
              > $LoginRS = mysql_query($Lo ginRS__query, $conn_newland) or
              > die(mysql_error ());
              > $loginFoundUser = mysql_num_rows( $LoginRS);
              > if ($loginFoundUse r) {
              >
              > $loginStrGroup = mysql_result($L oginRS,0,'userG roup');
              >
              > //declare two session variables and assign them
              > $GLOBALS['MM_Username'] = $loginUsername;
              > $GLOBALS['MM_UserGroup'] = $loginStrGroup;
              >
              > //register the session variables
              > session_registe r("MM_Username" );
              > session_registe r("MM_UserGroup ");
              >
              > if (isset($_SESSIO N['PrevUrl']) && true) {
              > $MM_redirectLog inSuccess = $_SESSION['PrevUrl'];
              > }
              > header("Locatio n: ". $MM_redirectLog inSuccess );
              > }
              > else {
              > header("Locatio n: ". $MM_redirectLog inFailed );
              > }
              > }
              > ?>
              >
              >[/color]


              Comment

              Working...