Title: cookie code doesn&#039;t work right
Last modified: August 20, 2016

---

# cookie code doesn't work right

 *  [georgebaker](https://wordpress.org/support/users/georgebaker/)
 * (@georgebaker)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/cookie-code-doesnt-work-right/)
 * Hi,
 * I have this code below which i have saved in functions.php
 *     ```
       function set_newuser_cookie() {
       	if (!isset($_COOKIE['sitename_newvisitor'])) {
       		setcookie('sitename_newvisitor', 1, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false);
       	}
       }
       add_action( 'init', 'set_newuser_cookie');
       ```
   
 * And then i have this code in index.php
 *     ```
       <!DOCTYPE HTML>
   
       <html>
   
       <head>
           <title>Test af database</title>
           <link rel="stylesheet" type="text/css" href="http://johnnylai.me/lotus/wp-content/themes/split/style.css">
       </head>
   
       <body>
   
       <?php
   
       if (isset($_COOKIE['sitename_newvisitor'])) {
   
            //echo 'Welcome back!';
            $cookieValue = $_COOKIE['sitename_newvisitor']; // Get value of cookie
   
            if ($cookieValue == virksomhed)
            {
               echo '<META HTTP-EQUIV="Refresh" Content="0; URL=http://www.johnnylai.me/lotus/virksomhed">';
               exit;
               // header('Location: http://www.johnnylai.me/lotus/virksomhed');
            }
               else
            {
               echo '<META HTTP-EQUIV="Refresh" Content="0; URL=http://www.johnnylai.me/lotus/privat">';
               exit;
               // header('Location: http://www.johnnylai.me/lotus/privat');
            }
       }
           else
       {
           // echo 'Hello new visitor!';
           // $cookieValue = $_COOKIE['sitename_newvisitor'];
       }
       ?>
   
       <div id="wrapper">
           <div id="top"></div>
           <a href="http://johnnylai.me/lotus/privat/index.php"><div id="left">PRIVAT</div></a>
           <a href="http://johnnylai.me/lotus/virksomhed/index.php"><div id="right">VIRKSOMHED</div></a>
       </div>
   
       </body>
   
       </html>
       ```
   
 * Here is how it should work.
    The first time the user comes into the site the 
   user clicks on one of the two links(left or right section) and a cookie is saved,
   next time the same user comes to the site they goes directly to ‘privat’ or ‘
   virksomhed’ depend on what they clicked the firt time. It works fine when i click
   on ‘privat’, but not if i click on ‘virksomhed’
 * Can anybody see what i’m missing in the code?

The topic ‘cookie code doesn't work right’ is closed to new replies.

 * 0 replies
 * 1 participant
 * Last reply from: [georgebaker](https://wordpress.org/support/users/georgebaker/)
 * Last activity: [13 years, 2 months ago](https://wordpress.org/support/topic/cookie-code-doesnt-work-right/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
