session problem

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

    session problem

    this script don't work

    Script A: set session variable


    <?php
    class Page0A {
    function Page0A(){
    $this->init();
    $this->save();
    header("Locatio n: page0b.php");
    }
    function init(){
    session_start() ;
    }
    function save(){
    global $_SESSION;
    $_SESSION["name"] = "fabio";
    }
    }
    $p = new Page0A();
    ?>


    Script B retrive variable

    <?php
    class Page0B{
    function Page0B(){
    $this->init();
    $this->display();
    }
    function init(){
    session_start() ;
    }
    function display(){
    global $_SESSION;
    echo $_SESSION["name"];
    }
    }
    $p = new Page0B();
    ?>

    this NOT WORK for me ! i don't undestand!
  • Jan Pieter Kunst

    #2
    Re: session problem

    Hywel wrote:[color=blue]
    > this script don't work
    >
    > Script A: set session variable
    >
    >
    > <?php
    > class Page0A {
    > function Page0A(){
    > $this->init();
    > $this->save();
    > header("Locatio n: page0b.php");
    > }
    > function init(){
    > session_start() ;
    > }
    > function save(){
    > global $_SESSION;
    > $_SESSION["name"] = "fabio";
    > }
    > }
    > $p = new Page0A();
    > ?>
    >
    >
    > Script B retrive variable
    >
    > <?php
    > class Page0B{
    > function Page0B(){
    > $this->init();
    > $this->display();
    > }
    > function init(){
    > session_start() ;
    > }
    > function display(){
    > global $_SESSION;
    > echo $_SESSION["name"];
    > }
    > }
    > $p = new Page0B();
    > ?>
    >
    > this NOT WORK for me ! i don't undestand![/color]

    I did not verify if this is the problem, but you don't need to use

    global $_SESSION;

    because $_SESSION is a 'superglobal' which is automatically known in all
    scopes. Try it without the 'global $_SESSION;' lines and see if that helps.

    JP

    --
    Sorry, <devnull@cauce. org> is a spam trap.
    Real e-mail address unavailable. 5000+ spams per month.

    Comment

    • Andy Hassall

      #3
      Re: session problem

      On 12 Jan 2005 07:43:16 -0800, f.dearcangelis@ essematica.it (Hywel) wrote:
      [color=blue]
      >this script don't work
      >
      >Script A: set session variable
      >
      ><?php
      >class Page0A {
      > function Page0A(){
      > $this->init();
      > $this->save();
      > header("Locatio n: page0b.php");[/color]

      Location headers require an absolute URL, not a relative URL.
      [color=blue]
      > }
      > function init(){
      > session_start() ;
      > }
      > function save(){
      > global $_SESSION;[/color]

      Not required - $_SESSION is 'superglobal'.
      [color=blue]
      > $_SESSION["name"] = "fabio";
      > }
      >}
      >$p = new Page0A();
      >?>
      >
      >
      >Script B retrive variable
      >
      ><?php
      >class Page0B{
      > function Page0B(){
      > $this->init();
      > $this->display();
      > }
      > function init(){
      > session_start() ;
      > }
      > function display(){
      > global $_SESSION;[/color]

      Not required - $_SESSION is 'superglobal'.
      [color=blue]
      > echo $_SESSION["name"];
      > }
      >}
      >$p = new Page0B();
      >?>
      >
      >this NOT WORK for me ! i don't undestand![/color]

      What does it do? It prints "fabio" when I run it.

      --
      Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
      <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

      Comment

      • Erwin Moller

        #4
        Re: session problem

        Andy Hassall wrote:
        [color=blue]
        > On 12 Jan 2005 07:43:16 -0800, f.dearcangelis@ essematica.it (Hywel) wrote:
        >[color=green]
        >>this script don't work
        >>
        >>Script A: set session variable
        >>
        >><?php
        >>class Page0A {
        >>function Page0A(){
        >>$this->init();
        >>$this->save();
        >>header("Locat ion: page0b.php");[/color]
        >
        > Location headers require an absolute URL, not a relative URL.[/color]

        Hi ANdy,

        Not true.
        I often use relative URLs and that works on all browsers.

        Regards,
        Erwin Moller

        Comment

        • Michael Fesser

          #5
          Re: session problem

          .oO(Erwin Moller)
          [color=blue]
          >Andy Hassall wrote:
          >[color=green]
          >> Location headers require an absolute URL, not a relative URL.[/color]
          >
          >Not true.[/color]

          Not true. It's required by the HTTP spec.
          [color=blue]
          >I often use relative URLs and that works on all browsers.[/color]

          Did you test really _all_ browsers? Surely not. They don't have to
          accept relative URLs, even if most of them probably do. The HTTP spec
          clearly states "The field value consists of a single absolute URI."

          And with PHP it's rather trivial to automatically build an absolute URL
          from a relative one, using predefined variables in $_SERVER.

          Micha

          Comment

          • Andy Hassall

            #6
            Re: session problem

            On Thu, 13 Jan 2005 12:47:11 +0100, Erwin Moller
            <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> wrote:
            [color=blue][color=green]
            >> Location headers require an absolute URL, not a relative URL.[/color]
            >
            >Not true.
            >I often use relative URLs and that works on all browsers.[/color]

            I'll see your unsubstantiated empirical claim, and raise you an official
            standard:



            "
            14.30 Location

            The Location response-header field is used to redirect the recipient to a
            location other than the Request-URI for completion of the request or
            identification of a new resource. For 201 (Created) responses, the Location is
            that of the new resource which was created by the request. For 3xx responses,
            the location SHOULD indicate the server's preferred URI for automatic
            redirection to the resource. The field value consists of a single absolute URI.

            Location = "Location" ":" absoluteURI
            "

            Some browsers may correct for your mistakes, but that doesn't make it right.
            For instance, Lynx prints a warning 'Location URL is not absolute.' when
            following a relative (i.e. malformed) Location header.

            --
            Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
            <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

            Comment

            • John Dunlop

              #7
              HTTP Location header field value

              Andy Hassall wrote:
              [color=blue]
              > Some browsers may correct for your mistakes, but that doesn't make it right.
              > For instance, Lynx prints a warning 'Location URL is not absolute.' when
              > following a relative (i.e. malformed) Location header.[/color]

              Who can name a browser, however archaic, that doesn't
              correct this error?

              --
              Jock

              Comment

              • porneL

                #8
                Re: HTTP Location header field value

                [color=blue]
                > Who can name a browser, however archaic, that doesn't
                > correct this error?[/color]

                Probably you could find a bunch of web bots, download managers and other
                HTTP implementations .
                HTTP RFC is quite clear on this, and I see no reason to disrespect it,
                despite browsers tolerance.

                --
                * html {redirect-to: url(http://browsehappy.pl) ;}

                Comment

                • Erwin Moller

                  #9
                  Re: session problem

                  Michael Fesser wrote:
                  [color=blue]
                  > .oO(Erwin Moller)
                  >[color=green]
                  >>Andy Hassall wrote:
                  >>[color=darkred]
                  >>> Location headers require an absolute URL, not a relative URL.[/color]
                  >>
                  >>Not true.[/color]
                  >
                  > Not true. It's required by the HTTP spec.
                  >[color=green]
                  >>I often use relative URLs and that works on all browsers.[/color]
                  >
                  > Did you test really _all_ browsers? Surely not. They don't have to
                  > accept relative URLs, even if most of them probably do. The HTTP spec
                  > clearly states "The field value consists of a single absolute URI."
                  >
                  > And with PHP it's rather trivial to automatically build an absolute URL
                  > from a relative one, using predefined variables in $_SERVER.
                  >
                  > Micha[/color]

                  Thanks Micha (and Andy in the other thread) for correcting me.
                  Sorry for the misinformation.

                  I read up, and yes: you are right of course: It is all in the rfc.
                  This means all the zillion times I used the relative location are all wrong.
                  :-(
                  *Erwin slaps hit head 10 times against the wall*
                  auw.

                  Micha wrote:[color=blue]
                  > And with PHP it's rather trivial to automatically build an absolute URL
                  > from a relative one, using predefined variables in $_SERVER.[/color]

                  Trivial when you have done it maybe. :P

                  But I want to feed the absolute to the header.

                  I made a script test.php, and ran it locally.
                  It spitted out everything in $_SERVER.

                  I *think* the following info should be enough:

                  key:SERVER_NAME value:localhost
                  key:REQUEST_URI value:/~erwin/test.php

                  With that information I can write a function that takes a relative path and
                  translates it into a absolute on.
                  Right?

                  So something like:

                  function makePathAbsolut e($relPath){
                  $path = $_SERVER["SERVER_NAM E"].$_SERVER["REQUEST_UR I"];
                  // 1) remove everything after the last /
                  // 2) remove everyting after the second last / for each ../ found at the
                  beginning of $relPath
                  // 3) remover all (possible repeating) ../ from $relPath
                  // 4) $path .= $relPath;
                  return $path;
                  }

                  Micha, can you do that in 1 line with regexp? ;-)
                  (Just a joke, I can do it myself now)

                  Thanks!
                  I'll improve my location-headers.

                  Regards,
                  Erwin Moller

                  Comment

                  • Michael Fesser

                    #10
                    Re: session problem

                    .oO(Erwin Moller)
                    [color=blue]
                    >Michael Fesser wrote:
                    >[color=green]
                    >> And with PHP it's rather trivial to automatically build an absolute URL
                    >> from a relative one, using predefined variables in $_SERVER.[/color]
                    >
                    >Trivial when you have done it maybe. :P[/color]

                    Copy & paste from the manual. ;-þ



                    But the full example given there is not always necessary. Dependent on
                    your site structure it might be enough to just use $_SERVER['HTTP_HOST']
                    and the filename to build an absolute URL.

                    Using dirname() also takes care of subdirectories if necessary.

                    Micha

                    Comment

                    • Erwin Moller

                      #11
                      Re: session problem

                      Michael Fesser wrote:
                      [color=blue]
                      > .oO(Erwin Moller)
                      >[color=green]
                      >>Michael Fesser wrote:
                      >>[color=darkred]
                      >>> And with PHP it's rather trivial to automatically build an absolute URL
                      >>> from a relative one, using predefined variables in $_SERVER.[/color]
                      >>
                      >>Trivial when you have done it maybe. :P[/color]
                      >
                      > Copy & paste from the manual. ;-þ
                      >
                      > http://www.php.net/header
                      >
                      > But the full example given there is not always necessary. Dependent on
                      > your site structure it might be enough to just use $_SERVER['HTTP_HOST']
                      > and the filename to build an absolute URL.
                      >
                      > Using dirname() also takes care of subdirectories if necessary.
                      >
                      > Micha[/color]

                      Thanks Micha,

                      Great advise as usual. :-)

                      Have a nice weekend!

                      Regards,
                      Erwin Moller

                      Comment

                      Working...