Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2002
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with serialize

    I have serialized an object and "added" it in a cookie. when i try to unserialize the object to a variable and call the gettype function i get as output "boolean". This is the code for unserialize:

    Code:

    $connection = $HTTP_COOKIE_VARS['login'];
    $oracle = unserialize($connection);
    echo gettype($connection)."<BR>".gettype($oracle);



    the output is:
    string
    boolean

    The code for serializing the object is the following:

    Code:

    $connection = serialize($oracle);
    if (setcookie("login", $connection)){
    header("Location: search.php");
    }



    What is wrong with unserialize? I saw the manual of php and it didn't mention that returns a boolean!!!!!!! And why should return a boolean in the first placE?!?!?!?!?! Please help me with that

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,058
    Thanks
    10
    Thanked 96 Times in 94 Posts
    theres nothing wrong with serialize, if you look again at the manual you will see

    "Never use gettype() to test for a certain type"

    instead use is_object or is_array etc , that said until we know exactly what kind of data you are serializing its hard to know...

    if you mean a true class object then that class needs to be accessible wherever you try and use it i.e. the below example is useless unless the include('s_cart.class.php'); line is there

    PHP Code:
    <?
      
    include 's_cart.class.php'
      if(
    $_SESSION["cart"]){
        
    $scart=unserialize($_SESSION["cart"]);
      }else{
        
    $scart = new s_cart();
      }
    ?>
    so above if the session exists we reproduce our saved object , else we start a new one, again unless we include the s_cart class it wont work.

    (the session data is saved by a method of c_cart like so
    function save_as_sess(){
    $_SESSION["cart"]=serialize($this);
    }
    )
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •