#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    32
    Rep Power
    1

    SQL and Site exported to new host and one error


    Hi All,

    I got stumped on something strange regarding a SQL database function being called from a PHP file.

    I transferred a site from one hosting company to another and downloaded all the web site files and exported the SQL database
    as usual. Then on the new hosting platform uploaded the site files and imported that same SQL database and then changed the SQL connection string to the new database configuration and evrything on the site looked to work just fine.

    I then had a few people tell me they could not submit the contact request form and thought, well I have done SQL database transfers before without issues after changing the connection string to the new database settings.

    If everything else was EXACTLY the same code, file names, etc.. why would the contavt form not submit?

    I checked and in the SQL DB there are two CAPTCHA tables named: captchas and captcha_temp

    The table named ' captchas ' has 3 fields labeled capid, question and answer. The table named ' captcha_temp ' has 4 fields labeled ctid, capid,
    sessid and rndkey .

    Of course I spent hours going through all the PHP files that call to the SQL DB and could not find a field for something called $ans_key and then in a string see a $cap_ans, but that is not a field in the table named ' captcha ' that it looks how it is written.

    In the PHP code here you can see a hash key or something for $ans_key, but do not see a field in any table in the SQL DB labeled $ans_key.

    Code:
    <?php
    	include("access/include/functions.php");
    	include("access/include/session.php");
    	require_once("captcha/class.captcha.php");
    	// ===================================================PRocess Mailform
    	///////// Contact     /////////-->
    	$sessid=session_id();
    	foreach($_POST as $key=>$value) {
    		$$key = $value;		
    		//echo"--// $key = $value<br>";
    	}	 
    	
    	// echo"--------------------------------------// Session";
    /* 	foreach($_SESSION as $key=>$value) {
    		$$key = $value;
    		echo"$key = $value<br>";
    	}	  
    	 */
    	 
    //$ans_key='52c6cdb12f358c0ad6c4be46e4e62dac';
    
    	// $sessid=$_SESSION['sessid'];
    	//lets  validate then destroy the temp record in the captchas
    	
    	if(!$capid)$capid=$cap_id;
    	$db=$database->connection;
    	$captcha = new Text_Captcha($db);
    	
    	if (!$captcha->validate($cap_ans,$capid,$ans_key)){ 
    		$step2=false;
    		$errmsg=true;
    	}else{
    		$captcha->deleteTempKey($capid, $sessid);
    	}	
    
    	if($step2){	
    	
    		// LET's LOG this INFO	==============================================================// 
    		$admin_errmsg=array();
    		$contact="$title $firstname $lastname";
    		$_SESSION['contact']="$title $lastname";
    
    and so on...
    Then in the class.captcha.php file called on there is nothing I see wrong, but I am an Amatuer..

    Then in the class.captcha.php file called on there is nothing I see wrong, but I am an Amatuer..

    Code:
    <?php
    
    class Text_Captcha
    {
    	// Reference to the database connection where the `captchas` table resides
    	var $db_conn = false;
    	
    	// Error message
    	var $err_msg = "";
    	//var $connection;
    	/**
    	 * Constructs the class
    	 * @param $db - Reference to the database connection where the `captchas` table resides
    	 */
    	function Text_Captcha(&$db = null) {
    		global $tryover,$sessid;
    		if (!empty($db)) $this->db_conn = $db;
    		if($tryover){ 
    			$this->sessid=$sessid;
    		}else
    			$this->sessid=session_id();
    	}
    	
    	/**
    	 * Randomly find one single question
    	 */
    	function get_question() {
    		global $tryover;
    		$ret = false;		
    		
    		if ($this->db_conn) {
    			// Count questions exist
    			$query = "SELECT COUNT(*) AS count_all FROM captchas";
    			$result = mysql_query($query, $this->db_conn);
    			$data = mysql_fetch_array($result);
    			$q_count = $data["count_all"];
    			mysql_free_result($result);
    			
    			// Get one random row
    			$query = "SELECT * FROM captchas LIMIT " . (rand(0, $q_count-1)) . ", 1";
    			$result = mysql_query($query, $this->db_conn);
    			$data = mysql_fetch_array($result);
    			$ret = $data["question"];
    			$captcha_id = $data["capid"];
    			mysql_free_result($result);
    	
    			// Persist captcha ID
    			$sessid=$this->sessid; 	
    			$this->persist_id($captcha_id,$sessid);
    			$rndkey=$this->setTempKey($captcha_id,$sessid);
    			if(!$rndkey)
    				$this->err_msg = "Key not posted";
    				
    		} else {
    			$this->err_msg = "Can not find database connection";
    		}	
    		$output=array($ret,$rndkey,$captcha_id);
    		return $output;
    	}
    
    	
    	/**
    	 * Validate user submitted CAPTCHA answer
    	 * @param $answer - user answer
    	 */
    	function validate($answer,$capid,$rndkey) {
    		
    		//	$captcha_id = $this->persisted_id('id');	//this will not work over ajax;
    		$safeid=mysql_escape_string($captcha_id);
    		$sessid=$this->persisted_id('sess');
    		$answer_ok = false;
    
    		if (strlen($answer) > 0) {
    			if ($this->db_conn) {
    				
    				$query = "
    					SELECT c.*, t.* 
    					FROM captchas c, captcha_temp t 
    					WHERE c.capid=t.capid
    					AND c.capid ='$capid' 
    					AND t.rndkey='$rndkey'";
    
    				
    //echo "<br>$query<br>";					AND t.sessid='$sessid'";
    				
    				$result = mysql_query($query, $this->db_conn);
    				
    				if($result) { 
    					$data = mysql_fetch_array($result);
    					$db_answer = $data["answer"];
    					
    					if($db_answer){
    
    					// Match answer
    						$arr_answers = split(":", $db_answer);
    						for ($i = 0; $i < count($arr_answers); $i++) {
    							$check_against = $arr_answers[$i];
    							if (preg_match("/\b$check_against\b/i", $answer)) {
    								$answer_ok = true;
    								break;
    							}				
    						}					
    					}else{
    						$this->err_msg = "Empty answer";
    					}
    				}else{
    					$this->err_msg = "Question doesn't exist";
    				}
    				mysql_free_result($result);
    			}else{
    				$this->err_msg = "Can not find database connection";
    			}
    		}else{
    			$this->err_msg = "Empty answer";
    		}
    		//update  the randkey with new vars
    /* 		if(!$answer_ok){
    			
    			$this->updateTempKey($capid);
    		
    		} */
    		return $answer_ok;
    	}
    	
    	
    	/**
    	 * Method for retrieving persisted TEMP  ID
    	 * 
    	 * @access private
    	 */
    
    	function deleteTempKey($capid, $sessid) {
    		global $database;
    
    		$table="captcha_temp";
    		$criteria="sessid='$sessid'";
    
    		$result =$database->deleteUser($table,$criteria);
    		
    		if($result) 
    			return $rndkey;
    		else 
    			return false;
    	}
    	
    	
    	
    	function setTempKey($capid,$sessid) {
    		global $session;
    		$rndkey=$session->generateRandID();
    		
    		$query = "INSERT INTO captcha_temp (capid, sessid,`rndkey`) VALUES ($capid, '$sessid','$rndkey')";
    		$result =mysql_query($query, $this->db_conn); 
    		
    		if($result) 
    			return $rndkey;
    		else 
    			return false;
    	}
    	
    	function updateTempKey($capid) {
    		global $database,$session;
    		$fvalue=$session->generateRandID();//$rndkey
    		$table="captcha_temp";
    		$fname="`rndkey`";
    		$idfield=	'capid';	
    		$idval=$capid;
    
    		$result =$database->updateField($table,$fname,$fvalue,$idfield,$idval );	
    		if($result) {
    			return $fvalue; 
    		}else 
    			return false;
    	}
    
    	
    	/**
    	 * Method for persiting currently showing CAPTCHA_ID
    	 * This is only example, you should make use the server to persist this ID, instead of cookie in the client
    	 * Normally you'd like to override this method to implement a safer mechanism,
    	 * ie. database session variables storage, temp. table, etc.
    	 *
    	 * @param $id - CAPTCHA ID
    	 * @access private
    	 */
    	function persist_id($id,$sessid){
    		$_SESSION["captcha_id"] = $id;
    		$_SESSION["sessid"] = $sessid;		
    	}
    	
    	/**
    	 * Method for retrieving persisted CAPTCAH ID
    	 * 
    	 * @access private
    	 */
    	function persisted_id($what){
    		if($what=='id'){
    			$out=$_SESSION["captcha_id"];
    		}else{ 		
    			$out=$_SESSION["sessid"];			
    		}		
    		return $out;
    	}
    
    	
    }//end class
    
    ?>
    Can anyone see anything or point me in a direction to follow as the files and forms are exactly the same as when they were
    working on the previous hosting account..

    Count a pathway have changed from the previous ' .. ' that some calls make or could have a field been corrupted? Or do I need to do something to add the $ans_key field and hash somewhere in the SL DB as I can not find that?

    Pulling the last of my hair out so I hope one of you Wizards can tell me what you see or if it could be something like a new hosting company needing to check to ensure I am on a certain PHP scripting platform, etc...

    Thanks in advance!!!

    Jay
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    What is the error? Do you have any insight into what's happening or where something might be wrong? Why do you believe it's the captcha?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    32
    Rep Power
    1

    Error


    It has to be the captcha as the form has a function forr a zip code entry field that when entered auto enters the city and state and that works as you fill out the form.

    If that works aand the answer is never accepted on the captcha that has to be it, right?

    It always states that is the incorrect captcha answer..

    Thanks again..
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    It always states that is the incorrect captcha answer..
    THAT is your problem. That summary of a paraphrase of an error message is the information you need to move forward. Don't ever assume anything. It doesn't have to be anything in particular, it HAS to be the exact error message you're given.

    Next step: Find the code where that error message is printed. Show it here.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    32
    Rep Power
    1

    Captcha and error message here


    Ok, but with this captcha that is the error message.

    Here it is exactly:

    Captcha Question : Which day is on the weekend between Wednesday, Thursday and Sunday?

    Answer: Sunday

    ERROR! You did not get the correct answer. If you are a human being, try again and make sure you spell the answer correctly.

    And this goes on and on if you have answers and questions like what is 2 + 2? obviously 4 ...

    ERROR! You did not get the correct answer. If you are a human being, try again and make sure you spell the answer correctly.


    I checked the table field for the answers to each captcha question that can be generated and they all are correct and have it so capitalization is not an issue..

    Any thoughts?
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    Ok, but with this captcha that is the error message.
    Except it's NOT though. This is a computer forum where we talk about computers. To a computer, and to us, "that is the incorrect captcha answer.." is absolutely not the same as "ERROR! You did not get the correct answer. If you are a human being, try again and make sure you spell the answer correctly."

    Still need to read the rest of my post and find this code.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    867
    Rep Power
    275
    I'm no php guru by any means and I may be wrong about this, but, I'm seeing in your class.captcha.php file things like :
    global $tryover,$sessid;
    global $tryover;

    which I believe refer to global variables.
    Was register_globals turned On in your old host?..and turned Off at your new host, by chance?
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    Originally Posted by DonR
    I'm no php guru by any means and I may be wrong about this, but, I'm seeing in your class.captcha.php file things like :
    global $tryover,$sessid;
    global $tryover;

    which I believe refer to global variables.
    Was register_globals turned On in your old host?..and turned Off at your new host, by chance?
    Good point. I figured the code was just sloppy and using globals, but it could be double sloppy and using globals created through register_globals.

    OP, do you have error_reporting turned on in php.ini or at the top of your script?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    OP's lack of response may be from him having threads elsewhere
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    32
    Rep Power
    1

    Finally Found the Answer


    I appreciate everyone's help and since the error was just a captcha error message and nothing to do with a concrete scripting line in a page's code it was hard to find the answer.

    It was corrected by actually changing the SQL DB Connection string to use DB_HOST and DB_PASSWORD as the script in the pages (PHP) was calling for DB_SERVER and DB_PASS and Godaddy used different tags for their connection string. Of course then I just had to go through the site files and change out the code snippets that had to deal with either of those calls.

    That was the first time a SQL DB import was not working due to the main generated SQL CONNECTION STRING items for me so luckily that was the resolution...

    Sure I would have found the answer quickly if when the form submitted it prompted COULD NOT CONNECT TO THE DATABASE instead of a captcha error message saying I was not verified as a human!!

    Jay
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,971
    Rep Power
    375
    if you had your errors ON then it would have, also doesnt PHP stop working if a fatal error (like connecting to db) occurs?

IMN logo majestic logo threadwatch logo seochat tools logo