• 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.