By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,417 Members | 1,845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,417 IT Pros & Developers. It's quick & easy.

Illegal Offset & Other Assorted Errors

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

Expand|Select|Wrap|Line Numbers
  1. Warning: Illegal offset type in /home/poison1/public_html/wp-content/themes/thesis_17/custom/custom_functions.php  on line 25
  2.  
  3. 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:

Expand|Select|Wrap|Line Numbers
  1. function setcookielive($name="Believe_Adwords", $value=1, $expire=0, $path="/", $domain=".nickholliday.com", $secure=false, $httponly=false) {
  2.     //set a cookie as usual, but ALSO add it to $_COOKIE so the current page load has access
  3.     $_COOKIE[$name] = $value;
  4.     $expire=time()+3888000;
  5.     $value=$_GET['gclid'];
  6.     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!
Jul 20 '10 #1

✓ answered by Dormilich

because add_cookie() does not accept parameters.

Share this Question
Share on Google+
19 Replies


Atli
Expert 5K+
P: 5,058
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.
Jul 20 '10 #2

Dormilich
Expert Mod 5K+
P: 8,639
the second error tells you what’s up, in both lines, you feed an object instead of the expected String.
Jul 20 '10 #3

P: 9
Here's what I got when I did var_dump($name):

Expand|Select|Wrap|Line Numbers
  1. object(WP)#140 (9) {
  2.  ["public_query_vars"]=>  array(47) {
  3.   [0]=>  string(1) "m"
  4.   [1]=>  string(1) "p"
  5.   [2]=>  string(5) "posts"
  6.   [3]=>  string(1) "w"
  7.   [4]=>  string(3) "cat"
  8.   [5]=>  string(12) "withcomments"
  9.   [6]=>  string(15) "withoutcomments"
  10.   [7]=>  string(1) "s"
  11.   [8]=>  string(6) "search"
  12.   [9]=>  string(5) "exact"
  13.   [10]=>  string(8) "sentence"
  14.   [11]=>  string(5) "debug"
  15.   [12]=>  string(8) "calendar"
  16.   [13]=>  string(4) "page"
  17.   [14]=>  string(5) "paged"
  18.   [15]=>  string(4) "more"
  19.   [16]=>  string(2) "tb"
  20.   [17]=>  string(2) "pb"
  21.   [18]=>  string(6) "author"
  22.   [19]=>  string(5) "order"
  23.   [20]=>  string(7) "orderby"
  24.   [21]=>  string(4) "year"
  25.   [22]=>  string(8) "monthnum"
  26.   [23]=>  string(3) "day"
  27.   [24]=>  string(4) "hour"
  28.   [25]=>  string(6) "minute"
  29.   [26]=>  string(6) "second"
  30.   [27]=>  string(4) "name"
  31.   [28]=>  string(13) "category_name"
  32.   [29]=>  string(3) "tag"
  33.   [30]=>  string(4) "feed"
  34.   [31]=>  string(11) "author_name"
  35.   [32]=>  string(6) "static"
  36.   [33]=>  string(8) "pagename"
  37.   [34]=>  string(7) "page_id"
  38.   [35]=>  string(5) "error"
  39.   [36]=>  string(14) "comments_popup"
  40.   [37]=>  string(10) "attachment"
  41.   [38]=>  string(13) "attachment_id"
  42.   [39]=>  string(7) "subpost"
  43.   [40]=>  string(10) "subpost_id"
  44.   [41]=>  string(7) "preview"
  45.   [42]=>  string(6) "robots"
  46.   [43]=>  string(8) "taxonomy"
  47.   [44]=>  string(4) "term"
  48.   [45]=>  string(5) "cpage"
  49.   [46]=>  string(9) "post_type"
  50.  }
  51.  ["private_query_vars"]=>  array(19) {
  52.   [0]=>  string(6) "offset"
  53.   [1]=>  string(14) "posts_per_page"
  54.   [2]=>  string(22) "posts_per_archive_page"
  55.   [3]=>  string(9) "showposts"
  56.   [4]=>  string(8) "nopaging"
  57.   [5]=>  string(9) "post_type"
  58.   [6]=>  string(11) "post_status"
  59.   [7]=>  string(12) "category__in"
  60.   [8]=>  string(16) "category__not_in"
  61.   [9]=>  string(13) "category__and"
  62.   [10]=>  string(7) "tag__in"
  63.   [11]=>  string(11) "tag__not_in"
  64.   [12]=>  string(8) "tag__and"
  65.   [13]=>  string(12) "tag_slug__in"
  66.   [14]=>  string(13) "tag_slug__and"
  67.   [15]=>  string(6) "tag_id"
  68.   [16]=>  string(14) "post_mime_type"
  69.   [17]=>  string(4) "perm"
  70.   [18]=>  string(17) "comments_per_page"
  71.  }
  72.  ["extra_query_vars"]=>  array(0) { }
  73.  ["query_vars"]=>  array(0) { }
  74.  ["query_string"]=>  NULL
  75.  ["request"]=>  string(0) ""
  76.  ["matched_rule"]=>  NULL
  77.  ["matched_query"]=>  NULL 
  78.  ["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_Adwords"

I'm pretty new to script writing so you'll have to pardon my lack of context!
Jul 20 '10 #4

Dormilich
Expert Mod 5K+
P: 8,639
As far as I can tell, I'm setting $name to be the string "Believe_Adwords"
see the first word in the output? it says "object" (and it further says, it’s of the WP class)
Jul 20 '10 #5

P: 9
@Dormilich
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.
Jul 20 '10 #6

Dormilich
Expert Mod 5K+
P: 8,639
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?
Jul 20 '10 #7

P: 9
Yes, I'm using Wordpress. I call the function from my "custom_function.php" file. I add it as an action to the "send_headers" 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_functions.php. It includes the old function (commented out) that worked with no problem:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // Using hooks is absolutely the smartest, most bulletproof way to implement things like plugins,
  4. // custom design elements, and ads. You can add your hook calls below, and they should take the 
  5. // following form:
  6. // add_action('thesis_hook_name', 'function_name');
  7. // The function you name above will run at the location of the specified hook. The example
  8. // hook below demonstrates how you can insert Thesis' default recent posts widget above
  9. // the content in Sidebar 1:
  10. // add_action('thesis_hook_before_sidebar_1', 'thesis_widget_recent_posts');
  11.  
  12. // Delete this line, including the dashes to the left, and add your hooks in its place.
  13.  
  14.  
  15. /* Function from http://believedesign.net/php/how-to-track-customer-phone-calls-generated-through-adwords 
  16. function add_cookie() {
  17. if ($_GET['gclid']) {
  18. $expire=time()+3888000;
  19. setcookie("Believe_Adwords",$_GET['gclid'],$expire,"/",".nickholliday.com");
  20. }
  21. }
  22.  
  23. add_action('send_headers','add_cookie'); */
  24.  
  25.  
  26.  
  27. /* Function to make cookie go live on the first load of page */
  28. function setcookielive($name="Believe_Adwords", $value=1, $expire=0, $path="/", $domain=".nickholliday.com", $secure=false, $httponly=false) {
  29.     //set a cookie as usual, but ALSO add it to $_COOKIE so the current page load has access
  30.     $_COOKIE[$name] = $value;
  31.     $expire=time()+3888000;
  32.     $value=$_GET['gclid'];
  33.     return setcookie($name,$value,$expire,$path,$domain,$secure,$httponly);
  34.  
  35. add_action('send_headers','setcookielive'); 
  36.  
  37.  
  38. /* Function from http://believedesign.net/php/how-to-track-customer-phone-calls-generated-through-adwords */
  39. function get_phone_number($atts) {
  40.     echo (isset($_COOKIE['Believe_Adwords'])) ? '555-0968-5566' : '555-8979-4567';
  41. }
  42.  
  43. add_shortcode('get-phone-number','get_phone_number');
  44. ?>
Jul 20 '10 #8

Dormilich
Expert Mod 5K+
P: 8,639
what does the add_action() function do?
Jul 21 '10 #9

P: 9
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

Expand|Select|Wrap|Line Numbers
  1. 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.
Jul 21 '10 #10

Dormilich
Expert Mod 5K+
P: 8,639
the add_action() function is responsible, why you have an object instead of a string.
Jul 22 '10 #11

P: 9
In that case, why would the add_cookie() function work fine, but the setcookielive() function doesn't?
Jul 22 '10 #12

Dormilich
Expert Mod 5K+
P: 8,639
because add_cookie() does not accept parameters.
Jul 23 '10 #13

P: 9
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?
Jul 23 '10 #14

Markus
Expert 5K+
P: 6,050
@Nick Holliday
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.

Expand|Select|Wrap|Line Numbers
  1. // Just a function
  2. function say_hello() 
  3. {
  4.   echo 'Hello!', PHP_EOL;
  5. }
  6. // Again, just a function
  7. function say_hello_ex($greeting) 
  8. {
  9.   echo $greeting, PHP_EOL;
  10. }
  11.  
  12. // A class
  13. class Person 
  14. {
  15.   private $name;
  16.   private $age;
  17.  
  18.   public function __construct($name, $age)
  19.   {
  20.      $this->name = $name;
  21.      $this->age  = $age;
  22.   }
  23.   public function getName() { return $this->name; }
  24.   public function setName($name) { $this->name = $name; }
  25.   public function getAge() { return $this->age; }
  26.   public function setAge($age) { $this->age = $age; }
  27. }
  28.  
  29. // An object: an instance of a class.
  30. $person = new Person('Mark', 19);
  31.  
  32. printf("Person's name: %s, Age: %d\n",
  33.     $person->getName(),
  34.     $person->getAge()
  35. );
  36.  
  37. say_hello();
  38. say_hello_ex("Greetings, " . $person->getName());
  39.  
Mark.
Jul 24 '10 #15

P: 9
Thanks for that explanation.

Does that mean that in the following code that $name had been defined as an object (I understood Dormilich to say it was an object) elsewhere on my wordpress site?

Expand|Select|Wrap|Line Numbers
  1. # function setcookielive($name="Believe_Adwords", $value=1, $expire=0, $path="/", $domain=".nickholliday.com", $secure=false, $httponly=false) {
  2. #     //set a cookie as usual, but ALSO add it to $_COOKIE so the current page load has access
  3. #     $_COOKIE[$name] = $value;
  4. #     $expire=time()+3888000;
  5. #     $value=$_GET['gclid'];
  6. #     return setcookie($name,$value,$expire,$path,$domain,$secure,$httponly);
  7. # } 
Jul 24 '10 #16

Dormilich
Expert Mod 5K+
P: 8,639
no. that’s the doing of add_action(). that function is passing a WP object as first argument to setcookielive().
Jul 24 '10 #17

P: 9
@Dormilich
so send_headers() is an object?
Jul 24 '10 #18

Dormilich
Expert Mod 5K+
P: 8,639
the 'send_headers' in
Expand|Select|Wrap|Line Numbers
  1. add_action('send_headers','add_cookie');
is a string. but unless I know how add_action actually works, I can’t tell you, what’s passed to add_cookie() and why. I only can assume that it is some kind of event which is part of WordPress.

to give an analogy, in JavaScript, event handlers pass an event object as parameter to the function. (note the similarity in the calls)
Expand|Select|Wrap|Line Numbers
  1. function doSomething(a)
  2. {
  3.     alert(a);
  4. }
  5. window.addEventListener("load", doSomething, false);
  6.  
  7. // should show something like
  8. // [object Event]
Jul 24 '10 #19

100+
P: 115
thanks for the solution folks, I was facing a problem akin to this one.
Jul 25 '10 #20

Post your reply

Sign in to post your reply or Sign up for a free account.