Results 1 to 4 of 4

Thread: How to create a cookie

  1. #1
    Join Date
    Sep 2009
    Posts
    135

    How to create a cookie

    I lost in the vast Internet resources. Some offer a full paragraph, other 2 lines but does not offer what I want to do etc. I would like a cookie. Preferably in php. The smallest possible and simple cookie that prevents the activation of a javascript if it has already been run once on all of my site. For example with the following code:
    HTML Code:
    <script>
    alert('This script is repeated on every page of my site but do not view that once during the session of the surfer);
    </script>
    Could you help me?

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: How to create a cookie

    You set a cookie and put a value to "false", for example

    after you make a condition

    Code:
    if($_COOKIE['hello'] == false)
    {
     echo 'your script';
    setcookie('hello', true);
     
    
    }

  3. #3
    Join Date
    Sep 2009
    Posts
    135

    Re: How to create a cookie

    Thank you for your answer

    The following simple code does not work on my php page.
    PHP Code:
    <?php
    setcookie
    ('MyCookie''false', (time() + 3600));
    ?>
    <script>
    if($_COOKIE['MyCookie'] == false)
    {
    alert('this message will pop 1000 times!')
    setcookie('MyCookie', true);
    }
    </script>
    Can you help me to fix it?

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: How to create a cookie

    You mixed up php and javascipt:

    PHP Code:
    <?php
    setcookie
    ('MyCookie''false', (time() + 3600));
    ?>
    <script>
    <?php
    if($_COOKIE['MyCookie'] == false):
    ?>
    alert('this message will pop 1000 times!')
    <?php
    setcookie
    ('MyCookie'true);
    endif;
    ?>
    </script>
    and after that, you can manage cookies in javascript and repeat the same code in a cleaner way
    HTML Code:
    <script type="text/javascript">
      if(!document.cookie)
      {
       alert("first" );
       document.cookie = 'visited';
      }
     </script>

Similar Threads

  1. How to use ASP to maintain the cookie
    By Anirvinya in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 03:38 AM
  2. How to Create a Cookie in PHP?
    By sivaranjan in forum Software Development
    Replies: 4
    Last Post: 27-01-2010, 10:53 PM
  3. what is cookie in java?
    By Jabeen in forum Software Development
    Replies: 3
    Last Post: 21-11-2009, 02:53 PM
  4. LG Cookie applications
    By B e B o in forum Portable Devices
    Replies: 2
    Last Post: 21-09-2009, 12:52 PM
  5. What is a Cookie?
    By Dharmavira in forum Technology & Internet
    Replies: 5
    Last Post: 18-02-2009, 12:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,034,660.77635 seconds with 17 queries