I am using a script that counts clicks on various links in the home page. The site is for the community channel of a cable TV company and the user can purchase ads or post an obituary or a weather related delay or calcellation.
We want to add a count so the siteowner can see how many clicks on the paid ads links actually result in an ad being submitted for posting. When the user clicks on the button "Place Ad", an email is sent to the community channel. The following code was added to email.php
echo "The ad selected is $ad";
//Yes the name of the ad is printed to screen
switch($ad){
case "One Day General or Business Message":
$id=2;
break;
case "One Month Nonprofit Message":
$id=3;
break;
default:
echo "The ID is not getting thru.";
break;
}
completed($id);
The function called is as follows:
function completed($id){
// First check if the ID is set and if it is valid (contains only numbers)
$id = $_POST['id']; echo "ID in function completed($id) is $id<br />";
//NO! the ID does not print to screen
if(empty($id) || preg_match("/\D/",$id)) {die("Invalid ID, numbers only allowed!");}
// This scentence does print to screen
// Get lines from file
$lines=file($settings['logfile']);
// Let's find the line that contains our ID
$found=0;
$i=0;
foreach ($lines as $thisline) {
if (preg_match("/^($id\%\%)/",$thisline)) {
$thisline=chop($thisline);
// We found the line, now we get number of ads PLACED from the line
list($id,$added,$url,$count,$placed,$name)=explode("%%",$thisline);
// Increase placed by 1 and update this line
$placed++;
$lines[$i]=$id."%%".$added."%%".$url."%%".$count."%%".$placed."%%".$name.$newline;
$found=1;
break;
}
// This line didn't contain ID, lets go to the next one
$i++;
}
if($found != 1) {die("This ID doesn't exist!");}
// Rewrite the log file with the updated line
$content = implode('', $lines);
$fp = fopen($settings['logfile'],"wb") or die("Can't write to log file! Please Change the file permissions (CHMOD to 666 on UNIX machines!)");
fputs($fp,$content);
fclose($fp);
// Redirect to the link URL
Header("Location: $url");
exit();
}
?>
Does anyone have any idea why the above code doesn't work. Is there something special that must be done to assign a string to an integer value?
Any help will be greatly appreciated