Setcookie question

Hey all,

I am a compete n00b to programming not just PHP so please be gentle. ::slight_smile:

I have a Wordpress website. It has a home page and a members page. I want to set a cookie when someone first goes to the members page so whenever they return to the site they will always go to the members page.

This is the code that I have come up with. I am pretty sure at least the syntax is incorrect at the least.

[php]<?php
if ( is_page( 8 ) || setcookie(“reg_user”, time()+606024*365);

if (!isset ($_COOKIE[‘reg_user’]))
header (“http://homepage.com: nocookie.php”);
else
header (“http://homepage/members-area: foundcookie.php”);
?>
[/php]

I am also not sure were to place the code in a wordpress blog my first thought would be the custom_function.php file is this correct?

Thanks so much for your help! ;D

I’m afraid I can’t answer where to put this on a WordPress site, I haven’t hacked around with WordPress enough to be able to tell. However, I can tell you what’s wrong with your code. The header() function sets a HTTP header, (as you might expect), but you have to tell it which header to set. In your case, you want the Location header. Alter your lines as follows, and it should work all right for you:

[php]

<?php if (!isset ($_COOKIE['reg_user'])){ header ("Location: http://homepage.com: nocookie.php"); }else{ header ("Location: http://homepage/members-area: foundcookie.php"); } ?>

[/php]

Also, your code example appears to be missing the curly braces for your if/else block…not sure if that’s a typo, but I’m pretty sure they need to be there!

Here is my response:

[php]

<?php if (isset ($_COOKIE['reg_user'])) header ("Location: http://homepage.com/nocookie.php"); else header ("Location: http://homepage.com/members-area.php"); } ?>

[/php]

Note: normally I would add curly braces but this time it isn’t needed because it is only one line long. :wink:

Thanks for the help!

Do I need to have the location redirect to a .php or can I just use my regular url. I am not sure I understand this part. Sorry if I am being unclear.

You can use the Location header to redirect to any valid URL.

Sponsor our Newsletter | Privacy Policy | Terms of Service