Copy link to clipboard
Copied
Ok, I will try to make this simple as to avoid any
confusion.
I am trying to receive connection requests from remote applications installed on user pc's.
The application uses POST method to send the request to connect to my database, and if required update some tables.
The application is not a webpage. I have no access to the internal workings of the application nor do I have any specific information about how it sends its POST. I am sure of the following:
The app will POST to my .cfm page
"IsConnectionAllowed" = "yes"
This is asking my cfm page if its ok to connect. I would need to use a <cfif> to verify the the first part of the POST is in fact
"IsConnectionAllowed" = "yes"
If this returns true, (the app also uses to POST to pass "username" and "password") my .cfm page will then perform a query and check the username and password against the database. If a matching record is found, the application expects my page to reply with
"#Answer# OK- connected;"
Once the answer is given the application will then respond with data to insert or update my tables.
Now, I know how to do the queries and update tables etc, but I have never received data from an application before.
I tried using the <cfform> tag with a mehod of GET, and then trying to send my reply with <cfoutput>, but that didn't work. The application doesn't give any feedback or error messages. It either connects or not.
I also tried the <cfhttp> tag, but that didnt work either. I am not very experinced with coldfusion. I used it years ago and am just getting back into it.
I am sure this is a pretty easy thing to do, but my methods arent working.
Using the above information, can somone please show me how I would accomplish this communication? I can then use the same method to retrieve the data for my table updates and queries.
Here is a sample of what I have already tried:
<cfform method="get">
<cfif>
"IsConnectionAllowed" = "yes"
<!-- I need to query a username or password. But for testing I will just give the OK to connect -->
<cfoutput>#Answer# Ok - connected;</cfoutput>
<cfelse>
<!-- When I query username and password, if the query returns no records I deny the connection -->
<cfoutput>#Answer# Not connected;</cfoutput>
</cfif>
</cfform>
BTW, I have examples of this being done using .php and .asp. I need to do this using .cfm. I can post the either the .php or.asp code if it will be helpful in converting the function into a .cfm page.
Just as I speculated, you're just returning plain text - i.e. just like generating a normal web page.
The code below loosely mimics your PHP code. I didn't test it, so it might contain errors. I used tags instead of cfscript, since you might be working with ColdFusion 8 and not 9. I disable all coldfusion output and wrap all that is to be output, between <cfoutput></cfoutput>. This way no extra line breaks or spaces get ever sent. <cfscript>ed syntax would of course take care of this, which I mor
...Copy link to clipboard
Copied
To me it sounds you just need to output data as someone posts a form to your template.
You don't need <cfhttp> or <cfform> or anything to just write out a response code. Just plain CF Output should be enough.
It's another matter then, how the client application keeps a session for example, if the username/password authentication is ok - how do you know the next time that the application that is posting, is the same one which authenticated OK previously...
Since we might be missing a ton of details here, posting the php code would at least help a great deal.
-Fernis
Copy link to clipboard
Copied
Hi Fernis
,
Thank you for your reply. This data being posted is not a form. The data is being sent however using http post. Also, the data that will be sent following my page's acceptance to the conenction will all be done in the same session. Once the data is sent, the remote application terminates the connection. If in the future more data needs to be updated, it will then send a new request using the same POST method to the page. I hope that makes sense. Maybe the php code will be a little more clear as to what I am trying to achieve
Here the code php code I am trying to translate to cfm:
if($_POST['IsConnectionAllowed']=="yes")
{
// the value "$databaseconnexion" is set to true in common.php if the SQL connexion is ok
if($databaseconnexion==FALSE)
{
echo "#Answer# Error - unable to connect to mySQL database;";
return;
}
// check username and password
if($query = "SELECT * FROM user_profile WHERE UserName='$_POST[UserName]' and Password='$_POST[Password]'")
$result=@mysql_query($query);if(!$result){echo "#Answer# SQL Error - ".mysql_error();return;}
if(mysql_num_rows($result)==0)
{
echo "#Answer# Error - Username don't exist or wrong password;";
return;
}
//If we are here, username, password and connexion to SQL database is ok so we reply it's ok.
// this will also send the welcome message.
echo "#Answer# Ok - connected;";
printf("#welcome#%s#endwelcome#",$cfg['welcome_message']);
WriteDebug();
exit();
}
After the above there are update queries run...I will be able to deal with those provided I can get a grip on how to handle the initial connection request that comes via the POST.
Copy link to clipboard
Copied
Just as I speculated, you're just returning plain text - i.e. just like generating a normal web page.
The code below loosely mimics your PHP code. I didn't test it, so it might contain errors. I used tags instead of cfscript, since you might be working with ColdFusion 8 and not 9. I disable all coldfusion output and wrap all that is to be output, between <cfoutput></cfoutput>. This way no extra line breaks or spaces get ever sent. <cfscript>ed syntax would of course take care of this, which I more happily use when working with CF9.
<cfsetting enablecfoutputonly="yes">
<cfif structKeyExists(Form,"isConnectionAllowed") AND form.isConnectionAllowed EQ "yes">
<cfif databaseConnexion EQ FALSE>
<cfoutput>##Answer## Error - unable to connect to mySQL database;</cfoutput>
<cfabort>
</cfif>
<cfquery name="qResult" datasource="myDatasourceNameHere">
SELECT * FROM user_profile
WHERE UserName = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="80" value="#Form.UserName#">
AND Password = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="80" value="#Form.Password#">
</cfquery>
<cfif qResult.recordcount eq 0>
<cfoutput>##Answer## Error - Username does not exist or wrong password;</cfoutput>
<cfabort>
</cfif>
<cfoutput>##Answer## OK - connected;</cfoutput>
</cfif>
What still puzzles me is; does the client app make separate connections to your server or not - to first check if connection can be made, and then for username/password? Obviously not, although you first mentioned it might. In your code example, username and password can only be checked if isConnectionAllowed is posted on the same request.
I'd imagine the client app posts these three fields (isConnectionAllowed, username, password) also whenever it makes subsequent calls to update query data, because there's no any session handling implemented in the PHP code.
Hope this helps. *shrug* ![]()
--
- Fernis - fernis.net - ColdFusion Developer For Hire
Copy link to clipboard
Copied
Thanks for this Fernis.
I am going to give it a quick try now. And to answer your question, The application sends the update data all in the same session where connection request and the username and password are authenticated. Once that update is done via the same POST session, the remote application drops the connection and ends the session. If the app needs to update more in the future, it calls the same cfm and authenticates again. It will do this each time it has data to update or insert.
I am not using mySql or php, hence the reason I am trying to migrate this snippet of php code to cfm. I'll come back shortly and let you know if your suggested code works. Fingers Crossed.
Copy link to clipboard
Copied
Thanks Fernis.
That code allowed me to connect!
I have an error on the remote application, but I think thats due to the fact that I commented out the database connection and query strings. As I am mostly concerned with connection at this point, I just wanted to test that my cfm page will recognise the POST request and send the right answer, In this case
##Answer## Ok - connected;
That worked perfectly and the application received the ok to connect.
I can now use your code as a base to carry on with my database connection and update query.
I can't thank you enough. I wouldnt have figured that out in a million years of trial and error.
I knew it was simple though. I was over thinking it.
I do have one last question though... In the code you presented you have
<cfif structKeyExists(Form,"isConnectionAllowed") AND form.isConnectionAllowed EQ "yes">
Can you you explain this structKeyExists(Form,"isConnectionAllowed")
I have never seen this before. Why the Form followed by a comma? I am guessing you are asking if there is an element in the POST called "isConnectionAllowed". Is that correct? If so, do I not need to do that for each POST'ed element such as username, password, and each of the other fields that will be updated in the session?
Copy link to clipboard
Copied
You got it right - I am just checking that FORM scope (same as $_POST in php) has a certain "field", before I start evaluating it.
I only made the double check with one of the FORM variables because I was lazy, and I trusted that if one of the fields exist, the rest would be there too. But you might want to check for all form field variables similarly before trying to access them.
The syntax is the same as
<cfif structKeyExists(Form,"isConnectionAllowed")>
<cfif Form["isConnectionAllowed"] EQ "yes">
....
But ColdFusion allows you to chain those conditions. The second one will only be checked if the first is true, so there won't be an error when writing
<cfif structKeyExists(Form,"isConnectionAllowed") AND form["isConnectionAllowed"] EQ "yes">
Note how I introduced the [" "] notation of structure keys instead of dotted notation. Both work similarly, but if you happen to have spaces in your structure key names, StructureName["key name"] is the way to go, since StructureName.key name would of course result as an error.
Now I'm off to bed. 2:30am here... back on line in 10+ hours. I'm glad you got it! ![]()
-Fernis
Copy link to clipboard
Copied
Nice one!
Again, thank you for your help. I learned alot of new things in your replies. Always a good thing.
Sleep well. Maybe I'll trouble you more tomorrow. ![]()
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more