SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    tracking a user with php?

    How would i make it so if a user visits

    http://www.mysite.com/a.php

    i would have a javascript set a cookie

    then if the same user visits (within 30 days or so)

    http://www.mysite.com/b.php

    it'll write their ip address to ips.txt?

    is this simple to do?

    someone told me that this could also be done with php sessions, so that's why i posted it here. can someone point me to the right direction?

  2. #2
    SitePoint Enthusiast lotrgamemast's Avatar
    Join Date
    Apr 2005
    Location
    Cheshire, England
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could do it with PHP cookies.

    In a.php have somehing like:

    PHP Code:
    <?php

    setcookie
    ('user_ip'$_SERVER['REMOTE_ADDR']);

    ?>
    and on b.php:

    PHP Code:
    <?php

    if ( isset($_COOKIE['user_ip']) ) {
              
    $handle fopen('ips.txt''a');
              if ( 
    fwrite($handle$_COOKIE['user_ip']) === FALSE ) {
                        die(
    'Cannot write to file');
              }
              echo 
    'IP wrote to file';
    }

    ?>
    Obiusly needs perfecting but that should be fine. Also note i havent atually tested that as ive not got access to my development server at the minute. Should work though.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks alot! works perfectly, but i have some questions if you dont mind:

    1.) $handle = fopen('ips.txt', 'a') <---- what does "a" do? does it open up a.php from the server?


    I just thought of something and I would like to track from different pages.

    Let's say i have these pages (i'll add the setcookie here):

    a1.php
    a2.php
    a3.php


    and of course in b.php i'll add all of the tracking code.


    can i do something like this?

    in a1.php
    PHP Code:
    <?php
    setcookie
    ('a1'$_SERVER['REMOTE_ADDR']);
    ?>
    in a2.php
    PHP Code:
    <?php
    setcookie
    ('a2'$_SERVER['REMOTE_ADDR']);
    ?>
    in a3.php
    PHP Code:
    <?php
    setcookie
    ('a3'$_SERVER['REMOTE_ADDR']);
    ?>


    then in b.php i'll have all of the checking if there's a cookie:




    PHP Code:


    <?php
    if ( isset($_COOKIE['a1']) ) {
              
    $handle fopen('ips.txt''a1');
              if ( 
    fwrite($handle$_COOKIE['user_ip']) === FALSE ) {
                        die(
    'Cannot write to file');
              }
              echo 
    'IP wrote to file';
    }


    if ( isset(
    $_COOKIE['a2']) ) {
              
    $handle fopen('ips.txt''a2');
              if ( 
    fwrite($handle$_COOKIE['user_ip']) === FALSE ) {
                        die(
    'Cannot write to file');
              }
              echo 
    'IP wrote to file';
    }


    if ( isset(
    $_COOKIE['a3']) ) {
              
    $handle fopen('ips.txt''a3');
              if ( 
    fwrite($handle$_COOKIE['user_ip']) === FALSE ) {
                        die(
    'Cannot write to file');
              }
              echo 
    'IP wrote to file';
    }

    so in ips.txt it will check which pages the users came from. will that 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
  •