ianj844 Posted March 25, 2015 Share Posted March 25, 2015 Hi, I am working on a system for my client. I have created a hook code that creates a random key and inserts it into MYSQL. The code is working except it is creating 4 codes and inserting them every time it runs. What could be the cause of this? Thanks, Ian 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted March 25, 2015 Share Posted March 25, 2015 What is the hook that you are using? 0 Quote Link to comment Share on other sites More sharing options...
ianj844 Posted March 25, 2015 Author Share Posted March 25, 2015 <?php if (!defined("WHMCS")) die("This file cannot be accessed directly"); /* Load the Database */ $db_host = "localhost"; $db_username = "REMOVED"; $db_password = "REMOVED"; $db_database = "REMOVED"; $connect = mysql_connect($db_host,$db_username,$db_password); $database = mysql_select_db($db_database,$connect); /* Functions */ /*Key Generator*/ function KeyGen(){ $key = md5(microtime()); $new_key = 'SMVA-'; for($i=1; $i <= 12; $i ++ ){ $new_key .= $key[$i]; if ( $i%4==0 && $i != 12) $new_key.='-'; } return strtoupper($new_key); } /*Assign Key And Email */ function hook_send_key_email() { $line = KeyGen(); $link = mysql_query("INSERT INTO api_keys (key_code) VALUES ('$line')") or die(mysql_error()); $merge_fields['pull_key'] = $line; return $merge_fields; } add_hook("EmailPreSend", 1,"hook_send_key_email"); ?> 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted March 25, 2015 Share Posted March 25, 2015 First of all why are you establishing a connection to database? You're already connected. Secondly why attaching this task to EmailPreSend hook? This hook point runs every single time WHMCS sends an email. I don't know what is the purpose of KeyGen() function but I find pretty impossible that this is the right hook point for you. What's the point? 0 Quote Link to comment Share on other sites More sharing options...
ianj844 Posted March 25, 2015 Author Share Posted March 25, 2015 Because it's being inserted into a different database than the WHMCS database. The Keygen is what generates the key 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted March 25, 2015 Share Posted March 25, 2015 Ok but what's the point of the Key? Is it related to orders, client registration or specific products/services? I'm asking this because I don't understand the reason why you need to store this key for every email sent by WHMCS. 0 Quote Link to comment Share on other sites More sharing options...
ianj844 Posted March 25, 2015 Author Share Posted March 25, 2015 I don't that's the problem we have one product that requires a key to use its an instant issue key the keygen generates the key then we need to email it to the customer 0 Quote Link to comment Share on other sites More sharing options...
sentq Posted March 26, 2015 Share Posted March 26, 2015 I don't that's the problem we have one product that requires a key to use its an instant issue key the keygen generates the key then we need to email it to the customer as it generates the "key" when sending email, you may need to modify it so your key will be generated when the specified email template used, well to be clear you need to do this for example: /*Assign Key And Email */ function hook_send_key_email($vars) { if ($vars['messagename']=="Generated Key Template Or Something Else"){ $line = KeyGen(); $link = mysql_query("INSERT INTO api_keys (key_code) VALUES ('$line')") or die(mysql_error()); $merge_fields['pull_key'] = $line; return $merge_fields; } } this way key is generated once and when needed 0 Quote Link to comment Share on other sites More sharing options...
ianj844 Posted March 26, 2015 Author Share Posted March 26, 2015 Thank you soooo much! This worked wonderfully! 0 Quote Link to comment Share on other sites More sharing options...
sentq Posted March 26, 2015 Share Posted March 26, 2015 Thank you soooo much! This worked wonderfully! you're welcome 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.