SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem with function and global

    Hello all. I am writing a function that I need to return a true/false. I also need to return an error message if one exists. From what the little hamster in my head is telling me, using "global" and the variable name should make that variable available to the rest of the script. I can't get it to work.

    Here is my code. The pac variable is set with a valid code.
    PHP Code:
    function activationCheck($pac$pLength=6){

        global 
    $activateMessage;

        if(
    strlen($pac) != $pLength){
            
    // Since the acceptible length is 6 chars, we have an error.
            
    $activateMessage "The activation code you supplied is invalid.  Please verify your code and try again.";
            
            return 
    false;
        }else{
            
    // Since we only accept X characters and this is X chars, we may continue.
            
            
    $passwdCheck mysql_query("select userid, password, active, actcode, gentime from useractivate_t where actCode = '$pac' and active = '0'") or die(mysql_error());
            
    $passwdResult mysql_fetch_array($passwdCheck);
            
            if(
    mysql_num_rows($passwdCheck) == 0){ // We didn't find any records with this activation code.
                
    $activateMessage "I'm sorry, but I am unable to verify you activation code. Please verify your code and try again.";
                
                return 
    false;
            }else{
                
    $genTime time() - 60*60*24// 60 seconds * 60 minutes * 24 hours
                
                
    if($passwdResult['gentime'] < $genTime){
                    
    // This was generated over 24 hours ago. Send an error
                    
    $activateMessage "I'm sorry, but the password exiration time has passed.  Please request a new password.";
                    
                    return 
    false;
                }else{
                    return 
    true;
                }
            }
        }
    }

    activationCheck($pac
    Thanks

    Chris

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why check for true or false? All you have are error messages, so check for a value on the $activateMessage variable and if there is no value, then "true", else "false".

    Remove the "global $activateMessage" and replace the "activationCheck($pac)" with the following:
    PHP Code:
    if(!$message activationCheck($pac))
      
    $message 'Activation complete!';
     
    echo 
    $message
    Is this something that you are looking for?
    John

  3. #3
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function activationCheck($pac$pLength=6)
    {
      if(
    strlen($pac) != $pLength)
      { 
    // Since the acceptible length is 6 chars, we have an error.
        
    $activateMessage "The activation code you supplied is invalid. Please verify your code and try again.";
      }
      else
      { 
    // Since we only accept X characters and this is X chars, we may continue.
        
    $passwdCheck mysql_query("select userid, password, active, actcode, gentime from useractivate_t where actCode = '$pac' and active = '0'" ) or die(mysql_error());
        
    $passwdResult mysql_fetch_array($passwdCheck);
        if(
    mysql_num_rows($passwdCheck) == 0)
        { 
    // We didn't find any records with this activation code.
          
    $activateMessage "I'm sorry, but I am unable to verify you activation code. Please verify your code and try again.";
        }
        else
        {
          
    $genTime time() - 60*60*24// 60 seconds * 60 minutes * 24 hours
          
    if($passwdResult['gentime'] < $genTime)
          { 
    // This was generated over 24 hours ago. Send an error
            
    $activateMessage "I'm sorry, but the password exiration time has passed. Please request a new password.";
          }
        }
        return 
    $activateMessage;
      }
    }
    if(!
    $message activationCheck($pac))
    $message 'Activation complete!';

    echo 
    $message
    John

  4. #4
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Soooo, basically, if no $activateMessage, then the user activation code is valid. Good suggestion. I'll think on this and see what damage I can do.

    Thanks

    Chris

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree 8) There is no errors in your script. Instead of attempting to return true or false, return the string $activateMessage instead.

    To be sure that the user/whatever is valid, have $activateMessage = ''; at the beginning of the function and use this as an alternative to PHP John's suggestion:

    PHP Code:
    if(activateCheck($pac) == (string) '') {
    # good 
    } else {
    # bad

    And your laughing 8)

  6. #6
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, with a quick test, it doesn't validate. I am testing a invalid $pac and it is returning as valid. Even echoing the mysql_num_rows, I get 0. However, the message I get back is passed.

    I'll post the whole code below.

  7. #7
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function activationCheck($pac, $pLength=6){

        if(strlen($pac) != $pLength){
            // Since the acceptible length is 6 chars, we have an error.
            $activateMessage = "The activation code you supplied is invalid.  Please verify your code and try again.";
            
            //return false;
        }else{
            // Since we only accept X characters and this is X chars, we may continue.
            
            $passwdCheck = mysql_query("select userid, password, active, actcode, gentime from useractivate_t where actCode = '$pac' and active = '0'") or die(mysql_error());
            $passwdResult = mysql_fetch_array($passwdCheck);
            echo mysql_num_rows($passwdCheck);
            if(mysql_num_rows($passwdCheck) == 0){ // We didn't find any records with this activation code.
                $activateMessage = "I'm sorry, but I am unable to verify you activation code. Please verify your code and try again.";
                
                //return false;
            }else{
                $genTime = time() - 60*60*24; // 60 seconds * 60 minutes * 24 hours
                
                if($passwdResult['gentime'] < $genTime){
                    // This was generated over 24 hours ago. Send an error
                    $activateMessage = "I'm sorry, but the password exiration time has passed.  Please request a new password.";
                    
                    //return false;
                }
            }
        }
    }

    function passwordSecurity($password){
        // This function will look at the submitted password when the user first signs up.
        // It will look and verify that the password matches the criteria below.
        // Alpha - 6 chars
        // Numbers - 2 chars
        //
        for($i=0; $i < strlen($password); $i++){
            $pCheck = substr($password, $i, 1);
            if(ereg("^[[0-9]]*$",$pCheck)){
                $numb = $numb + 1;
            }
            if(ereg("^[[a-zA-Z]]*$",$pCheck)){
                $alpha = $alpha + 1;
            }
        }
        if($alpha < 6 || $numb < 2){
            return false;
        }else{
            return true;
        }
    }

    function userPassword($pac){
        // This function will allow the users to enter their new password into the system.
        // When this form is submitted, it will call itself and then verify the password has
        // the needed security in it.  6 Alpha & 2 Numbers
        ?>
        <form action="<? echo $_SERVER['PHP_SELF']; ?>" name="blah" method="post">
        <input type="hidden" name="pac" value="<?=$pac;?>">
        <input type="text" name="password" size="10" maxlength="10">
        <input type="submit" name="submit" value="Submit Password">
        </form>    
        <?
        
    die;
    }

    if(!
    $activateMessage activationCheck($pac)){ // Need to search the database and see if this pac id is in the database.

        // Valid activation code found.  Ok to continue
        
    if($_POST){
            if(
    passwordSecurity($_POST['password'])){
                
    $activateMessage "passed";
                
            }else{
                
    userPassword($_POST['pac']);
            }
        }else{
            
    userPassword($_GET['pac']);
        }
        echo 
    $activateMessage;
    }

  8. #8
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Who's suggestion though ? PHP John's or mine 8)

  9. #9
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, that would be PHP John's.

    Chris

  10. #10
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are not returning the $activateMessage value, that I can see. Look at my original post. Near the bottom of your function you will see "return $activateMessage"
    John

  11. #11
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right. I didn't post the test code. I simply replaced the "false" with $activeMessage and then removed the final else section. Always validated true.

    Chris

  12. #12
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So is the code you posted above your test or production code?
    John

  13. #13
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uhhhh, yes??? Is there something wrong with it? Tell me. I can take it.

    Chris

  14. #14
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Call me a nitpicker Dr. Livingston, but what good is it to cast an empty string to a string:
    PHP Code:
    (string) '' 
    ?

    '' is already a string so there's no need to cast it to a string

  15. #15
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chris, maybe you don't understand. Your function, as you have it posted on #7, activationCheck() has ALL of the "returns" commented out, therefore it is returning nothing at all.
    John

  16. #16
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function activationCheck($pac, $pLength=6)
    {
      $activateMessage = ''; 
      if(strlen($pac) != $pLength)
      { // Since the acceptible length is 6 chars, we have an error. 
        $activateMessage = "The activation code you supplied is invalid.  Please verify your code and try again."; 
      }
      else
      { // Since we only accept X characters and this is X chars, we may continue. 
         
        $passwdCheck = mysql_query("SELECT userid, password, active, actcode, gentime
                                    FROM
                                      useractivate_t
                                    WHERE
                                      actCode = '$pac' AND
                                      active    = 0")
                       or die(mysql_error()); 
        $passwdResult = mysql_fetch_array($passwdCheck); 
        
        echo mysql_num_rows($passwdCheck); 
        
        if(!mysql_num_rows($passwdCheck))
        { // We didn't find any records with this activation code. 
          $activateMessage = "I'm sorry, but I am unable to verify your activation code. Please verify your code and try again."; 
        }
        else
        { 
          $genTime = time() - 60*60*24; // 60 seconds * 60 minutes * 24 hours 
           
          if($passwdResult['gentime'] < $genTime)
          { // This was generated over 24 hours ago. Send an error 
            $activateMessage = "I'm sorry, but this activation code has expired.  Please request a new activation code."; 
          } 
        } 
      }
      return $activateMessage; 
    }
     
    function passwordSecurity($password)

      // This function will look at the submitted password when the user first signs up. 
      // It will look and verify that the password matches the criteria below. 
      // Alpha - 6 chars 
      // Numbers - 2 chars 
      for($i=0; $i < strlen($password); $i++)
      { 
        $pCheck = substr($password, $i, 1); 
        if(ereg("^[[0-9]]*$", $pCheck))
        { 
          $numb = $numb + 1; 
        } 
        if(ereg("^[[a-zA-Z]]*$", $pCheck))
        { 
          $alpha = $alpha + 1; 
        } 
      } 
      if($alpha < 6 || $numb < 2)
      { 
        return false; 
      }
      else
      { 
        return true; 
      } 

     
    function userPassword($pac)

      // This function will allow the users to enter their new password into the system. 
      // When this form is submitted, it will call itself and then verify the password has 
      // the needed security in it.  6 Alpha & 2 Numbers 
      ?> 
      <form action="<? echo $_SERVER['PHP_SELF']; ?>" name="blah" method="post"> 
      <input type="hidden" name="pac" value="<?=$pac;?>"> 
      <input type="text" name="password" size="10" maxlength="10"> 
      <input type="submit" name="submit" value="Submit Password"> 
      </form>     
      <?php 
      
    die; 
    }
     
     
    if(!
    $activateMessage activationCheck($pac))
    // Need to search the database and see if this pac id is in the database. 
      // Valid activation code found.  Ok to continue 
      
    if($_POST['submit'])
      { 
        if(
    passwordSecurity($_POST['password']))
        { 
          
    $activateMessage "passed"
        }
        else
        { 
          
    userPassword($_POST['pac']); 
        } 
      }
      else
      { 
        
    userPassword($_GET['pac']); 
      } 
      echo 
    $activateMessage
    }
    John

  17. #17
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I miss-understood your suggestion in your first post. That is why it wouldn't work for me. I'll try this in the am.

    Thanks

    Chris


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
  •