Reply
 
Thread Tools
Old 12-01-2004, 01:46 AM   #1   [permalink]
Dastafarin
Forum Master
 
Dastafarin's Avatar
 
Join Date: 7 Feb 2001
Location: my little sanctuary
Posts: 3,751
Send a message via ICQ to Dastafarin Send a message via AIM to Dastafarin Send a message via Yahoo to Dastafarin
PHP, Objects, and calling Serialized Cookies

Ok, I have been working through this new PHP book, and have finished the OOP section, problem is, one of the scripts didn't work correctly. I have checked the book's site, looked at the forum, downloaded the script, but nothing fixes the problem.

Problem:

The script is very simple, it creates a page using an Object, Constructors and a template. When you first load the page, you are given the basic page, with a form in the middle allowing you to select the page title, background color, text color, and table width. It then sends that information via POST to itself, sets all the Parameters, then stores all the information into a serialized cookie. The cookie stores fine, has all the information. And, when you go to the page right after hitting submit, it shows it how it should look. Problem is, when you try to visit again, and load the site off the cookie, it doesn't work. Instead, it gives me this:

Quote:
Fatal error: Call to a member function on a non-object in /home/nferno/public_html/php/adv/instance5.php on line 9
I have tried doing what the book says and using urlencode(), urldecode() on the serialize and unserialize functions respectively, but then it doesn't even store the cookie.

This is the portion of the script that it mainly deals with:

PHP Code:
<?php

require_once "HtmlTemplate2.class";
require_once 
"HtmlNavTemplate.class";

if (
$HTTP_COOKIE_VARS['html_object']) {

    
$page unserialize ($HTTP_COOKIE_VARS['html_object']);
    
$page->CreatePage();

} elseif (
$HTTP_POST_VARS['submit']) {

    
$page = new HtmlNavTemplate ("template4.inc");
    
$page->SetParameter ("PAGE_TITLE"$page_title);
    
$page->SetParameter ("CSS_LINK""style.css");
    
$page->SetParameter ("BG_COLOR"$bg_color);
    
$page->SetParameter ("TEXT_COLOR"$text_color);
    
$page->SetParameter ("TABLE_WIDTH"$table_width);
    
$page->SetParameter ("NAVIGATION_LINKS"$page->DetermineLinks());
    
$page->SetParameter ("PAGE_CONTENT""Here is your custom page.");
    
setcookie ("html_object"serialize ($page), time() + 100000""""0);
    
$page->CreatePage();

} else {
also, here are HtmlNavTemplate.class and HtmlTemplate2.class respectively:

PHP Code:
---------------------------------------------------
HtmlNavTemplate.class
---------------------------------------------------
<?php

class HtmlNavTemplate extends HtmlTemplate2 {

    function 
DetermineLinks () {

        
$open opendir (".");
        while (
$file readdir ($open)) {

            if ((
is_dir ($file)) and (substr ($file01) != ".")) {

                
$name str_replace ("_"" "$file);
                
$nav_links .= "<A HREF=\"$file/\">$name</A><BR />\n";

            }

        }

        
closedir ($open);
        return 
$nav_links;

    }

}

?>
---------------------------------------------------
HtmlTemplate2.class
---------------------------------------------------
<?php

class HtmlTemplate2 {

    var 
$template;
    var 
$html;
    var 
$parameters = array();

    function 
HtmlTemplate2 ($template) {

        
$this->template $template;
        
$this->html implode ("", (file($this->template)));

    }

    function 
SetParameter ($variable$value) {

        
$this->parameters[$variable] = $value;

    }

    function 
CreatePage () {

        foreach (
$this->parameters as $key => $value) {
            
$template_name '{' $key '}';
            
$this->html str_replace ($template_name$value$this->html);

        }

        echo 
$this->html;

    }

}

?>
Finally, I tried using a

PHP Code:
print_r ($_COOKIE); 
to see what the unserialized code looks like, and it showed the "htmlnavtemplate" all in lowercase, and I think that might be the problem, but I don't know how to fix that. I tried ereg_replace, but that didn't work, as I expected.
__________________
oh wait...what?
Dastafarin is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


New To Site? Need Help?

All times are GMT -4. The time now is 01:47 PM.


Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2018, vBulletin Solutions, Inc.