GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Oct-2004, 12:15
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough
Unhappy

variables in functions help


Aiight i'm having a few probs assigning a value to a variable. In the main progam code I can assign the same variable different values. In this case i'm using a boolean variable called $haltemail to work with a sendmail script. It is false at the beginning of the code, and if any errors occur then its value is changed to true. This works all good except for when I use a to check for errors, and then another function to display error messages. Somewhere in my code the value isn't being assigned to the same variable. IE it stays false when I say: $haltemail = true;

There is obviously and issue with how I use this variable with the functions...but I though if I originally declared the variable not being in a function..it should be a global varable and all functions use that variable?

Take a look at my example code, this is how i declare my $haltemail variable in my main program:

PHP Code:

$haltemail = false;

function check_banlist($banlist, $email, $recipient, $allowedrecipients, $haltemail) {
   if (count($banlist)) {
      $allow = true;
      foreach($banlist as $banned) {
         $temp = explode("@", $banned);

         if ($temp[0] == "*") {
            $temp2 = explode("@", $email);
            if (trim(strtolower($temp2[1])) == trim(strtolower($temp[1])))
               $allow = false;
         } else {
            if (trim(strtolower($email)) == trim(strtolower($banned)))
               $allow = false;
         }
      }

     }

   if (!$allow) {
      print_error("You are using from a <b>banned email address.</b>");
      $haltemail = true;
   }

// lets now compare the recipient to the allowed recipients in the array
   $allowsend = false;
   $allowboo = false;

   if ((count($allowedrecipients)) && (!$haltemail)) {
        $recallow = true;
        // check recipient with all values in array. If one exists that matches recipient then send mail
        foreach($allowedrecipients as $checkallowed)
        {
                 if (trim(strtolower($recipient)) == trim(strtolower($checkallowed)))
                 $allowsend = true;
        }

        // check domain of email against domain specified in array
        if (!$allowsend)
        {
              foreach($allowedrecipients as $recbanned)
              {
                       if (substr($recbanned, 0, 1) == "@")
                       {
                           $temp = explode("@", $recbanned);
                           $temp2 = explode("@", $recipient);
                           if ((trim(strtolower($temp2[1])) == trim(strtolower($temp[1]))) && (!$allowedboo))
                           $allowedboo = true;
                       }

              }
              // oops...recipient not found in array :( - Print error and haltemail
              if (!$allowedboo) {
              print_error("You are using an <b>unorthorized recipient email address:</b><i> $recipient</i>");
              $haltemail = true;
              }
        }
        // found a match, make sure not to haltemail and carry on with script
        if ($allowsend)
        $haltemail = false;
   }
   return $haltemail;
//end check banlist & recipient check
}

// lets call the function
if ($banlist)
   check_banlist($banlist, $email, $recipient, $allowedrecipients, $haltemail);

// if $haltemail false then send mail
if (!$haltemail)
sendmail(email, subject etc);
else print "Email not sent!"; 



if error occured $haltemail should be set to true and email not sent but this is not happening...help!!!
  #2  
Old 15-Oct-2004, 23:33
JasonMichael's Avatar
JasonMichael JasonMichael is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Posts: 135
JasonMichael has a spectacular aura about
It looks like a very simple fix. Since $haltemail is not global, that function manipulates it privately. Nonetheless, I see that you are returning the value of $haltemail, but when you call the function, you aren't retrieving the returned value.

Therefore, the fix will be as follows... change this code:
PHP Code:

// lets call the function 
if ($banlist) 
   check_banlist($banlist, $email, $recipient, $allowedrecipients, $haltemail); 



To say this instead:
PHP Code:

if ($banlist) 
       $haltemail=check_banlist($banlist, $email, $recipient, $allowedrecipients, $haltemail); 



I hope this helps.
  #3  
Old 16-Oct-2004, 11:59
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough
hmmm okay i guess i soughta understand that...

Lets say if i was to declare $haltemail = true in one function called print_error...would it the same concept?
Like so:

Instead of declaring the $haltemail variable here...
PHP Code:

if (!$allow) {
      print_error("You are using a <b>banned email address.</b>");
      $haltemail = true;
   } 



...I want to declare it in the print_error function. IE when ever there is an error.
So would this work?:


PHP Code:

function print_error($reason, $haltemail) {
      print "The form was not submitted for the following reasons:<p class=\"textnorm\">\n";
      print "<ul><font class=\"textnorm\">\n";
      print $reason."\n";
      print "</ul><p class=\"textnorm\">Please <a href=\"javascript:history.back(1);\">go back</a> to the form and try again.</p>\n";
       $haltemail = true;
}

if (!$banned) 
$haltemail = print_error("You are using a <b>banned email address.</b>", $haltemail); 



Or is there not a way to make $haltemail a completely global variable where i don't have to use varaible passing between functions etc?
  #4  
Old 16-Oct-2004, 14:01
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough
thanx alot jasonmichael big help i have finally solved my problems!!

Check out my code in action:

fill out the form however you want...this email will not be sent cos the hidden $recipient field is set to user@hotmail.com which is not allowed recipient.

www.spliffsa.com

My sendmail script was being abused by spammers, now i have successfully secured it through using only allowed recipient email addy's.

Check out the main contact page:

www.spliffsa.com

This contact page will eventually control all form data throughout the site...
Peace!
  #5  
Old 16-Oct-2004, 14:07
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough
Here's some of the working code to check email and recipient validility with a working $haltemail variable....

PHP Code:

// make $haltemail true if any error occurs
function print_error($reason,$haltemail) {
   global $version;
//   build_body($title, $bgcolor, $text_color, $link_color, $vlink_color, $alink_color, $style_sheet);
   print "The email was not sent for the following reasons:<p class=\"textnorm\"><ul><font class=\"textnorm\">";
   print $reason."\n</ul><p class=\"textnorm\">Please <a href=\"javascript:history.back(1);\">go back</a> to the form and try again.</p>\n";
   $haltemail = true;
   return $haltemail;
}

// function to check the banlist and allowed recipients

function check_banlist($banlist, $email, $recipient, $allowedrecipients, $haltemail) {
   $email = trim($email);
   // lets not check against banned emails if email is invalid
   if (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,6}$", $email))
   $haltemail = print_error("Your <b>email address</b> is invalid",$haltemail);
   else {

         if (count($banlist) && (!$haltemail))
         {
             $allow = true;
             foreach($banlist as $banned)
             {
                      $temp = explode("@", $banned);
                      if ($temp[0] == "*")
                      {
                           $temp2 = explode("@", $email);
                           if (trim(strtolower($temp2[1])) == trim(strtolower($temp[1])))
                           $allow = false;
                      }
                      else
                      {
                          if (trim(strtolower($email)) == trim(strtolower($banned)))
                          $allow = false;
                      }
             }

         }
   if (!$allow) $haltemail = print_error("You are using a <b>banned email address.</b>",$haltemail);
   }


// lets now compare the recipient to the allowed recipients in the array
   $allowsend = false;
   $allowedboo = false;

   if ((count($allowedrecipients)) && (!$haltemail)) {
        $recallow = true;
        // check recipient with all values in array. If one exists that matches recipient then send mail
        foreach($allowedrecipients as $checkallowed)
        {
                 if (trim(strtolower($recipient)) == trim(strtolower($checkallowed)))
                 $allowsend = true;
        }

        // check domain of email against domain specified in array
        if (!$allowsend)
        {
              foreach($allowedrecipients as $recbanned)
              {
                       if (substr($recbanned, 0, 1) == "@")
                       {
                           $temp = explode("@", $recbanned);
                           $temp2 = explode("@", $recipient);
                           if ((trim(strtolower($temp2[1])) == trim(strtolower($temp[1]))) && (!$allowedboo))
                           $allowedboo = true;
                       }

              }
              // oops...recipient not found in array :( - Print error and haltemail
              if (!$allowedboo) {
              $haltemail = print_error("You are using an <b>unorthorized recipient email address:</b><i> $recipient</i>",$haltemail);
              }
        }
        // found a match, make sure not to haltemail and carry on with script
        if ($allowsend)
        $haltemail = false;
   }
   return $haltemail;
//end check banlist & recipient check
}


// lets check email validility
if ($banlist) $haltemail = check_banlist($banlist, $email, $recipient, $allowedrecipients, $haltemail); 


  #6  
Old 16-Oct-2004, 21:20
JasonMichael's Avatar
JasonMichael JasonMichael is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Posts: 135
JasonMichael has a spectacular aura about
Looks pretty good! I'm glad it worked out.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variables out of an iframe by url JUNK KED MySQL / PHP Forum 5 31-Jul-2007 10:33
conflict between printf and stdarg.h va functions mirizar C Programming Language 3 12-Jul-2004 09:11
About variables in C mantako C Programming Language 3 08-Jul-2004 05:11
Understanding functions tommy69 C Programming Language 15 15-Mar-2004 18:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:54.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.