It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming PHP and MySQL Forum

Problem with creating cookie and showing/hiding div based on

Moderator: Malcolm

Problem with creating cookie and showing/hiding div based on

Postby purplemonster on Fri Apr 24, 2009 6:59 pm

Hi there,

I am trying to set a cookie on html page with php code, which if cookie is not yet set shows content of a specific Div on that page (in this case it's div called Autoload). But after it's set (which is on next page refresh or reload) I want it to do nothing.

I came up with following code, but it wont work... It always displays Div content even if the cookie has been set (or probably because I haven't set it properly).

So what I need is:
User goes to page, Div content is shown, then on next refresh/reload Div is not shown anymore.

What am I doing wrong?

Here is the code I've put in the BODY of html page:


Code: Select all
<?php
if(isset($_COOKIE['imekukija']))
{
}
else
{ echo
?>
<div id="autoload">
<script src="lightbox/prototype.js" type="text/javascript"></script>
<script src="lightbox/scriptaculous.js?load=effects" type="text/javascript"></script>
<script src="lightbox/lightbox.js" type="text/javascript"></script>
<a href="http://www.webmaster-talk.com/images/index_open.jpg" rel="lightbox" id="img"></a>
</div>
<?php
  }
?>


<?php
setcookie("imekukija", "nekiValue", time()+3600);
?>
purplemonster
 
Posts: 24
Joined: Fri Jul 04, 2008 9:01 pm

Re: Problem with creating cookie and showing/hiding div based on

Postby rse on Tue Apr 28, 2009 3:05 am

if you do a print_r($_COOKIE) does it show the cookie data?

If not it may be that you need to add your domain when setting the cookie as I know I've had to do this on some servers. For more info checkout http://uk3.php.net/manual/en/function.setcookie.php.

For example:

Code: Select all
setcookie("imekukija", "nekiValue", time()+3600, "/", "example.com");


Also, you can shorten your testing code to the following:

Code: Select all
<?php
if(!isset($_COOKIE['imekukija'])) {
?>
<div id="autoload">
<script src="lightbox/prototype.js" type="text/javascript"></script>
<script src="lightbox/scriptaculous.js?load=effects" type="text/javascript"></script>
<script src="lightbox/lightbox.js" type="text/javascript"></script>
<a href="http://www.webmaster-talk.com/images/index_open.jpg" rel="lightbox" id="img"></a>
</div>
<?php
setcookie("imekukija", "nekiValue", time()+3600, "/", "example.com");
  }
?>


I hope this helps :).

- Adam
Adam Williams
PHP Web Developer

Personal: http://30things.net
Blog: http://www.root-servers.co.uk
User avatar
rse
100+ Club
 
Posts: 141
Joined: Sun Oct 10, 2004 8:15 pm
Location: Leeds, UK


Who is online

Users browsing this forum: No registered users and 1 guest