Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    India
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Php Cookies Help

    Hello Friends,

    my doubt is ...

    when a person clicks on a link present on Page A, it should check for the cookies (if returning or new visitor) and should take him to 2 separate pages.


    i.e Page A to Page B if returning visitor
    Page A to Page C if new visitor.

    i having started learning php and if any one could provide me the working code, it will help me a lot.

    thanks in advance

  • #2
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    the page for the link use this

    PHP Code:
    <?php


    if (isset($_COOKIE['history'])) {
        
        
    $link "http://link.com/c.html";
        
    } else {
        
        
    $link "http://link.com/b.html";
    }

    echo 
    "<a href=\"$link\">Link</a>";




    ?>
    on the page you want to make the cookie use something like

    PHP Code:
    setcookie("history""I've been here"time()+9999); 

  • Users who have thanked Zangeel for this post:

    gudipudi (05-09-2009)

  • #3
    New Coder
    Join Date
    May 2009
    Location
    India
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    thanks for the quick reply sir,

    i am trying it now and update you in 5min

  • #4
    New Coder
    Join Date
    May 2009
    Location
    India
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I am getting this error zangeel ( plz check this page http://tycheit.com/rama.php )

  • #5
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    It means you already output contents to the page. You have to set the cookie before anything else in the script.

  • #6
    New Coder
    Join Date
    May 2009
    Location
    India
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by thesavior View Post
    It means you already output contents to the page. You have to set the cookie before anything else in the script.
    below is the code how i placed in the page


    <html>
    <head></head>
    <body>
    <?php
    $inTwoMonths = 60 * 60 * 24 * 60 + time();
    setcookie('lastVisit', date("G:i - m/d/y"), $inTwoMonths);
    ?>
    <?php
    if (isset($_COOKIE['lastVisit'])) {

    $link = "c.html";
    } else {
    $link = "b.html";
    }
    echo "<a href=\"$link\">Link</a>";
    ?>
    </body>
    </html>

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Try something like
    PHP Code:
    <?php
    if(isset($_COOKIE['lastVisit'])){
    $link "c.html";
    }
    else{
    $inTwoMonths 60 60 24 60 time();
    setcookie('lastVisit'date("G:i - m/d/y"), $inTwoMonths);
    $link "b.html";
    }
    ?>
    <html>
    <head></head>
    <body>

    <?php

    echo "<a href=\"$link\">Link</a>";
    ?>
    </body>
    </html>
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    New Coder
    Join Date
    May 2009
    Location
    India
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Try something like
    PHP Code:
    <?php
    if(isset($_COOKIE['lastVisit'])){
    $link "c.html";
    }
    else{
    $inTwoMonths 60 60 24 60 time();
    setcookie('lastVisit'date("G:i - m/d/y"), $inTwoMonths);
    $link "b.html";
    }
    ?>
    <html>
    <head></head>
    <body>

    <?php

    echo "<a href=\"$link\">Link</a>";
    ?>
    </body>
    </html>
    Thanks sir, the previous error is gone....but it still opens link C even if refresh cookies or delete cookies

  • #9
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Could you please update your online page ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #10
    New Coder
    Join Date
    May 2009
    Location
    India
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Could you please update your online page ?
    updated and still the same
    http://tycheit.com/rama.php

  • #11
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by gudipudi View Post
    updated and still the same
    http://tycheit.com/rama.php
    It's working as specified in the code. For the first time the link points to b.html and when I refresh the page, it's getting pointed to c.html
    Do you need to check whether the user visited that page? Then you need to set the cookie from that page (You may need to change the extension to .php)
    Last edited by abduraooft; 05-09-2009 at 12:37 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    gudipudi (05-09-2009)

  • #12
    New Coder
    Join Date
    May 2009
    Location
    India
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    It's working as specified in the code. For the first time the link points to b.html and when I refresh the page, it's getting pointed to c.html
    SO you need to check whether the user visited that page? Then you need to set the cookie from that page (You may need to change the extension to .php)
    thanks a lot sir for your kind help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •