aspose file tools*
The moose likes Applets and the fly likes Java Applet unable to use a PHP page to store a cookie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Java Applet unable to use a PHP page to store a cookie" Watch "Java Applet unable to use a PHP page to store a cookie" New topic
Author

Java Applet unable to use a PHP page to store a cookie

Enrico Rosina
Greenhorn

Joined: Nov 06, 2010
Posts: 14
Hello everybody,

I have one PHP page launching an Applet in my PHP application. With this Applet I would like to store user preferences in a cookie.

I found in this forum a useful thread: Applet and PHP. This PHP solution would be a nice work-around for me. Because my Java Applet is stand-alone, there is no servlet on the host side. And so no privileges set and no rights to store cookies directly from the Applet.

When I launch the PHP page manually, giving the URL and values, then the cookie is stored without problem. But when I activate this page from my Applet, I can see no trace of this cookie when I try to re-read it programmatically or if I look at the cookies using the browser (Firefox's right-click on the page allows to display them).

Is there a reason why this would not be possible? Security policy or anything else?

I won't post my Applet's code, it is pretty similar to the one from the given URL above (thank's to the author). I was able to open a connection to the PHP session with my applet, send values to this page using POST, and get values back from this page. So this part is OK.

Just in case, here is the PHP page code. To launch it manually (= not from my Applet), I just have to change 2x "_POST" by "_GET" where the page must read input cookieName and cookieValue.

Any help would be greatly appreciated.
Thanks
Enrico



Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
The URLConnection class does not handle cookies automatically; you'll have to enable that by using the java.net.CookieManager class. Its javadocs explain how that is done.
Enrico Rosina
Greenhorn

Joined: Nov 06, 2010
Posts: 14
Oops... poor me, I spent hours trying the CookieManager, implementing CookiePolicy, CookieStore and so on. As I said in post (after "edit"), I did not have much success in that, only java privilege exceptions... I think I will go back to Cobol :-)

(edit again):
And how do you explain that I can read other existing cookies in that way? I just could not store one, but I read all other cookies of my php pages from the same codebase..

Thank you for your reply, anyways
Enrico
Rendelle Fox
Greenhorn

Joined: Nov 07, 2010
Posts: 20
I just got exactly this working, just today. My problem most of the way through was that if you have the cookie set for a different portion of the server, it's hard to know which one it will read.

For example, for me and because of my several iterations of the PHP file, I had cookies under both .x10premium.org and .skyy.x10premium.org and so it only seemed to be working whenever I tried a new cookie name (which took me forever to realize).

I would suggest going in and clearing your cookies. Also, did you try clearing out the read stuff from the cookie Setting? Because I did that and it failed for me as well. Furthermore, I think you might need to be viewing the cookies in the same browser that you set them. I frequently use Firefox for nearly everything, and something else for testing applets because I can't find an easy way to get Firefox to reload the applet with my new changes without having to close all my tabs. This messed me up for a while.

Here is my current working code:

(codebase is the URL of the applet, this.getCodeBase() is you call it from the JApplet. name and value are parameters to the function)



And, cookies.php:



Good luck! If you have any other questions, feel free to ask; hopefully you can sort this out.
Enrico Rosina
Greenhorn

Joined: Nov 06, 2010
Posts: 14
Hello,

Thank you very much Randelle, I found 2-3 interesting things in your suggestions. In particular this syntax:

I did not use "SET=&", (but the parameter seemed to be read correctly in the PHP), and then I used the "POST" RequestMethod which was suggested in this post.
At least, now that you tell me that it is possible, I will insist searching for a solution.

As for the rest, I really tested in a simplified environment, in order to reduce the risks. In my application There is only one applet, completely alone in my jar file and directly inserted in the index.php (top-level page). So only one url, plus the 2 hidden pages used to read/write cookies, which are at the same url place (No subdirectories). I use firefox only to set or get the cookies, after having cleared all cookies, cache, history and so on. And as I said I also try to display them directly from the applet page (using the right-click menu). And then I can see one cookie which I created directly from the index page, but not the cookie which I created using the URLConnection.

One thing I wonder about, on this forum if you right-click on the page and select the last menu, you can see the cookies from coderanch.com (_utmb, _utmv, SESIONID, ....). After you log in you can also see the juserForumId. I was never asked to accept cookies from this site, not even before being logged in. How did they do that? Javascript? If I use a java CookieHandler and I sign my applet, I will scare the users with freightful warnings, and I am not sure they will accept, though my only aim is remember their preferred settings like the language.

As soon as I can I wil try your suggestions and give feed-back.
Have a nice day.
Enrico
Rendelle Fox
Greenhorn

Joined: Nov 07, 2010
Posts: 20
I used the SET and GET parameters so that I could read them in the PHP and thus use the same file to do both... it will only be necessary depending on how you are doing your php. Unless you mean that I put the equals in there? Because yeah, I did notice it seems to be necessary for parameters with no values, only names, to have the equals in order to be recognized when I go through the applet although doing it directly from the page URL doesn't need it.

I went from that post as well (in fact, I found it because you mentioned it) but I switched from POST to GET because if you use GET, you can use the same PHP file to add cookies manually from the browser as you do to set them from the applet. (I had thought REQUEST was supposed to accept both POST and GET, but in practice it would only accept the GET). (EDIT to be clear: it would only accept the GET *from the applet*, I have successfully used REQUEST in the past to do both POST and GET from webpages).

1. Have you been able to write a PHP page (mine should work for you, but make sure you use GET if you use it...) that lets you get and set cookies directly? (ie, for my example you would type into the browser somepage.com/cookies.php?SET&name=cookiename&value=cookievalue and then view it with either somepage.com/cookies.php?GET&name=cookiename or somepage.com/cookies.php?DEBUG)

2. You have said you cannot set a cookie with your applet, does that mean you are successfully loading it?

3. Can you post all your code?

Also, I briefly mentioned in my last post, but I think it wasn't very what I was talking about... in my code where I say " I don't know why this is necessary, but it really seems to be". If you try to just *write* the cookie without then reading (even if there's nothing *to* read) it won't work. So make sure you leave that in.
Rendelle Fox
Greenhorn

Joined: Nov 07, 2010
Posts: 20
Here, this is a much neater version of my applet code:



As a static function, it can be called like this:

SaveGame.saveData(codebase, "cookiename", "cookievalue");
Enrico Rosina
Greenhorn

Joined: Nov 06, 2010
Posts: 14
Ok Rendelle,

Here I am again after unsuccessful tests. All seems perfect, except that the cookie is not stored. But the connection is ok, if I put "echo" in your cookie.php, I can see the parameter values and good stuff, of course activating that from your SaveGame class.

You did a nice job with your SaveGame class, thank you again. Funny, I have a game on a personal site, and it would be great to use your code over there to store the cookies. Now the cookies are stored performing an input form in PHP, and I am not satisfied with the result.

Just in case you want to play cards against an applet: here is my Skip-Bo simulator

I don't have to copy paste my full code here, the SaveGame class is a perfect copy / paste of your code. But I will try to attach following sources:

  • TestApplet.php - Contains the applet tag to launch AppletToPhp.class
  • AppletToPhp.java - Applet with a button allowing to launch SaveGame
  • cookies.php - Your cookie saver / reader, in which I added some lines


  • [EDIT] => sorry, I am unable to attach these files, no zip, java, php or txt files are allowed...
    I put them here: http:/www.anisor.ch/PhpApplets.zip (link does not work, copy / paste it if you want to download)

    I tried on my localhost php site, I also tried with my www site, I tried with Firefox and with Internet Explorer. I also tried to convert my UTF-8 php files to ISO-8859-1 encoding, just in case the initial BOM would hurt.

    I'm really running short of ideas to solve this problem. Do you think there could be an incorrect setting in PHP (any global variable to set?) or in Firefox preferences? I tried to allow everything in Firefox security, just in case, but no success.

    Would someone please test from my website if the applet stores cookies? Here it would be:
    - Store a cookie manually: http://www.anisor.ch/cookies.php?MANUAL_SET&name=anumber&value=128
    - Re-read this cookie: http://www.anisor.ch/cookies.php?MANUAL_GET&name=anumber
    - Read all cookies of this site: http://www.anisor.ch/cookies.php?DEBUG=
    Note: I will not let this code for long on the site, maybe one month or so, then I will clean it up and change this post.

    Thank you again for any help you could give. Anyways you did already the most important: you told me that what I'm looking for is not impossible!
    Have a nice evening and thank you for your attention!
    Enrico
    Rendelle Fox
    Greenhorn

    Joined: Nov 07, 2010
    Posts: 20
    I'm about to leave the house, so only a quick reply for now: Yes, those links store cookies for me. Are you saying that they do or do not work for you? There's no point looking at the applet code if setting it manually isn't working. Then, it must be the settings. If setting it manually *is* working for you, then it would seem unlikely that it's the PHP settings, although I sure don't know enough about it to say for sure.

    PS. I don't see any attachments... But I'm new here, so maybe I just don't know where to look.
    Enrico Rosina
    Greenhorn

    Joined: Nov 06, 2010
    Posts: 14
    Thank you for trying!

    Manually the cookies are stored ok. It fails only when it is your class SaveGame calling them with GET or SET parameters.
    I could not attach the files, source files seem to be forbidden. I guess only images can be attached. So see my previous message, I made an edit and I gave an address where they can be found.

    Bye
    Enrico
    Rendelle Fox
    Greenhorn

    Joined: Nov 07, 2010
    Posts: 20
    Just one more question before I go. (I will try to compile your stuff later).

    You are using GET from the applet, as I do in my SaveGame class? Because if so, you shouldn't need a separate MANUAL_GET and GET parameters in the php, as far as I can see...?
    Enrico Rosina
    Greenhorn

    Joined: Nov 06, 2010
    Posts: 14
    I am using your class with methods saveData and loadData, unchanged.
    But when I try them manually, directly from the url, then I must erase all this:
    "/", "." . $_SERVER['HTTP_HOST'], 0


    If I put all these parameters in cookies.php, then it fails.
    That's why I created manual methods. I was tired to cut and paste them :-)

    PS: only images can be attached here, I found it in a topic.

    Rendelle Fox
    Greenhorn

    Joined: Nov 07, 2010
    Posts: 20
    That's odd, I didn't need to erase that stuff.

    Have you tried erasing it for what you use with the applet, too?
    Rendelle Fox
    Greenhorn

    Joined: Nov 07, 2010
    Posts: 20
    OK, I:
    1) put everything in one folder;
    2) changed to because I'm not running it off of your server;
    3) changed AppletToPhp.txt to AppletToPhp.java because I'm guessing you only changed the extension in order to attach it to your post;
    4) added to the top of SaveGame.java because it wouldn't compile without it;
    5) compiled with

    6) renamed TestApplet.txt to TestApplet.htm;
    7) opened TestApplet.htm in Firefox 3.6.9 ("Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9");
    8) clicked the second button;
    9) I got this error: java.security.AccessControlException - access denied (java.net.SocketPermission www.anisor.ch:80 connect,resolve)
    10) I looked that up and discovered that I can't try to connect to something that isn't on the same server as the applet, so I reversed step 2, renamed cookies.txt to cookies.php, and put both that and TestApplet.htm and TestApplet.jar on my server.

    When I ran it from there and checked cookies.php?DEBUG, it had stored the cookie.

    SO, if you did everything exactly like I did it, then it can't be your code, and I guess it must be your settings. Well, either that or some unexpected thing about how you're doing it, like you're somehow running cached code or something.

    I am using a hosted server (x10hosting) so I cannot access my PHP settings except this static information that they provide us, hopefully it is enough (attached).


    PS. I double-posted...



    [Thumbnail for phpsettings.png]

    Enrico Rosina
    Greenhorn

    Joined: Nov 06, 2010
    Posts: 14
    Hello Rendelle,

    Thank you again for all your work. Tonight after having re-read your posts carefully, I made some more work searching for a reason. I'm definitely convinced that the code is good, some environment conditions must be the cause of the problem. Curiously, I have dual boot Linux and Windows, so I would not expect having the same problem on both environments. As for having also the problem on my site, because my Internet provider has certainly not exactly the same settings as mine. I will try Install one or two other browsers, just to have other settings to play with.

    I will let the problem cool down a few days, I'm being tired of trying. But I will come back. Did you try my game on my site? Maybe if you like it you will be repaid a little bit for your efforts. : -)

    Bye / regards
    Enrico
    Rendelle Fox
    Greenhorn

    Joined: Nov 07, 2010
    Posts: 20
    Ah, so you don't have access to your settings either? Pity.

    The game I'm making right now is going to be running on someone else's site when I'm done, so now that we know that matters, I may ask them to try hosting it a little early for me so I can make sure it works there too. Depending on how that goes, I may have some more information for you in a few days' time.

    (I did try your game just now. I don't really know how to play, not more than a quick perusal of the rules and a single play-through can give me, but maybe when I'm not so busy I'll come back to it. It started up in French for me, though. Is that because I'm from Canada? Just so you know, only a minority of Canadians are French, and we can stop taking it in school as early as ninth grade, so lots of us aren't good enough to be able to find our way to the menu that can change the language. You might want to consider making language one of the things you can change via the much-more-obvious drop-down boxes to the right).

    EDIT: PS, I still think it's worth a try to use your MANUAL_GET and MANUAL_SET from the SaveGame class and see whether that works for you before you go delving into PHP settings.
    Enrico Rosina
    Greenhorn

    Joined: Nov 06, 2010
    Posts: 14
    Haha no, though Canada provides most visitors on my site, I'm not clever enough to guess their language.
    It's simply the default language on my site because French is my first language.

    You was supposed to click on my link given above, so you would have been welcomed in English. Here is a
    second chance to go there: Skip-Bo simulator
    I will think about your suggestion to add a language on the Skip-Bo page, in case others land there
    accidentally.

    The Skip-Bo is mainly a stand-alone application, with it's own menus and settings. But I put it in a JInternalFrame,
    so I can include it simply in the application's JFrame, or in the Applet frame. So the Skip-Bo has it's own menu and
    there should not be other settings on the PHP page. I just had to put these because up to now I was unable to
    store a cookie with the Applet. But this will change if this present post keeps its promises...
    You said also:
    I still think it's worth a try to use your MANUAL_GET and MANUAL_SET ...

    Believe me, I tried several dozens of times each syntax that I could imagine, including these options...

    Here was Switzerland speaking, have a nice evening. :-)
    Enrico
    Rendelle Fox
    Greenhorn

    Joined: Nov 07, 2010
    Posts: 20
    Ah! (For the record, I was greeted in English, but the game itself started up in French. )

    In other news, I think I have something for you:



    The server that I mentioned, that I needed my game to be able to run on, failed with my other script. After some more research, I sent this updated version over, and it worked. I am particularly suspicious that it's the ini_set line that fixed it, and I have high hopes that it will work for you, too.

    Note that the ini_set only changes the setting for the duration of the script that runs it. Also, you may want to familiarize yourself with the setting before you use it in a production version - it is apparently there for security reasons, but I suspect that with this script, which can only retrieve cookies that have the prefix it adds to cookies it sends, would be safe to use for non-secure information. I mean, if you allow all the cookies on your site to be accessed by scripts you might be opening yourself up, but if you only allow it for save-game sort of information, it won't really matter if someone steals it. So although I can't tell you this for sure, I suspect it's only non-secure for the cookies with the prefix.

    Cheers!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Java Applet unable to use a PHP page to store a cookie
     
    Similar Threads
    Cookies not getting reset in Netscape 4.72!!! URGENT!!!
    [HELP] How to login to a website and get XML data of that site
    cookie problem
    Using a method to retrieve a cookie
    SESSION TRACKING.