Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: If cookie is set, then display content of a different div

  1. #1
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    230
    Thanks
    158
    Thanked 1 Time in 1 Post

    Default If cookie is set, then display content of a different div

    Hi guys:

    Could you please direct me to a script that could set a cookie on the the first visit to a page... then, when it finds that cookie on the second visit, it will display the content of a different div.

    I've searched a long time but I cant find anything that will do this. I assume I've been using the wrong search words.

    Thanks,

    Kenny
    Last edited by KennyP; 11-07-2010 at 05:20 PM.

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Cookies are really easy to set using jQuery. Here's a link to a few tutorials on Google: http://www.google.co.uk/#hl=en&sourc...50cf575c8019d8
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. The Following User Says Thank You to Beverleyh For This Useful Post:

    KennyP (11-07-2010)

  4. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    This should provide you all the information you need for the PHP solution.

    http://php.net/manual/en/function.setcookie.php

    If you have questions or problems post back.
    Corrections to my coding/thoughts welcome.

  5. The Following User Says Thank You to bluewalrus For This Useful Post:

    KennyP (11-07-2010)

  6. #4
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    230
    Thanks
    158
    Thanked 1 Time in 1 Post

    Default

    Thank you for your replies, BeverlyH and bluewalrus. I checked both links and found good cookie setting tutorials worth reading. However, they mention nothing in regard to displaying div 1 on the first page visit, and switching to div 2 on all subsequent visits. That is, until the cookie expires.

    Will using the php solution always work, as opposed to not working with the java method in browsers where java is disabled?

    Thanks

  7. #5
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try:
    Code:
    <?php
    if(isset($_COOKIE['div'])){
      echo "<div>I can tell you've been here before</div>";
    } else {
      setcookie('div', true, 5184000 + time()); //hold fo 2 months
      echo "<div>Its your first time being here</div>";
    }
    ?>
    Good luck!
    Jeremy | jfein.net

  8. The Following User Says Thank You to Nile For This Useful Post:

    KennyP (11-07-2010)

  9. #6
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    230
    Thanks
    158
    Thanked 1 Time in 1 Post

    Default

    Thank you Nile. I entered your PHP code at the very top of a page and it works perfectly. The proper messages display on first and second visit.

    To integrate the actual content of my two divs with your code, do you advise subtituting their content within your code, or should it be done by assigning IDs?

  10. #7
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Just change the following:
    Code:
    <?php
    if(isset($_COOKIE['div'])){
      echo "<div>I can tell you've been here before</div>";
    } else {
      setcookie('div', true, 5184000 + time()); //hold fo 2 months
      echo "<div>Its your first time being here</div>";
    }
    ?>
    Red = after cookie is set
    Highlighted = first timer

    Good luck
    Jeremy | jfein.net

  11. The Following User Says Thank You to Nile For This Useful Post:

    KennyP (11-07-2010)

  12. #8
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    230
    Thanks
    158
    Thanked 1 Time in 1 Post

    Default

    I moved your code into the body section, as is, but it's now generating "...cannot modify header information...headers already sent."

    I came across this problem some time ago with another script, but I can't remember the solution.

    EDIT:

    I remembered; I removed all white spaces before and after the opening and closing tags, but the same message is still generated.
    Last edited by KennyP; 11-05-2010 at 10:07 PM.

  13. #9
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    At the top of your page put:
    Code:
    <?php
    $revisit = false;
    if(isset($_COOKIE['div'])){
      $revisit = true;
    } else {
      setcookie('div', true, 5184000 + time()); //hold fo 2 months
      $revisit = false;
    }
    ?>
    Then where you want your divs put:
    Code:
    <?php
    if($revisit){
    echo "<div>You've been here</div>"
    } else {
    echo "<div>Its your first time!</div>
    }
    ?>
    Jeremy | jfein.net

  14. The Following User Says Thank You to Nile For This Useful Post:

    KennyP (11-07-2010)

  15. #10
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    230
    Thanks
    158
    Thanked 1 Time in 1 Post

    Default

    Nile:

    It's now working perfectly, after using your previous example to add a quote mark and semicolons, as follows...

    <?php
    if($revisit){
    echo "<div>You've been here</div>";
    } else {
    echo "<div>Its your first time!</div>";
    }
    ?>

    I'll now substitute the contents of my two divs...it should work.

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
  •