BR
asked on
PHP cookie
I have a huge banner, I want to show it to my user only once. Like Yahoo does.
I think cookies the best way for this. ( I use PHP )
I need someting like
if the user has already seen it print nothing
else print the source code of the banner..
how can I set a cookie and how can retrive data from the cookie?
I think cookies the best way for this. ( I use PHP )
I need someting like
if the user has already seen it print nothing
else print the source code of the banner..
how can I set a cookie and how can retrive data from the cookie?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
The idea is that you set any data in the cookies that indicate that the visitor have seen the banner, for example:
When you print the banner for the first time, add this code:
Whenever you want to know if the visitor have seen the banner or not use the following if statement
When you print the banner for the first time, add this code:
setcookie("banner_status", 1, 60*60*24*30, "/");
Whenever you want to know if the visitor have seen the banner or not use the following if statement
if ($_COOKIE["banner_status"] == 1)
{
// The user have seen the banner
} else {
// The user have not seen the banner
}
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Dear Medo3337,
that is exactly what I want.
I wrote it like you did, but it didn't work.
but there is an error message below.
Notice: Undefined index: banner_status in /srv/web/www/real-tr/sites /files/rea l.tr-2010/ data/sessi on/banner_ cookie.php on line 24
The user have not seen the banner
I wrote it like this,
<?php
setcookie("banner_status", 1, 60*60*24*30, "/");
if ($_COOKIE["banner_status"] == 1)
{
echo "The user have seen the banner";
} else {
echo "The user have not seen the banner";
}
?>
that is exactly what I want.
I wrote it like you did, but it didn't work.
but there is an error message below.
Notice: Undefined index: banner_status in /srv/web/www/real-tr/sites
The user have not seen the banner
I wrote it like this,
<?php
setcookie("banner_status",
if ($_COOKIE["banner_status"]
{
echo "The user have seen the banner";
} else {
echo "The user have not seen the banner";
}
?>
ASKER
Dear bportlock,
thank you very much,
that was really helpful,
I want to learn why I should use path parameter '/' ?
what is it for?
thank you very much,
that was really helpful,
I want to learn why I should use path parameter '/' ?
what is it for?
Use the following code:
<?php
setcookie("banner_status", 1, time() + 60*60*24*30);
if ($_COOKIE["banner_status"] == 1)
{
echo "The user have seen the banner";
} else {
echo "The user have not seen the banner";
}
?>
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
thank you very much
ASKER
the output is :
three : cookiethree
two : cookietwo
one : cookieone
how can I assign a varible to those cookies?
setcookie("cookie[three]",
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");
// after the page reloads, print them out
if (isset($_COOKIE['cookie'])
foreach ($_COOKIE['cookie'] as $name => $value) {
$name = htmlspecialchars($name);
$value = htmlspecialchars($value);
echo "$name : $value <br />\n";
}
}