Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can PHP and Servlets use the same session variables?

Posted on 2003-11-17
8
Medium Priority
?
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:aolXFT
ID: 9768215
It would help if you could let us know how Java handles sessions.
0
 
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 1000 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 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

721 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