6 replies
Hi i am trying to make cookie using this code

$cookiename = "abcd";
$cookie_value = "12345";
$expire_time = time()+60*60*24*30;
setcookie("$cookiename", "$cookie_value", "$expire_time");

but my code is not working is there any error ? please tell me
#cookies
  • Profile picture of the author David Beroff
    Make sure that you are doing this on the very first line of output, i.e., before any other HTML is output. Cookies need to be set in the header lines.

    Also not sure why you are surrounding the variables with quotes in the setcookie(); they're not needed.
    Signature
    Put MY voice on YOUR video: AwesomeAmericanAudio.com
    {{ DiscussionBoard.errors[8787256].message }}
  • Profile picture of the author chrisranjana.com
    Originally Posted by lovefax89 View Post


    but my code is not working is there any error ? please tell me
    Can you explain in more details what exactly is not working? Are you not able to read the cookie ?
    Signature

    Chris, Developer, Chrisranjana.com,
    AWS Developer GCP Engineer Python NodeJS Programmer

    {{ DiscussionBoard.errors[8787867].message }}
    • Profile picture of the author lovefax89
      Originally Posted by chrisranjana.com View Post

      Can you explain in more details what exactly is not working? Are you not able to read the cookie ?
      mean's cookie is not creating, i cannot see cookie in my browser, this code is working fine in my localhost but not in hosting

      When i try to create cookie like this
      setcookie("abc", "123", time()+60*60*24*30);

      Code is working but it's not working with any variables.
      {{ DiscussionBoard.errors[8788293].message }}
      • Profile picture of the author David Beroff
        Originally Posted by lovefax89 View Post

        Code is working but it's not working with any variables.
        Right. See my second point above. You're passing a string as a number.
        Signature
        Put MY voice on YOUR video: AwesomeAmericanAudio.com
        {{ DiscussionBoard.errors[8788308].message }}
        • Profile picture of the author lovefax89
          Originally Posted by David Beroff View Post

          Right. See my second point above. You're passing a string as a number.
          Thanks for your help i found this error in my log "PHP Warning: Cannot modify header information - headers already sent by........"

          to remove this error i use "ob_start()" in my code now cookie is creating
          {{ DiscussionBoard.errors[8788592].message }}
          • Profile picture of the author David Beroff
            Originally Posted by lovefax89 View Post

            "PHP Warning: Cannot modify header information - headers already sent by........"
            Right, and that was my first point. You're welcome.
            Signature
            Put MY voice on YOUR video: AwesomeAmericanAudio.com
            {{ DiscussionBoard.errors[8788926].message }}

Trending Topics