SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Cookie Tracking

    I am in need of tracking a visitor across 2 pages. On the entry page when he gets to my site I set a cookie and store the referring keyword in MySQL. My goal is to drive the visitor to page 2. I want to track which keyword the user who makes it to page 2 originally arrived with.

    Here is how I set the cookie:

    <?php
    $kwd = $_GET['seed'];
    setcookie("kwdCookie", $kwd, time()+3600, "/");
    ?>
    A different script stores it in MySQL in a field called "entrykw". Now when/if the visitor goes to page 2 I want the cookie variable/keyword stored in the same DB in the field "exitkw".

    I don't know how to track the cookie to page 2? I thought about using PHP sessions, but my knowledge in that area is even worse.

    Anyone knows what I need to do to track the cookie and how to enter the data back into MySQL?

    Thanks.

    Chris

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean ?
    PHP Code:
    echo $_COOKIE['kwdCookie']; 
    This will return your keyword that was in the variable '$kwd'. I am not sure whether this simple one is what you are looking for or not..? :-)
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not what I am looking for - echo is easy

    Ok, I researched sessions and are able to write the data to a text file - but have some problems. My data is not appending and the output looks weird when I import it into Excel.

    Here is what I use on page #1 (entry page of visitor)

    <?php
    session_start();
    //register variables
    session_register("kwd");
    session_register("kwip");
    session_register("kwreferer");
    session_register("kwlocation");
    session_register("kwquery");
    //variables definition
    $kwd = $_GET['seed'];
    $kwip = $_SERVER['REMOTE_ADDR'];
    $kwreferer = $_SERVER['HTTP_REFERER'];
    $kwlocation = $_SERVER['REQUEST_URI'];
    $kwquery = $_SERVER['QUERY_STRING'];
    setcookie("kwdCookie", $kwd, time()+3600, "/");
    //write data to file
    $sessionfile = fopen("sessionfile.txt", "w");
    fputs($sessionfile, session_encode( ) );
    fclose($sessionfile);
    ?>
    As mentioned the data is not appending. I also do not know if I need to destroy the session here or not.

    On page #2 I use the following:

    <?php
    session_start();
    //variables definition
    $kwd = $_GET['seed'];
    $kwip = $_SERVER['REMOTE_ADDR'];
    $kwreferer = $_SERVER['HTTP_REFERER'];
    $kwlocation = $_SERVER['REQUEST_URI'];
    $kwquery = $_SERVER['QUERY_STRING'];
    setcookie("kwdCookie", $kwd, time()+3600, "/");
    //write data to file
    $sessionfile = fopen("sessionfile2.txt", "w");
    fputs($sessionfile, session_encode( ) );
    fclose($sessionfile);
    session_destroy();
    ?>
    Again, it does not seem to append the data. Also - is the session destroy correctly used here? It does not seem to do it, because all testing is not working unless I use a second browser session.

    Any additional help would be appreciated. Bonus question (just kidding): How could I get this data into the same MySQL database from here?

    Thanks for your help.

    Chris

  4. #4
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Christoph View Post
    I am in need of tracking a visitor across 2 pages. On the entry page when he gets to my site I set a cookie and store the referring keyword in MySQL. My goal is to drive the visitor to page 2. I want to track which keyword the user who makes it to page 2 originally arrived with.

    Here is how I set the cookie:



    A different script stores it in MySQL in a field called "entrykw". Now when/if the visitor goes to page 2 I want the cookie variable/keyword stored in the same DB in the field "exitkw".

    I don't know how to track the cookie to page 2? I thought about using PHP sessions, but my knowledge in that area is even worse.

    Anyone knows what I need to do to track the cookie and how to enter the data back into MySQL?

    Thanks.

    Chris
    A little late, but why not read the value of the cookie named 'kwdCookie' on page 2 using $_COOKIE['kwdCookie'] ? This way, page 2 can see whether a cookie was set and then see what value of $kwd was set on page 1.


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
  •