Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

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
0
cas27
Asked:
cas27
2 Solutions
 
lozlozCommented:
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
0
 
cas27Author Commented:
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
0
 
shmertCommented:
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).
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
aolXFTCommented:
It would help if you could let us know how Java handles sessions.
0
 
DoppyNLCommented:
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.
0
 
shmertCommented:
THe problem with using a database is, java is stateful, so the java application would need to continually query the database to get the session vars.  A much slicker interaction would be having PHP directly modify the session instances whenever it is time to save its session data.  I suppose using the java integration functions would be the best way to do this.  Personally, I've utterly failed at getting those working on OS X, however :(
0
 
Oliver_DornaufCommented:
PHP and a servlet _CAN NOT SHARE_ any variables (incl. session variables).
What you can do is to share resources (ie a database table or a plain file).
0
 
cas27Author Commented:
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
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now