Ready to get started?Download WordPress


Creating a Simple Cookie? (4 posts)

  1. exids
    Posted 5 years ago #

    Good day.

    This is my first post here, I usually find everythingI am looking for the first time through but I am ready to admit defeat on this seemingly simple problem.

    All I want to do is set a cookie with one value that would be retrieved from teh URL. I want to use simple code like this:

    $src = $_GET['src'];
    setcookie("source", $src, time() + 864000, "/", ".mysite.com");

    Outside of wordpress using a simple php file, this function poses no problem. But as soon as I add this to my function.php template, the variable "$src" is no longer inserted into the setcookie function.

    If I do the following, the value for "src" prints fine on the screen:

    $src = $_GET['src'];
    setcookie("source", $src, time() + 864000, "/", ".mysite.com");
    echo $src;

    So, why why is it that the value for "src" will not insert into the setcookie function?

    Strangely enough, when I do something like this, it DOES insert the value for "src" without problem:

    $src = '100';
    setcookie("source", $src, time() + 864000, "/", ".mysite.com");

    I know the answer must be simple, but I am absolutley stumped. Could this be a wordpress bug? Thanks a ton in advance for help in resolving this problem.

  2. exids
    Posted 5 years ago #

    By the way, the value for "src" is set in the URL as such:


  3. drumminlogan
    Posted 5 years ago #

    Did you ever find an answer? I am trying to do the same thing. Thanks.

  4. indianabenny
    Posted 4 years ago #

    I was going nuts trying to set a basic cookie as well with PHP. Finally tried javascript to set the cookie and then I was able to access the info/values via PHP. I used the Set_Cookie function located here to do it:

    I hope WP looks at why cookies are impossible to set within the system. It's extremely annoying not to be able to set a basic cookie using setcookie (a base function in PHP).

Topic Closed

This topic has been closed to new replies.

About this Topic