5 Replies - 540 Views - Last Post: 03 November 2010 - 01:09 AM Rate Topic: -----

#1 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Identical functions - 1 not working

Posted 03 November 2010 - 12:53 AM

Ok... so I'm working on a Content Management System for giggles while I'm looking for a job... and I'm getting some seriously strange behavior from my script.


This is from database.php (script handles all database related functions)

  /**
   * emailTaken - Returns true if the email has
   * been taken, false otherwise.
   */
   function emailTaken($email){
      if(!get_magic_quotes_gpc()){
         $email = addslashes($email);
      }
      $q = "SELECT email FROM ".TBL_USERS." WHERE email = '$email'";
      $result = mysql_query($q, $this->connection);
      return (mysql_numrows($result) > 0);
   }
   
   
   
  /**
   * checkActKey - Returns true if the activation key
   * is valid.
   */
   function checkActKey($key){
      if(!get_magic_quotes_gpc()){
         $key = addslashes($key);
      }
      $q = "SELECT key FROM ".TBL_KEYS." WHERE key = '$key'";
      $result = mysql_query($q, $this->connection);
      return (mysql_numrows($result) > 0);
   }




The function on top, emailTaken() works PERFECTLY FINE... while checkActKey() doesn't like to work.




The form passes the input data to a function called from process.php (my form handler)

<?php
   function procRegister(){
      global $session, $form;
      if(ALL_LOWERCASE){
         $_POST['xuser'] = strtolower($_POST['xuser']);
      }
      $retval = $session->register($_POST['xuser'], $_POST['xpass'], $_POST['email'], $_POST['actKey']);
      if($retval == 0){
         $_SESSION['reguname'] = $_POST['xuser'];
         $_SESSION['regsuccess'] = true;
         header("Location: ".$session->referrer);
      }
      else if($retval == 1){
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
      else if($retval == 2){
         $_SESSION['reguname'] = $_POST['xuser'];
         $_SESSION['regsuccess'] = false;
         header("Location: ".$session->referrer);
      }
   }
?>




... and you can see from there, the data is passed along to another function to actually process it in session.php:

<?php
  function register($subuser, $subpass, $subemail, $subact){
      global $database, $form, $mailer;  //The database, form and mailer object
      
      /* Username error checking */
      $field = "user";  //Use field name for username
      if(!$subuser || strlen($subuser = trim($subuser)) == 0){
         $form->setError($field, "* Username not entered");
      } else {
         /* Spruce up username, check length */
         $subuser = stripslashes($subuser);
         if(strlen($subuser) < 4){
            $form->setError($field, "* Username below 4 characters");
         }
         else if(strlen($subuser) > 30){
            $form->setError($field, "* Username above 30 characters");
         }
         /* Check if username is not alphanumeric */
         else if(!eregi("^([0-9a-z])+$", $subuser)){
            $form->setError($field, "* Username not alphanumeric");
         }
         /* Check if username is reserved */
         else if(strcasecmp($subuser, GUEST_NAME) == 0){
            $form->setError($field, "* Username reserved word");
         }
         /* Check if username is already in use */
         else if($database->usernameTaken($subuser)){
            $form->setError($field, "* Username already in use");
         }

         /* Check if username is banned */
         else if($database->usernameBanned($subuser)){
            $form->setError($field, "* Username banned");
         }
      }

	/* Check if activation key is valid */
	$field = "actKey";
	if($database->checkActKey($subact)){
		$form->setError($field, "* Invalid Activation Code");
	}

      /* Password error checking */
      $field = "pass";  //Use field name for password
      if(!$subpass){
         $form->setError($field, "* Password not entered");
      }
      else{
         /* Spruce up password and check length*/
         $subpass = stripslashes($subpass);
         if(strlen($subpass) < 4){
            $form->setError($field, "* Password too short");
         }
         /* Check if password is not alphanumeric */
         else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){
            $form->setError($field, "* Password not alphanumeric");
         }
         /**
          * Note: I trimmed the password only after I checked the length
          * because if you fill the password field up with spaces
          * it looks like a lot more characters than 4, so it looks
          * kind of stupid to report "password too short".
          */
      }
      
      /* Email error checking */
      $field = "email";  //Use field name for email
      if(!$subemail || strlen($subemail = trim($subemail)) == 0){
         $form->setError($field, "* Email not entered");
      }
      else{
         /* Check if valid email address */
         $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
                 ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
                 ."\.([a-z]{2,}){1}$";
         if(!eregi($regex,$subemail)){
            $form->setError($field, "* Email invalid");
         }
         else if($database->emailTaken($subemail)){
            $form->setError($field, "* Email already in use");
         }
      }

      /* Errors exist, have user correct them */
      if($form->num_errors > 0){
         return 1;  //Errors with form
      }
      /* No errors, add the new account to the */
      else{
         if($database->addNewUser($subuser, md5($subpass), $subemail, $subact)){
            if(EMAIL_WELCOME){
               $mailer->sendWelcome($subuser,$subemail,$subpass);
            }
            return 0;  //New user added succesfully
         }else{
            return 2;  //Registration attempt failed
         }
      }
   }
?>






I'm pretty sure I've included absolutely everything that is executed along the path to this error message.




Note: If I make the following changes:

<?php
  /**
   * checkActKey - Returns true if the activation key
   * is valid.
   */
   function checkActKey($key){
      if(!get_magic_quotes_gpc()){
         $key = addslashes($key);
      }
      //$q = "SELECT key FROM keys WHERE key = '$key'";
      //$result = mysql_query($q, $this->connection);
      //return (mysql_num_rows($result) > 0);
      return 0;
   }
?>



The script runs PERFECTLY, even executing the checkEmail() function without flaw, which is theoretically identical to checkActKey()



I'm at a loss of words here. I'm a 10 year veteran PHP programmer and I've been stumped on this problem for 2 days now. I was a very active member in the PHP section here @DIC a while back... and I know there are some bright minds here, so lets see what you can all come up with ;)

Is This A Good Question/Topic? 0
  • +

Replies To: Identical functions - 1 not working

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: Identical functions - 1 not working

Posted 03 November 2010 - 12:57 AM

did you print the query strings and did you check those strings directly on the DB?

This post has been edited by Dormilich: 03 November 2010 - 12:57 AM

Was This Post Helpful? 0
  • +
  • -

#3 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: Identical functions - 1 not working

Posted 03 November 2010 - 12:59 AM

just thought I should add, the specific error message I am getting is this:


Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/thediamo/public_html/include/database.php on line 152


Line 152:

      return (mysql_numrows($result) > 0);


This post has been edited by pr4y: 03 November 2010 - 01:00 AM

Was This Post Helpful? 0
  • +
  • -

#4 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: Identical functions - 1 not working

Posted 03 November 2010 - 01:01 AM

I may be wrong here but isn't the word 'key' a specific keyword to SQL and not be used as a variable/column name in SQL?


EDIT: Just noticed your error message, should it be changed to mysql_num_rows perhaps..?

This post has been edited by Moshambi: 03 November 2010 - 01:03 AM

Was This Post Helpful? 1
  • +
  • -

#5 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: Identical functions - 1 not working

Posted 03 November 2010 - 01:06 AM

View PostMoshambi, on 03 November 2010 - 12:01 AM, said:

I may be wrong here but isn't the word 'key' a specific keyword to SQL and not be used as a variable/column name in SQL?




WOW! - I hadn't thought of that. I changed the table to actKeys from keys and the column from key to actKey... and the error goes away!


I should of posted here yesterday, would of saved me a TON of headache!
Was This Post Helpful? 0
  • +
  • -

#6 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: Identical functions - 1 not working

Posted 03 November 2010 - 01:09 AM

I sure do hate those headaches so I'm glad I could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1