Got a couple of errors on a little script I'm trying to write. The error messages are: - 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: - 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!
because add_cookie() does not accept parameters.
19 2386 Atli 5,058
Expert 4TB
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.
the second error tells you what’s up, in both lines, you feed an object instead of the expected String.
Here's what I got when I did var_dump($name): - 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_Adwords"
I'm pretty new to script writing so you'll have to pardon my lack of context!
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)
@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.
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?
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: - <?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');
-
?>
what does the add_action() function do?
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 - 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.
the add_action() function is responsible, why you have an object instead of a string.
In that case, why would the add_cookie() function work fine, but the setcookielive() function doesn't?
because add_cookie() does not accept parameters.
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?
@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. -
// 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.
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? - # 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);
-
# }
no. that’s the doing of add_action(). that function is passing a WP object as first argument to setcookielive().
the 'send_headers' in - 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) - function doSomething(a)
-
{
-
alert(a);
-
}
-
window.addEventListener("load", doSomething, false);
-
-
// should show something like
-
// [object Event]
thanks for the solution folks, I was facing a problem akin to this one.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Miguel Orrego |
last post by:
Hi,
I am trying to migrate an asp app from an NT4 Server to a Win 2k Server.
The directory structure & DSNs have been copied over and IIS configured in
the same way as the old server.
...
|
by: Chris Sharman |
last post by:
I've a very simple page (intranet, but moved to
http://services.ccagroup.co.uk/dtest.html for you - so the links are all
broken).
essentially it's just a list of links.
I've split it into two...
|
by: Steve - DND |
last post by:
We are continually receiving timeout, and "Unable to
write data to the transport connection" errors while using the
System.Net.HttpWebRequest class from an ASP.Net web page. Below are the two...
|
by: Richard Beacroft |
last post by:
Trying to write a C# Windows App to export all objects and content from
2 MSAccess 97 databases for comparison analysis.
very little documentation found.
Have managed to instantiate MSAccess,...
|
by: Sushma |
last post by:
CSiFtpConnection error LNK2001: unresolved external symbol "public:
virtual class ATL::CStringT<char,class StrTraitMFC_DLL<char,class
ATL::ChTraitsCRT<char> > > __thiscall...
|
by: Manish |
last post by:
The code is as ...
$folderlistfile = $path."xmlfile.xml";
/*
<list>
<details id="2">
<name>Books</name>
|
by: Jim Frazer |
last post by:
Hi,
I've just encountered a bunch of errors when loading a form in design mode.
These errors have not occured before and don't make sense to me. In
addition, none of my controls are displayed...
|
by: ErikL |
last post by:
Hey, I am looking for a list of illegal characters in XML..
For example:
è, À, ì are illegal characters. They could errors. I am looking for a massive list of these so we can filter them out for a...
|
by: prw8864 |
last post by:
In the following code I am trying to create a new templete class (SVector) that is a linked list with the added functionality of an insert method that automatically places items in sorted order.
I...
|
by: chazzy69 |
last post by:
Currently working on a site that has user and admin access. Now basically im trying to use -
$file1 = file_get_contents($url);
The file im trying to retrieve is only accessible via logining...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |