Feature Help - 50 Max Loss From 1 IP

Discussion in 'PHP' started by American, Aug 7, 2006.

  1. #1
    I'm making a referrer system which will use credits to decide weather or not ads are displayed... If a user has credits, 1 credit is removed per impression and no ad is displayed... If they have no credits, then an ad is displayed... I'd like to make it where a single IP can only count for the loss of a max of 50 credits per day... I've made this much so far:


    
    // Connect to database
    $Database_Host = "********";
    $Database_Name = "********";
    $Database_Username = "********";
    $Database_Password = "********";
    
    $DBConn = mysql_connect($Database_Host, $Database_Username, $Database_Password) or die("Could not connect to database server. Check your settings...");
    $DB_DB = mysql_select_db($Database_Name, $DBConn) or die("Could not connect to database ($Database_Name). Perhaps you don't have the right permissions on this DB. Check your settings...");
    
    
    $query=mysql_query("SELECT adcredits FROM testing"); 
    $query_row=mysql_fetch_array($query);
    
    
    // Generate the ad space...
     if (($query_row[adcredits]) > 0){
    	$subtracted = $query_row[adcredits] - 1; 
    	$sql = mysql_query("UPDATE testing SET adcredits='$subtracted'"); // Update the database after subtraction
      echo "No ad will be displayed since they have credits... You have $subtracted left... "; 
     }
     else {
      echo "Ad is displayed since you have no credits... <BR>[the ad would go here]"; 
    	$sql = mysql_query("UPDATE testing SET adcredits='0'"); // Ensure credit amount isin't a negative number...
     }
    
    
    PHP:
    Everything above works fine... But I'd like to add the feature mentioned above... Any ideas? I was thinking maybe cookies... But any working alternative will do fine...

    Thanks...
     
    American, Aug 7, 2006 IP
  2. sandossu

    sandossu Guest

    Messages:
    2,274
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don`t know how could work if you set no username, but i will make the feature you wanted, i hope it works:

    
    
    
    // Connect to database
    
    
    $Database_Host = "********";
    
    $Database_Name = "********";
    
    $Database_Username = "********";
    
    $Database_Password = "********";
    
     
    
    $DBConn = mysql_connect($Database_Host, $Database_Username, $Database_Password) or die("Could not connect to database server. Check your settings...");
    
    $DB_DB = mysql_select_db($Database_Name, $DBConn) or die("Could not connect to database ($Database_Name). Perhaps you don't have the right permissions on this DB. Check your settings...");
    
     
    $query=mysql_query("SELECT adcredits FROM testing");
    
    $query_row=mysql_fetch_array($query);
    
    
    
    // Generate the ad space...
    
    if(!isset($_COOKIE['visited']) || $_COOKIE['visited'] != 'yes') {
    
     if (($query_row[adcredits]) > 0){
    
        $subtracted = $query_row[adcredits] - 1;
    
        $sql = mysql_query("UPDATE testing SET adcredits='$subtracted'"); // Update the database after subtraction
    
      echo "No ad will be displayed since they have credits... You have $subtracted left... ";
    
     }
    
     else {
    
      echo "Ad is displayed since you have no credits... <BR>[the ad would go here]";
    
        $sql = mysql_query("UPDATE testing SET adcredits='0'"); // Ensure credit amount isin't a negative number...
    
     } 
    
    setcookie('visited','yes',time() + (60*60*24));
    
    }
    
    PHP:
     
    sandossu, Aug 7, 2006 IP
  3. American

    American Guest

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It gives this error:

    Warning: Cannot modify header information - headers already sent by (output started at /home/michael/public_html/test/frontend.php:41) in /home/michael/public_html/test/frontend.php on line 47
     
    American, Aug 7, 2006 IP
  4. Edynas

    Edynas Peon

    Messages:
    796
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I think that your setcookie should be before any other output so before the echo statements
     
    Edynas, Aug 8, 2006 IP
  5. American

    American Guest

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks! Works perfect now!!!
     
    American, Aug 8, 2006 IP
  6. sandossu

    sandossu Guest

    Messages:
    2,274
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    0
    #6
    or you could put a simple <?php ob_start();?> on the first line of the script
     
    sandossu, Aug 8, 2006 IP