Execute a remote function for inserts in DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

purefan
Forum Newbie
Posts: 15
Joined: Wed Aug 01, 2007 9:35 am

Execute a remote function for inserts in DB

Post by purefan »

Hello everyone!

First post here and its a question, bommer.
Well I need to have a system do a remote insert on a database, every X action the system would do this insert. Since the implementation could change we decided to host the actual function that does the insert in a remote server, and the client system would fetch this function and use it.

My approach was to fetch the code into a variable, create the function there with create_function and then just use it, it does work except for the parameters passed to the new function are not being taken into account by the code in the function.
So, there are inserts in the database, but with empty values.

Here is how I am building the function in the client system:

Code: Select all

// remPath is defined elsewhere
$remFile = $this->readRemoteFile($remPath);
// now $remFile has the code in the form of not-a-function but using variables

// now I create the function
$doRemLog = create_function('$thisIp, $license, $currUrl, $comment, $isAttempt',$remFile);
// as you can see there are several variables, that are used in the code for the function in $remFile
// this is probably where I am wrong

// then I know the code is being executed properly because in the code for the function
// there is a check for the parameters to see if they are empty, this check is working
// on this line (me trying to call the function)
$doRemLog($thisIp,$license,$currUrl,$comment,"1");

Could any of you wise guys help me debug this?
in short, the function is being created and it works, the parameters used in the function are always being used as empty.
I did check, and they are not empty.

Thank you!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

As far as I could understand this is your problem:

create_function('$thisIp, $license, $currUrl, $comment, $isAttempt',$remFile); - that means that you *define* a function with these *names* for the parameters, not values.

You really need to call the function as you do at the last line in your example.
Last edited by VladSun on Wed Aug 01, 2007 10:04 am, edited 2 times in total.
There are 10 types of people in this world, those who understand binary and those who don't
purefan
Forum Newbie
Posts: 15
Joined: Wed Aug 01, 2007 9:35 am

Post by purefan »

Hello VladSun,

Thank you for your reply, yes thats correct, but when I call it here:

Code: Select all

$doRemLog($thisIp,$license,$currUrl,$comment,"1");
Should it be using the predefined variables:

Code: Select all

$thisIp = 192.10.10.10;
$license= "My License code";
$currUrl = " _ ".$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$comment = "Still testing";
?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Code: Select all

$remFile = $this->readRemoteFile($remPath);

// Let us have $remFile = 'echo "$thisIp, $license, $currUrl, $comment, $isAttempt"';

$doRemLog = create_function('$thisIp, $license, $currUrl, $comment, $isAttempt',$remFile);

$_ip = 20;
$_license = 'GPL';
$currURL = 'blank';
$my_comment = 'No comment';

$doRemLog($_ip,$_license,$currURL,$my_comment, 1);
PS: Look above for my edited post.
There are 10 types of people in this world, those who understand binary and those who don't
purefan
Forum Newbie
Posts: 15
Joined: Wed Aug 01, 2007 9:35 am

Post by purefan »

hmm, copy/pasted it and the result is the same, empty inserts.
Im wondering, in the function code (long code) what should be the variables name? same as in the create_function right? like this:

Code: Select all

// these are the ones we're using to create_function: $thisIp, $license, $currUrl, $comment, $isAttempt
// so in the code they would work like this:
echo "thisIp: $thisIp";
//correct?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Yes, it is correct.
Can you give more code, pls?
There are 10 types of people in this world, those who understand binary and those who don't
purefan
Forum Newbie
Posts: 15
Joined: Wed Aug 01, 2007 9:35 am

Post by purefan »

sure, here is the code for the remote function

Code: Select all

//             Remote code to be used as a function in the client system


	// First, lets connect to the database
	$dbhost = 'localhost';
	$dbuser = 'purefan_external';
	$dbpass = 'external';
	$dbname = 'purefan_external';
	$dbtable = 'licensesReports';
	
	
	$conn_LR = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
	mysql_select_db($dbname,$conn_LR);	
	
	// first, lets check none of the values is empty
	if(	   $thisIp == ''
		|| $license == ''
		|| $currUrl == ''
		|| $comment == ''
	){echo "<br><hr>They are empty!:<br>thisIp: $thisIp<br>license:$license<br>currUrl:$currUrl<br>comment:$comment<hr>";} 
	
	// now lets check for the license to exist
	// Host for licenses
	$_hoMyCompany = '';
	// User for licenses
	$_usMyCompany = '';
	// Password for licenses
	$_paMyCompany = '';
	// Database name for license
	$_dbMyCompany = '';
	// Table name for the licenses
	$_taMyCompany = '';
	// Field in the table that holds the license key
	$_licenseField = '';
	// Field in the table that has the users email address
	$_emailField = '';
	
/*	$conn_MyCompany = mysql_connect($_hoMyCompany, $_usMyCompany, $_paMyCompany) or die(mysql_error());
	mysql_select_db($_dbMyCompany, $conn_MyCompany);
	// Need to know how is the table structured but heres a guess
	$_sqlCheck = "SELECT $_licenseField, $_emailField FROM $_taMyCompany WHERE $_licenseField = '$license' LIMIT 1";
	$_sqlCheckQ = mysql_query($_sqlCheck) or die(mysel_error()."<br>$_sqlCheck");
	*/
	if(true)//mysql_num_rows($_sqlCheckQ) > 0)
	{
		// the license is valid
		//lets proceed with the insert then
//		$_fromMyCompany = mysql_fetch_array($sqlQ, MYSQL_ASSOC);
		$_email = "fake@email.com";//$_fromMyCompany["$_emailField"];
		
		$sqlInsert = "INSERT INTO $dbtable(ip,license,domain,isAttempt,comment,time,userEmail) VALUES('$thisIp','$license','$currUrl','$isAttempt','','".time()."','$_email')";
		mysql_query($sqlInsert) or die(mysql_error()."<br>$sqlInsert");
		// return true;	
	}
In here, there was to be a validation for the license code but we skipped that part until this first dummy inserts work.

Need anything else?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

So, you do not see the message "They are empty!", but blank records are inserted?
There are 10 types of people in this world, those who understand binary and those who don't
purefan
Forum Newbie
Posts: 15
Joined: Wed Aug 01, 2007 9:35 am

Post by purefan »

Yeap, I do see the They're empty message. So im thinking Im not calling the parameters fine but I cant see why...
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Code: Select all

<?php
error_reporting(E_ALL);

$remFile = '{echo "$thisIp, $license, $currUrl, $comment, $isAttempt";}';
$doRemLog = create_function('$thisIp, $license, $currUrl, $comment, $isAttempt',$remFile); 

$_ip = 20;
$_license = 'GPL';
$currURL = 'blank';
$my_comment = 'No comment';

$doRemLog($_ip,$_license,$currURL,$my_comment, 1); 
?>
And I've got this output:

20, GPL, blank, No comment, 1
There are 10 types of people in this world, those who understand binary and those who don't
purefan
Forum Newbie
Posts: 15
Joined: Wed Aug 01, 2007 9:35 am

Post by purefan »

its funny because I can see it works with small functions, a multiplication, a divide... but with my big code it doesnt seem to work, its like if create_function can only work with a one-operation process.

Is there another way you could suggest so I can use that remote function? I didnt want to rely on allow_url_fopen as the client is likely to be used in many locations and this would imply having another requirement... but if its the only way I could just "require" this remote file....
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

You may use FTP directory for uploading/including php files :)
There are 10 types of people in this world, those who understand binary and those who don't
purefan
Forum Newbie
Posts: 15
Joined: Wed Aug 01, 2007 9:35 am

Post by purefan »

hmm... how do you mean?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

By giving Apache (or whatever www server you have) access to directories owned by specific FTP users. They upload the *.inc.php file and you include it.
There are 10 types of people in this world, those who understand binary and those who don't
purefan
Forum Newbie
Posts: 15
Joined: Wed Aug 01, 2007 9:35 am

Post by purefan »

so, you mean like using php to connect to both servers, the client and the server, to copy the file over to a temp location where I can just include it?
Post Reply