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?
LVL 1
BRMarketingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Beverley PortlockCommented:
Set cookies withg setcookie http://www.php.net/setcookie like so

setcookie( "cookieName", "value", time()+86400, "/" );

would set a cookie for 1 day (86400 seconds)

To detect the cookie check the $_COOKIE array

if ( isset( $_COOKIE['cookieName'] ) ) {
     // cookie found, do something
     echo "<img name='banner.jpg' />
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BRMarketingAuthor Commented:
When I use below code,
the output is :

three : cookiethree
two : cookietwo
one : cookieone

how can I assign a varible to those cookies?


setcookie("cookie[three]", "cookiethree");
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";
    }
}

0
Mohamed AbowardaSoftware EngineerCommented:
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:
 
setcookie("banner_status", 1, 60*60*24*30, "/");

Open in new window


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
}

Open in new window

0
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Beverley PortlockCommented:
"how can I assign a varible to those cookies?"

Just change the second parameter

setcookie("cookie[three]", $aPhpVariable );

but you must assign an expiry time in the future or else your cookies will expire as soon as you have created them. To set a future time use time() and add some time to it. 86400 seconds are one day so time() + 86400 is 24 hours starting now.

setcookie("cookie[three]", $aPhpVariable, time()+86400 );

Finally, your life will get a lot easier if you set the path parameter to '/'

setcookie("cookie[three]", $aPhpVariable, time()+86400, '/' );



0
BRMarketingAuthor Commented:
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/real.tr-2010/data/session/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";
}

?>
0
BRMarketingAuthor Commented:
Dear bportlock,
thank you very much,
that was really helpful,
I want to learn why I should use path parameter '/' ?
what is it for?
0
Mohamed AbowardaSoftware EngineerCommented:
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";
}

?>

Open in new window

0
Mohamed AbowardaSoftware EngineerCommented:
The function time() was missing in the line:
 
setcookie("banner_status", 1, 60*60*24*30);

Open in new window


It caused the cookie to expire and become useless

By adding the function time() to the line as the following, it will work!
 
0
Beverley PortlockCommented:
"I want to learn why I should use path parameter '/' ?"

Cookies are set in the folder in which they occur so if your domain is http://eaxample.com and you set a cookie then that cookie exists in the base folder of the website. Let us say you now access http://example.com/images and set a new value in the cookie, you will now have TWO cookies, one in / and one in /images.

Using the path parameter forces the cookie to be written into the specified location so you will get consistent behaviour. It is an optional parameter but if you lack a full understanding of all the little oddball things that cookies can do then leaving the path parameter out causes more confusion than you ever want to experience.

Just always put that 4th parameter in and always make it '/' and your life will get a lot easier.
0
BRMarketingAuthor Commented:
thank you very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.