Illegal Offset & Other Assorted Errors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ForumGuy22
    New Member
    • Jul 2010
    • 9

    Illegal Offset & Other Assorted Errors

    Got a couple of errors on a little script I'm trying to write. The error messages are:

    Code:
    Warning: Illegal offset type in /home/poison1/public_html/wp-content/themes/thesis_17/custom/custom_functions.php  on line 25
    
    Warning: setcookie() expects parameter 1 to be string, object given in /home/poison1/public_html/wp-content/themes/thesis_17/custom/custom_functions.php on line 28
    The function that's causing the trouble is:

    Code:
    function setcookielive($name="Believe_Adwords", $value=1, $expire=0, $path="/", $domain=".nickholliday.com", $secure=false, $httponly=false) {
        //set a cookie as usual, but ALSO add it to $_COOKIE so the current page load has access
        $_COOKIE[$name] = $value;
        $expire=time()+3888000;
        $value=$_GET['gclid'];
        return setcookie($name,$value,$expire,$path,$domain,$secure,$httponly);
    }
    Line #25 is the line that starts with $_COOKIE and line #28 is the return line.

    Any help would be greatly appreciated!
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    It would appear that you are passing the function an invalid value for the first parameter. If you add a var_dump($name) in there, what does it print?

    It's always a good idea to verify input values in function like that. Especially if you plan to reuse them. It gives you better debug data, and spares the user having to see the errors.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      the second error tells you what’s up, in both lines, you feed an object instead of the expected String.

      Comment

      • ForumGuy22
        New Member
        • Jul 2010
        • 9

        #4
        Here's what I got when I did var_dump($name) :

        Code:
        object(WP)#140 (9) {
         ["public_query_vars"]=>  array(47) {
          [0]=>  string(1) "m"
          [1]=>  string(1) "p"
          [2]=>  string(5) "posts"
          [3]=>  string(1) "w"
          [4]=>  string(3) "cat"
          [5]=>  string(12) "withcomments"
          [6]=>  string(15) "withoutcomments"
          [7]=>  string(1) "s"
          [8]=>  string(6) "search"
          [9]=>  string(5) "exact"
          [10]=>  string(8) "sentence"
          [11]=>  string(5) "debug"
          [12]=>  string(8) "calendar"
          [13]=>  string(4) "page"
          [14]=>  string(5) "paged"
          [15]=>  string(4) "more"
          [16]=>  string(2) "tb"
          [17]=>  string(2) "pb"
          [18]=>  string(6) "author"
          [19]=>  string(5) "order"
          [20]=>  string(7) "orderby"
          [21]=>  string(4) "year"
          [22]=>  string(8) "monthnum"
          [23]=>  string(3) "day"
          [24]=>  string(4) "hour"
          [25]=>  string(6) "minute"
          [26]=>  string(6) "second"
          [27]=>  string(4) "name"
          [28]=>  string(13) "category_name"
          [29]=>  string(3) "tag"
          [30]=>  string(4) "feed"
          [31]=>  string(11) "author_name"
          [32]=>  string(6) "static"
          [33]=>  string(8) "pagename"
          [34]=>  string(7) "page_id"
          [35]=>  string(5) "error"
          [36]=>  string(14) "comments_popup"
          [37]=>  string(10) "attachment"
          [38]=>  string(13) "attachment_id"
          [39]=>  string(7) "subpost"
          [40]=>  string(10) "subpost_id"
          [41]=>  string(7) "preview"
          [42]=>  string(6) "robots"
          [43]=>  string(8) "taxonomy"
          [44]=>  string(4) "term"
          [45]=>  string(5) "cpage"
          [46]=>  string(9) "post_type"
         }
         ["private_query_vars"]=>  array(19) {
          [0]=>  string(6) "offset"
          [1]=>  string(14) "posts_per_page"
          [2]=>  string(22) "posts_per_archive_page"
          [3]=>  string(9) "showposts"
          [4]=>  string(8) "nopaging"
          [5]=>  string(9) "post_type"
          [6]=>  string(11) "post_status"
          [7]=>  string(12) "category__in"
          [8]=>  string(16) "category__not_in"
          [9]=>  string(13) "category__and"
          [10]=>  string(7) "tag__in"
          [11]=>  string(11) "tag__not_in"
          [12]=>  string(8) "tag__and"
          [13]=>  string(12) "tag_slug__in"
          [14]=>  string(13) "tag_slug__and"
          [15]=>  string(6) "tag_id"
          [16]=>  string(14) "post_mime_type"
          [17]=>  string(4) "perm"
          [18]=>  string(17) "comments_per_page"
         }
         ["extra_query_vars"]=>  array(0) { }
         ["query_vars"]=>  array(0) { }
         ["query_string"]=>  NULL
         ["request"]=>  string(0) ""
         ["matched_rule"]=>  NULL
         ["matched_query"]=>  NULL 
         ["did_permalink"]=>  bool(false) 
        }
        "the second error tells you what’s up, in both lines, you feed an object instead of the expected String."

        Unfortunately I don't understand what that means. As far as I can tell, I'm setting $name to be the string "Believe_Adword s"

        I'm pretty new to script writing so you'll have to pardon my lack of context!
        Last edited by Dormilich; Jul 20 '10, 07:08 PM.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          As far as I can tell, I'm setting $name to be the string "Believe_Adword s"
          see the first word in the output? it says "object" (and it further says, it’s of the WP class)

          Comment

          • ForumGuy22
            New Member
            • Jul 2010
            • 9

            #6
            Originally posted by Dormilich
            see the first word in the output? it says "object" (and it further says, it’s of the WP class)
            Right, I understand that. What I'm saying is I don't understand why it would be an object when I did what my tutorials said would set a variable.

            I don't even know what an object is. When I google it, all I get is stuff about object-oriented programming.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              I don't even know what an object is. When I google it, all I get is stuff about object-oriented programming.
              well, that’s why it is called object-oriented programming …

              how do you call the setcookielive() function?

              do you use Wordpress?
              Last edited by Dormilich; Jul 20 '10, 07:09 PM.

              Comment

              • ForumGuy22
                New Member
                • Jul 2010
                • 9

                #8
                Yes, I'm using Wordpress. I call the function from my "custom_functio n.php" file. I add it as an action to the "send_heade rs" part of a Wordpress page.

                I previously just called the setcookie() function in this same context and everything worked great -- except that the page had to be refreshed before I could access the new cookie value.

                Here's my whole custom_function s.php. It includes the old function (commented out) that worked with no problem:

                Code:
                <?php
                
                // Using hooks is absolutely the smartest, most bulletproof way to implement things like plugins,
                // custom design elements, and ads. You can add your hook calls below, and they should take the 
                // following form:
                // add_action('thesis_hook_name', 'function_name');
                // The function you name above will run at the location of the specified hook. The example
                // hook below demonstrates how you can insert Thesis' default recent posts widget above
                // the content in Sidebar 1:
                // add_action('thesis_hook_before_sidebar_1', 'thesis_widget_recent_posts');
                
                // Delete this line, including the dashes to the left, and add your hooks in its place.
                
                
                /* Function from http://believedesign.net/php/how-to-track-customer-phone-calls-generated-through-adwords 
                function add_cookie() {
                if ($_GET['gclid']) {
                $expire=time()+3888000;
                setcookie("Believe_Adwords",$_GET['gclid'],$expire,"/",".nickholliday.com");
                }
                }
                
                add_action('send_headers','add_cookie'); */
                
                
                
                /* Function to make cookie go live on the first load of page */
                function setcookielive($name="Believe_Adwords", $value=1, $expire=0, $path="/", $domain=".nickholliday.com", $secure=false, $httponly=false) {
                    //set a cookie as usual, but ALSO add it to $_COOKIE so the current page load has access
                    $_COOKIE[$name] = $value;
                    $expire=time()+3888000;
                    $value=$_GET['gclid'];
                    return setcookie($name,$value,$expire,$path,$domain,$secure,$httponly);
                } 
                
                add_action('send_headers','setcookielive'); 
                
                
                /* Function from http://believedesign.net/php/how-to-track-customer-phone-calls-generated-through-adwords */
                function get_phone_number($atts) {
                    echo (isset($_COOKIE['Believe_Adwords'])) ? '555-0968-5566' : '555-8979-4567';
                }
                
                add_shortcode('get-phone-number','get_phone_number');
                ?>

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  what does the add_action() function do?

                  Comment

                  • ForumGuy22
                    New Member
                    • Jul 2010
                    • 9

                    #10
                    I believe add_action() is a wordpress thing that tells wordpress where in the page to insert your function.

                    So in my theme, I could have done

                    Code:
                    add_action('add_cookie','thesis_hook_after_header');
                    and that would have put that function after my page header. Of course it wouldn't have worked because I have to set the cookies in the http headers.

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      the add_action() function is responsible, why you have an object instead of a string.

                      Comment

                      • ForumGuy22
                        New Member
                        • Jul 2010
                        • 9

                        #12
                        In that case, why would the add_cookie() function work fine, but the setcookielive() function doesn't?

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          because add_cookie() does not accept parameters.

                          Comment

                          • ForumGuy22
                            New Member
                            • Jul 2010
                            • 9

                            #14
                            Why didn't you say so! :)

                            I was able to solve my problem. Thank you for your help!

                            So, a function with parameters is an object, but a function without parameters is not?

                            Comment

                            • Markus
                              Recognized Expert Expert
                              • Jun 2007
                              • 6092

                              #15
                              Originally posted by Nick Holliday
                              So, a function with parameters is an object, but a function without parameters is not?
                              No. A function with parameters is a function with parameters. A function with no parameters is a function with no parameters. An object is an instance of a class.

                              Code:
                              // Just a function
                              function say_hello() 
                              {
                                echo 'Hello!', PHP_EOL;
                              }
                              // Again, just a function
                              function say_hello_ex($greeting) 
                              {
                                echo $greeting, PHP_EOL;
                              }
                              
                              // A class
                              class Person 
                              {
                                private $name;
                                private $age;
                              
                                public function __construct($name, $age)
                                {
                                   $this->name = $name;
                                   $this->age  = $age;
                                }
                                public function getName() { return $this->name; }
                                public function setName($name) { $this->name = $name; }
                                public function getAge() { return $this->age; }
                                public function setAge($age) { $this->age = $age; }
                              }
                              
                              // An object: an instance of a class.
                              $person = new Person('Mark', 19);
                              
                              printf("Person's name: %s, Age: %d\n",
                                  $person->getName(),
                                  $person->getAge()
                              );
                              
                              say_hello();
                              say_hello_ex("Greetings, " . $person->getName());
                              Mark.

                              Comment

                              Working...