SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    May 2003
    Location
    nyc
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    forwarding objects to another page

    hi all,

    i am trying to implement a basic mvc flow with PHP and i need to forward the user to an error page along with the array containing error messages. i was trying to do it the way i would do it in Java by passing an object to another page on a http request during forward().

    I am setting the errors array into the $_SESSION (should probably put it into $_REQUEST) on the controller page (login.php) and forward the user to the view (invalid_login.php). the view page, however, does not seem to have access to the session values set prior to calling that page. all it prints is "Errors during login". here is the code:

    login.php:

    PHP Code:
    <?php
      
    require_once('../m/User.php');

      
    $username $_POST["username"];
      
    $password $_POST["password"];
      
      
    $user = new User;
      
    $user->setUsername($username);
      
    $user->setPassword($password);
      
      
    $errors $user->validateLogin();
      
      
    session_start();
      
      if (isset(
    $errors) && (sizeof($errors) > 0)) {
        
    $_SESSION['errors'] = $errors;
        
    header"Location: ../v/invalid_login.php\r\n" );
      } else {
        
    header"Location: ../v/valid_login.php\r\n" );
      }
    ?>
    invalid_login.php:

    PHP Code:
    <?php
    echo "Errors during login: ";
      
    if (isset(
    $_SESSION['errors']))
    {
      
    print_r ($errors);
    }
    ?>
    note, i am using PHP 4.3.1 and the register_globals is off

    any help will be greatly appreciated. thanks a lot,
    james

  2. #2
    SitePoint Addict
    Join Date
    May 2003
    Location
    nyc
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, in case anyone is curious: i got this resolved. the solution was to use include('../v/invalid_login.php');

    best,
    james

  3. #3
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Never thought about the idea of using sessions for error messages. I'll give it a try next time. Thanks.
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like you found the easier solution of using an include, but if you ever do want to know how to pass an object from one page to another as a GET or POST variable or even store it an a cookie, use serialize() and then urlencode()

    http://www.php.net/serialize

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was just about to suggest this very thing

    It would have been my first choice of use anyway; can't see why you'd need to use SESSIONs though since you don't actually need to persist the data ?

    Only put it to another page....


  6. #6
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by freakysid
    Looks like you found the easier solution of using an include, but if you ever do want to know how to pass an object from one page to another as a GET or POST variable or even store it an a cookie, use serialize() and then urlencode()
    I grabbed some examples from php.net for this serialize function but couldn't get it working. Do you have some functional example I could use perhaps? Thanks.
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe - you know I have never used serialize() because I don't do much OO programming in php. Hopefully someone will help out.

  8. #8
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok... np.
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine


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
  •