Solved

Can PHP and Servlets use the same session variables?

Posted on 2003-11-17
8
353 Views
Last Modified: 2008-03-06
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
Comment
Question by:cas27
8 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9767072
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
 

Author Comment

by:cas27
ID: 9767162
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
 
LVL 11

Expert Comment

by:shmert
ID: 9767279
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
 
LVL 6

Expert Comment

by:aolXFT
ID: 9768215
It would help if you could let us know how Java handles sessions.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 6

Expert Comment

by:DoppyNL
ID: 9769407
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
 
LVL 11

Accepted Solution

by:
shmert earned 250 total points
ID: 9769490
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
 
LVL 4

Assisted Solution

by:Oliver_Dornauf
Oliver_Dornauf earned 250 total points
ID: 9770034
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
 

Author Comment

by:cas27
ID: 9771857
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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now