I have a code for wordpress that will basically say "hello" to first time visitors if they don't have a cookie that is set when they visit the first time. But I get this error: <?php if (!get_cookie('firstvisit')) {?> hello <?php } ?> // cookie not set, first visit // create cookie to avoid hitting this case again $cookie = array( 'name' => 'firstvisit', 'value' => 'Yes', 'expire' => time()+86500, 'domain' => 'mysite.com', 'path' => '/', 'prefix' => '', ); set_cookie($cookie); } ?> How do I do this in wordpress?
<?php if (!isset($_COOKIE['firstvisit'])) { echo('Hello'); // cookie not set, first visit // create cookie Eto avoid hitting this case again $cookie = array( 'name' => 'firstvisit', 'value' => 'Yes', 'expire' => time()+86500, 'domain' => 'mysite.com', 'path' => '/', 'prefix' => '', ); setcookie($cookie); } ?> PHP:
A better way to do this without setting any extra cookies is by: if(!isset($_COOKIE)) { // No cookies set. First visit. echo "Hello"; } PHP: This is useful if you already have analytics or something else enabled that sets cookies anyway, since there is no point wasting space on browsers if you don't need to.
I keep getting this error if I use the code: "setcookie() expects parameter 1 to be string, array given in" Even for just that simple Hello echo. Please assist.
<?php if (!isset($_COOKIE['firstvisit'])) { echo('Hello'); // cookie not set, first visit // create cookie Eto avoid hitting this case again setcookie('firstvisit','Yes', time()+86500, '/', 'mysite.com'); } ?> PHP:
That jumps one hurdle, but I get a "Warning: Cannot modify header information - headers already sent by" at first it said All in One Plugin, but after I disabled the plugin it stays on but said that the line with "setcookie" was what already sent the header info. Makes no sense to me. Any help? Thanks again!