SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Location
    Yakima WA USA
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need hint for HTM/PHP connection

    I use a PHP script to generate HTM pages. I would like to log information regarding visitor 'clicks' I now have a PHP line:

    print " <a href=\"" . $SaleURL . "\"> For Purchase Click Here! </a>\n";

    I would like to modify that line so that when someone clicked on that link, my program will open a log file - append a line with Date & URL information and close the log file then proceed to the link URL.

    How do I do this?

    Thanks - Charles

  2. #2
    SitePoint Addict rvdavid's Avatar
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there,

    Quote Originally Posted by micheck View Post
    I use a PHP script to generate HTM pages. I would like to log information regarding visitor 'clicks' I now have a PHP line:

    print " <a href=\"" . $SaleURL . "\"> For Purchase Click Here! </a>\n";

    I would like to modify that line so that when someone clicked on that link, my program will open a log file - append a line with Date & URL information and close the log file then proceed to the link URL.

    How do I do this?

    Thanks - Charles


    The link $SaleURL will have to point to a processing page (like for example purchase.php) which does the following things:

    Code:
    - open a log file 
    - append a line with Date & URL information
    - close the log file 
    - redirect to the link URL
    Here's a quick example.

    Your product page - product.php
    PHP Code:
    $linkToSalePage 'http://www.salepage.com/viewproduct.php?productid=23423';
    $SaleURL 'purchase.php?link='.$linkToSalePage;
    print 
    " <a href=\"" $SaleURL "\"> For Purchase Click Here! </a>\n"
    Your function library - functions.php
    I recommend that you create a function library - which is a file consisting of reusable functions.

    This alone would make your application/site a little more modular so that you can reuse certain components - like logging and input validation in this case.

    What functions do is abstract the logic required to do certain things and gives allows you to reuse it in more than one place throughout your application, instead of having to copy and paste it when such functionality is needed.

    PHP Code:
    function cleanURL($link)
    {
         
    // do something to clean the URL and return the string - let's make believe that it returns false if the URL is deemed invalid and/or empty.
    }

    function 
    logEvent($type$eventInfo)
    {
    // use $type variable to decide what type of event it is you are logging.
    // open the file using fopen etc
    // write the required data into the file
    // close the file

    purchase.php would look something like:
    Log and redirect to the link URL - purchase.php
    PHP Code:
    require 'functions.php'// include your function library in the script so that purchase.php has access to functions like cleanURL() and logEvent()
    $link  cleanURL($_GET['link']);
    if (
    $link) {
        echo 
    'invalid link';
        exit;
    } else {
        
    logEvent('purchase'$link);
    }

    // clean your link file 
    header('Location: $link); 
    As you progress with your programming, you'll find better ways to create modular and extensible applications using the same concepts that is demonstrated in the example above.

    The first steps though, is to learn about abstraction and reuse.

    HTH. Good luck with it.

    Regards,


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
  •