Passing objects from one page to another

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

    Passing objects from one page to another

    I have a person class adn i want to derive an object of that class on one
    page and pass that object to a next page but that does not work for me and i
    do not understand why.

    Here is de code:

    class.person.ph p
    -----------------------------------
    class person {

    private $_name;

    public function setName($name) {

    $this->_name = $name;

    }

    public function getName() {

    return $this->_name;

    }

    }
    --------------------------------------------



    person1.php // here is the object created
    -------------------------------------------
    require_once("c lass.person.php ");

    $p = new person();

    $p->setName("Jim") ;

    $safep = urlencode(seria lize($p));

    header("Locatio n:person2.php?d ata=".$safep);

    exit;


    person2.php // page where the object is transfered to
    ---------------------------------------------------
    require_once("c lass.person.php ");

    $p = unserialize(url decode($_GET['data']));

    echo $p->getName();


  • ZeldorBlat

    #2
    Re: Passing objects from one page to another


    Marcel wrote:
    I have a person class adn i want to derive an object of that class on one
    page and pass that object to a next page but that does not work for me and i
    do not understand why.
    >
    Here is de code:
    >
    class.person.ph p
    -----------------------------------
    class person {
    >
    private $_name;
    >
    public function setName($name) {
    >
    $this->_name = $name;
    >
    }
    >
    public function getName() {
    >
    return $this->_name;
    >
    }
    >
    }
    --------------------------------------------
    >
    >
    >
    person1.php // here is the object created
    -------------------------------------------
    require_once("c lass.person.php ");
    >
    $p = new person();
    >
    $p->setName("Jim") ;
    >
    $safep = urlencode(seria lize($p));
    >
    header("Locatio n:person2.php?d ata=".$safep);
    >
    exit;
    >
    >
    person2.php // page where the object is transfered to
    ---------------------------------------------------
    require_once("c lass.person.php ");
    >
    $p = unserialize(url decode($_GET['data']));
    >
    echo $p->getName();
    Difficult to say since you haven't told us what doesn't work. As in
    you get some error that you can't unserialize it? Or no error at all
    (as in your error reporting is turned off)?

    My guess would be that
    $p = unserialize(url decode($_GET['data']));
    should really be just
    $p = unserialize($_G ET['data']);

    But I didn't test it.

    Comment

    • Marcel

      #3
      Re: Passing objects from one page to another


      "ZeldorBlat " <zeldorblat@gma il.comschreef in bericht
      news:1162998447 .934912.233680@ i42g2000cwa.goo glegroups.com.. .
      >
      Marcel wrote:
      >I have a person class adn i want to derive an object of that class on one
      >page and pass that object to a next page but that does not work for me
      >and i
      >do not understand why.
      >>
      >Here is de code:
      >>
      >class.person.p hp
      >-----------------------------------
      >class person {
      >>
      > private $_name;
      >>
      > public function setName($name) {
      >>
      > $this->_name = $name;
      >>
      > }
      >>
      > public function getName() {
      >>
      > return $this->_name;
      >>
      > }
      >>
      >}
      >--------------------------------------------
      >>
      >>
      >>
      >person1.php // here is the object created
      >-------------------------------------------
      >require_once(" class.person.ph p");
      >>
      >$p = new person();
      >>
      >$p->setName("Jim") ;
      >>
      >$safep = urlencode(seria lize($p));
      >>
      >header("Locati on:person2.php? data=".$safep);
      >>
      >exit;
      >>
      >>
      >person2.php // page where the object is transfered to
      >---------------------------------------------------
      >require_once(" class.person.ph p");
      >>
      >$p = unserialize(url decode($_GET['data']));
      >>
      >echo $p->getName();
      >
      Difficult to say since you haven't told us what doesn't work. As in
      you get some error that you can't unserialize it? Or no error at all
      (as in your error reporting is turned off)?
      >
      My guess would be that
      $p = unserialize(url decode($_GET['data']));
      should really be just
      $p = unserialize($_G ET['data']);
      >
      But I didn't test it.
      >
      Yes you are right, the errorcode i am getting is:

      Fatal error: Call to a member function getName() on a non-object in
      D:\public_html\ person2.php on line 7

      Regards,

      Marcel


      Comment

      • Marcin Dobrucki

        #4
        Re: Passing objects from one page to another

        Marcel wrote:
        I have a person class adn i want to derive an object of that class on one
        page and pass that object to a next page but that does not work for me and i
        do not understand why.
        ....
        I tried this code, and it works just fine. You didn't include the rest
        of the code, so maybe there is something there that breaks it?

        <?php
        class A {
        var $name;
        function A($name) {
        $this->name = $name; }
        function getName() {
        return $this->name; }
        }

        $a = new A("my name");
        $a_serialized = urlencode(seria lize($a));
        header("Locatio n: obj2.php?data=" .$a_serialized) ;
        ?>

        and:

        <?php
        class A {
        var $name;
        function A($name) {
        $this->name = $name; }
        function getName() {
        return $this->name; }
        }

        $a = unserialize(url decode($_GET['data']));
        echo get_class($a);
        echo "<br>";
        echo $a->getName();
        ?>

        Comment

        • Marcin Dobrucki

          #5
          Re: Passing objects from one page to another

          Marcel wrote:
          Yes you are right, the errorcode i am getting is:
          >
          Fatal error: Call to a member function getName() on a non-object in
          D:\public_html\ person2.php on line 7
          One step at a time: what do you get out of "urldecode" and then what
          do you get from "unserializ e"? Check the return values.

          Comment

          • Marcel

            #6
            Re: Passing objects from one page to another


            "Marcin Dobrucki" <Marcin.Dobruck i@TAKETHISAWAY. nokia.comschree f in
            bericht news:lJm4h.4249 0$_k2.774821@ne ws2.nokia.com.. .
            Marcel wrote:
            >I have a person class adn i want to derive an object of that class on one
            >page and pass that object to a next page but that does not work for me
            >and i do not understand why.
            ...
            I tried this code, and it works just fine. You didn't include the rest of
            the code, so maybe there is something there that breaks it?
            >
            <?php
            class A {
            var $name;
            function A($name) {
            $this->name = $name; }
            function getName() {
            return $this->name; }
            }
            >
            $a = new A("my name");
            $a_serialized = urlencode(seria lize($a));
            header("Locatio n: obj2.php?data=" .$a_serialized) ;
            ?>
            >
            and:
            >
            <?php
            class A {
            var $name;
            function A($name) {
            $this->name = $name; }
            function getName() {
            return $this->name; }
            }
            >
            $a = unserialize(url decode($_GET['data']));
            echo get_class($a);
            echo "<br>";
            echo $a->getName();
            ?>
            Hi Marcin,

            I tried your example but i am getting the same error: Call to a member
            function getName() on a non-object in

            Maybe it is a php.ini issue? I do not know what can be another reason....
            maybe magicquotes have to do something with this?

            Do you have any ideas?

            Marcel



            Comment

            • Marcel

              #7
              Re: Passing objects from one page to another


              "Marcin Dobrucki" <Marcin.Dobruck i@TAKETHISAWAY. nokia.comschree f in
              bericht news:VKm4h.4249 1$_k2.774821@ne ws2.nokia.com.. .
              Marcel wrote:
              >
              >Yes you are right, the errorcode i am getting is:
              >>
              >Fatal error: Call to a member function getName() on a non-object in
              >D:\public_html \person2.php on line 7
              >
              One step at a time: what do you get out of "urldecode" and then what do
              you get from "unserializ e"? Check the return values.
              It seems the unserialize function does not seem to work properly... i will
              do a little research at that first ok...


              Comment

              • thehuby@gmail.com

                #8
                Re: Passing objects from one page to another

                Wouldn't you be better using sessions?

                If you register a session variable you won't need to use the query
                string and you won't need to serialize and deserialise it.

                Take a look at http://uk2.php.net/manual/en/ref.session.php for some
                more details.

                Rick


                Comment

                • Michael Fesser

                  #9
                  Re: Passing objects from one page to another

                  ..oO(Marcel)
                  >I tried your example but i am getting the same error: Call to a member
                  >function getName() on a non-object in
                  error_reporting set to E_ALL? What browser do you use?
                  >Maybe it is a php.ini issue? I do not know what can be another reason....
                  >maybe magicquotes have to do something with this?
                  First you should fix the error in the redirect. The Location header
                  requires an absolute URL.

                  Micha

                  Comment

                  • Marcel

                    #10
                    Re: Passing objects from one page to another


                    "Marcin Dobrucki" <Marcin.Dobruck i@TAKETHISAWAY. nokia.comschree f in
                    bericht news:VKm4h.4249 1$_k2.774821@ne ws2.nokia.com.. .
                    Marcel wrote:
                    >
                    >Yes you are right, the errorcode i am getting is:
                    >>
                    >Fatal error: Call to a member function getName() on a non-object in
                    >D:\public_html \person2.php on line 7
                    >
                    One step at a time: what do you get out of "urldecode" and then what do
                    you get from "unserializ e"? Check the return values.
                    The unserialize function in person2.php did not return anything. I tried all
                    scripts on another server with the same PHP version installed and there
                    everything just worked fine. One difference i can think of is the fact that
                    the server where my script did not work is protected trough https


                    Comment

                    • Marcin Dobrucki

                      #11
                      Re: Passing objects from one page to another

                      Michael Fesser wrote:
                      First you should fix the error in the redirect. The Location header
                      requires an absolute URL.
                      True. But it seems to work with relative URLs as well.

                      Comment

                      • Jerry Stuckle

                        #12
                        Re: Passing objects from one page to another

                        Marcel wrote:
                        "Marcin Dobrucki" <Marcin.Dobruck i@TAKETHISAWAY. nokia.comschree f in
                        bericht news:VKm4h.4249 1$_k2.774821@ne ws2.nokia.com.. .
                        >
                        >>Marcel wrote:
                        >>
                        >>
                        >>>Yes you are right, the errorcode i am getting is:
                        >>>
                        >>>Fatal error: Call to a member function getName() on a non-object in
                        >>>D:\public_ht ml\person2.php on line 7
                        >>
                        > One step at a time: what do you get out of "urldecode" and then what do
                        >>you get from "unserializ e"? Check the return values.
                        >
                        >
                        The unserialize function in person2.php did not return anything. I tried all
                        scripts on another server with the same PHP version installed and there
                        everything just worked fine. One difference i can think of is the fact that
                        the server where my script did not work is protected trough https
                        >
                        >
                        Marcel,

                        http vs. https shouldn't make a difference, unless you're trying to mix
                        the two. That is, storing it in the session when using http and reading
                        it back with https (or vice versa).

                        Also, is the domain *exactly* the same? i.e. you don't have one as
                        www.example.com and the other as example.com?

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

                        Comment

                        • Michael Fesser

                          #13
                          Re: Passing objects from one page to another

                          ..oO(Marcin Dobrucki)
                          >Michael Fesser wrote:
                          >First you should fix the error in the redirect. The Location header
                          >requires an absolute URL.
                          >
                          True. But it seems to work with relative URLs as well.
                          A bug is a bug and should be fixed, especially if you're searching for
                          the reason of another bug, which could be caused by the first.

                          Micha

                          Comment

                          • Jerry Stuckle

                            #14
                            Re: Passing objects from one page to another

                            Michael Fesser wrote:
                            .oO(Marcin Dobrucki)
                            >
                            >
                            >>Michael Fesser wrote:
                            >>
                            >>>First you should fix the error in the redirect. The Location header
                            >>>requires an absolute URL.
                            >>
                            > True. But it seems to work with relative URLs as well.
                            >
                            >
                            A bug is a bug and should be fixed, especially if you're searching for
                            the reason of another bug, which could be caused by the first.
                            >
                            Micha
                            Yep, bugs need to be fixed. But how many times do people have to tell
                            you that PHP is working exactly according to the RFC's?

                            Additionally, it's not just PHP. It's every browser on the market.
                            Session ID's are kept in cookies. And even the browsers agree that
                            example.com is not the same as www.example.com - and will not send a
                            cookie generated by one host to the other.

                            If you don't like it, fine. Put in your own RFC and get the entire
                            internet to change. But don't keep complaining about it being a bug
                            just because it doesn't work like *you* would like it to. It's been
                            like that for almost 40 years, since it was arpanet. I know for sure it
                            was like that in the early 70's when I was on arpanet. And that's the
                            way it's designed to work.

                            As much as I hate to tell you, the internet does NOT revolve around Micha.

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

                            Comment

                            • Michael Fesser

                              #15
                              Re: Passing objects from one page to another

                              ..oO(Jerry Stuckle)
                              >If you don't like it, fine. Put in your own RFC and get the entire
                              >internet to change. But don't keep complaining about it being a bug
                              >just because it doesn't work like *you* would like it to.
                              RFC 2616, section 14.30 "Location":

                              | The field value consists of a single absolute URI.

                              Micha

                              Comment

                              Working...