Link to home
Start Free TrialLog in
Avatar of cas27
cas27

asked on

Can PHP and Servlets use the same session variables?

Hi,

A question for all of you more experienced programmers out there.  I am currently learning PHP, and know a fair amount of java servlets, and I want to have a servlet share a piece of information with my PHP page via session variables.

I am 99% sure that is impossible, do to how PHP and servlets work with sessions, but I have not been able to find a definitive answer.

This is pretty urgent so that's why the question is worth 500 pts.

Thanks in advance!
Craig
Avatar of lozloz
lozloz

hi,

i'm guessing you're set on using sessions and can't use POST values, attach a variable to a URL or set a cookie. how do java servlets deal with sessions? i suppose you've tried doing session_start(); print_r($_SESSION); on the first php page after setting your session values in java?

loz
Avatar of cas27

ASKER

loz,

I have tried that and the only thing that is output is "Array()".  Do you know what that means?

The reason I want to use sessions is that I have a whole bunch of servlets already working together with sessions, and I wanted to try and integrate PHP into the application.

I have used POST values and appending variables to the URL and got it to work, but how the site is set up, it would be easier for me to be able to access the same session data from within PHP as from the servlets.

I have thought of creating a table in my database with all of the pertinent info, but that means a lot of updating, and I'm not sure I want to go down that road yet.

My site is set up using 2 frames; the sidebar and the workspace.  The sidebar consists of static html pages that change depending on who accesses the site, with the links calling a servlet to be displayed in the workspace.

I suppose I can just put the links to the PHP pages in the workspace area, but I wanted to avoid doing that to keep the site design the same.

Thoughts?  Thanks!
Craig
You should try using PHP's session_set_save_handler() function
http://php.net/manual/en/function.session-set-save-handler.php
Look at the example on this page, it's pretty good.

You pass in user-defined callback functions that get triggered during all the different session phases.  This completely overrides the file-based method of handling sessions which is the default in PHP.

The tricky part will be creating an interface within your java app that allows PHP to get/set session variables.  Maybe you want to use XML/RPC, or straight sockets.  Or maybe you want to use PHP's java integration functions (http://php.net/manual/en/ref.java.php).
It would help if you could let us know how Java handles sessions.
If you change the session like shmert says you could store the information in a database.
Then you should be able to work with that data from your servlets.
Perhaps servlets can also handle custom sessions and thus you can share the same session.
ASKER CERTIFIED SOLUTION
Avatar of shmert
shmert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cas27

ASKER

Thanks shmert and Oliver.

Shmert - I found someone in house who has played around with your suggestions and so far it looks promising.

Oliver - This is what I was going to do, but I needed confirmation first.

Thanks a bunch guys!

Craig