0 ){ $domain = $hostparts[$tld-1]; $ids = array( 'id', 'C', 'kw' ); foreach( $ids as $param ){ if( !empty( $params[$param] ) ){ $source = makealphanum( $params[$param] ); } if( $source ) break; } if( !empty( $source ) ){ $tid = $prefix . substr( $domain . $source, -16 ); } } } // Last resort: we weren't able to create a tracking ID, so // generate a unique string to server as our ID. if( empty( $tid ) ){ $tid = substr( getfaketid( $prefix ), 0, 24 ); } $afflink = 'http://' . $affiliateid . '.' . $vendorid . '.hop.clickbank.net/?tid=' . $tid . $extra; header( "Location: $afflink" ); // Write out the data to our CSV file $fp = fopen( $logfile, 'a' ); if( $fp ){ fputs( $fp, logmsg() ); fclose( $fp ); } //************************************************************* // Functions used by the code above... //************************************************************* // Convert the string to a lower case alphanumeric-only string function makealphanum( $str ){ return ereg_replace( '[^a-z0-9]', '', strtolower( urldecode( $str ) ) ); } // Parse a query string into its constituent parts function parse_query( $var ){ $var = html_entity_decode( $var ); $var = explode( '&', $var ); $arr = array(); foreach( $var as $val ){ $x = explode( '=', $val ); $arr[$x[0]] = $x[1]; } unset( $val, $x, $var ); return $arr; } // Encode URLs for saving in the CSV file by converting // quotes and commas to URL escapes. function csvencode( $str ){ $str = str_replace( '"', '%22', $str ); $str = str_replace( ',', '%2C', $str ); return $str; } // Create the line of CSV data to append to the log file function logmsg(){ global $request; global $ip; global $referrer; global $browser; global $tid; global $reqtime; global $afflink; $msg = "$reqtime,$tid,$afflink,$ip," . csvencode( $request ) . "," . csvencode( $referrer ) . "," . csvencode( $browser ); return $msg . "\n"; } // Generate a fake TID with the given prefix. function getfaketid( $prefix ){ return uniqid( $prefix ); } ?>