Solved

Can PHP and Servlets use the same session variables?

Posted on 2003-11-17
8
358 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

623 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