Using java to communicate with a PHP script on my server - java

I need to be able to access my database, which in it's own isn't hard as java can directly access it. I want to, however, use a php script to access it, as I need to insert stuff into the database as well, and I don't want to have the username and password of a read-write accoutn for my database in my java code. If someone decompiles it, he can just access my database and do stuff with it...
So basicly, I want to use a PHP script and send $_POST request info from my java code to my php script
(yes, java, not javascript ;-) )

You can do this, no problem. Use a URLConnection with setDoOutput(true), and get its output stream for the POST.
Of course, your PHP script should make sure that all data which is sent is sane, as anyone could send such a request to your PHP script. (Or you would need some way for the Java application to authenticate itself to the PHP script, which simply shifts the problem of hiding these credentials instead of the database ones.)

You should store DB credentials in configuration file to avoid embedding them directly into the code.

Related

Java client connecting to PHP server for MySQL database with authentication

I'm working on a Java application that asks the user for a username & password, and then connects to a server to verify the entered details. If they are valid, it will then ask the server (by requesting a PHP page) for data about that user. The user can modify this information using the application GUI, and changes are sent back to the server.
The main challenge is that the server doesn't use any Java. I need to make the server only use PHP, and it must be able to handle connections from different users simultaneously.
What would be the best way to go about doing this?
EDIT: The application will be requesting multiple different scripts from the server for different types of data, and will need to send and receive quite a bit of data (Probably up to 500 pieces at a time).
Create a PHP file that accepts POST.
In your JAVA, try passing a custom header KEY:VALUE pair that's verified via your PHP file - or try to think of a more elaborate way.
Then, post along the username and password to the PHP file: POST http://server.com/java-auth.php?user=username&pass=password for pseudo example. Then, have that PHP file return a JSON-encoded string or a serialized string... or go super fancy, and encrypt it all with public/private keys.
Hope this helps :)

PHP, Java Applet communication

Here's what I would like to do.
I have a PHP file in my server where I would like to call java applet. The applet function will send a get request to read a page from third party server. Now I want page read from applet function to be sent to PHP script. To simply put ,i want the return value of the applet request function in a PHP variable. Is it possible to do?
I want to do this way because I already have the code to parse the page information in PHP, so I don't want to rewrite that in java again.
I wanted the Java applet because the request has to be sent using the client information like IP. So I don't want to use proxies.
Note: I am not trying to hack anyone's server. I am not a advanced programmer of either Java or PHP. Please reply me in a descriptive manner possibly with pseudo code.
I already have the code to parse the page information in PHP, so I don't want to rewrite that in java again.
PHP should be able to get that page more easily than can a Java applet. The applet would need to be trusted or in communication with a site that uses the 'cross-domain resources' file that explicitly allows hot-linking.
Searches on 'php proxie' seemed to spill out around 7.32 million hits. I'd start there.

connect to ms sql database in a server

i am working on a app which should take data from a database on a server. it is a ms sql database. i need some help like little code hints or some useful information.
thank you!
1) Create some ASP, JSP, PHP pages which recieve some parameters and will operate over the database (for example: create page authenticate which will accept username and password parameters and will return true or false, to make it more simple)
2) On your Android application, you should use for example HttpClient to make request to authenticate page you have created(if you have a public domain if you you don't, you can install web server on your machine, connect your Android to USB port and access your webpage using following IP: 10.0.2.2)
This is was just a simple example when your responce will containt only true or false. I would advice you also to give a look at JSON or XML parsing libraries. So on web server you should return your data encoding it with JSON or XML(sure you can also create your own protocol, everythin is up to you) and on Android you should parse it using JSON or XML parsing libs.
ALTERNATIVE
1) Create web service and call web service from Android
Give a look at some tutorials like:
http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/
http://kindohm.com/post/1097012742/androidhttpclientjson.html
Google more if it's still will be unclear for you.
Good luck ;)
Create scripts on your server to communicate between the database and your app, php scripts may be? Or if you are comfortable with SOAP/ASP.NET?

Is there a way to access cookies stored in Chrome from a Java program

Here is my ideal situation. I log into www.philstockworld.com using chrome. Once logged in I start up my java application that uses the cookies just stored by chrome. Then my java application goes to work. So here is my question.
Here is what my program can do now, I can login to the website using whatever browser I want, then look up the value of the PHPSESSID cookie and start up my app using that value. Then my app can do what it needs to. I can also supply my app with my username and password and have it log in, then store the returned PHPSESSID cookie and do what it needs to. However, what I would like to have happen is I login to the website using a browser, then my app starts and uses the PHPSESSID cookie from my browser session, without me having to look it up and copy it.
Is there a way for my java application to get the value of that cookie, without me having to manually type it in?
The location of the Cookies file is:
On Linux:
$HOME/.config/google-chrome/Default/Cookies
For other OS's see the user data page on chromium.org.
However, the file is stored in a binary format, so it's going to be hard for you to load the data within.
Joel's answer tells you where the cookie data is stored. This data is a sqlite3 database file. See this question for how to read a sqlite3 database.
I can't find how/where Chrome stores cookies, on Linux at least. Chances are that they won't be cached as simple plain text files and thus not be easily readable. You say you don't want to hard code your username/password in your java app - but why do you have do this? You could just pass them as arguments to your app?

Java and PHP sessions

I'm creating a Java applet which communicate with my PHP website by requesting pages and retrieving their contents. It works pretty well, and it allows my applet to use PHP sessions.
However, I tried to launch the applet with Opera (instead of Firefox), and it appears that Opera doesn't let my applet use its PHP session, and as Java (or at least IcedTea) doesn't handle PHP sessions itself, I can't make my applet work.
Is there any way to make Java be able to handle PHP sessions?
You would have to carry the PHP session ID in every query. That is, make a start_session.php which returns only the result of session_start(); and echo session_id() and use that number in the subsequent queries, that is, other.php?sid=XXXXX inside other.php use session_id($_GET['sid']); to start the session.

Categories