PDA

View Full Version : Session Problem


qenq
10-28-2007, 01:17 PM
Hi, i installed the calender in my existing programm which works with sessions too. When i login as an admin, my sessions are disturbed by the calender. When i am logged out, my programm sees that there was an session_destroy() and quits working.

So my question is, are there users here who have had the same problems, and how did they fix it, or is there someone else to help me out.

Thanks in advantage

Cheers
QenQ

Brian
10-29-2007, 02:12 AM
You may try having the calendar admin open in a new window.

qenq
10-29-2007, 02:54 AM
Yes, but if i do so, it is just the same, nothing changes if you open the calendar from an exisiting windon into a new window, the session_id will be the same, an if your calendar destroys it, my session_id is gone to.

On php.net i found this

Sessions and browser's tabs

May you have noticed when you open your website in two or more tabs in Firefox, Opera, IE 7.0 or use 'Control+N' in IE 6.0 to open a new window, it is using the same cookie or is passing the same session id, so the another tab is just a copy of the previous tab. What you do in one will affect the another and vice-versa. Even if you open Firefox again, it will use the same cookie of the previous session. But that is not what you need mostly of time, specially when you want to copy information from one place to another in your web application. This occurs because the default session name is "PHPSESSID" and all tabs will use it. There is a workaround and it rely only on changing the session's name.

Put these lines in the top of your main script (the script that call the subscripts) or on top of each script you have:

if(version_compare(phpversion(),'4.3.0')>=0) {
if(!ereg('^SESS[0-9]+$',$_REQUEST['SESSION_NAME'])) {
$_REQUEST['SESSION_NAME']='SESS'.uniqid('');
}
output_add_rewrite_var('SESSION_NAME',$_REQUEST['SESSION_NAME']);
session_name($_REQUEST['SESSION_NAME']);
}

How it works:

First we compare if the PHP version is at least 4.3.0 (the function output_add_rewrite_var() is not available before this release).

After we check if the SESSION_NAME element in $_REQUEST array is a valid string in the format "SESSIONxxxxx", where xxxxx is an unique id, generated by the script. If SESSION_NAME is not valid (ie. not set yet), we set a value to it.

uniqid('') will generate an unique id for a new session name. It don't need to be too strong like uniqid(rand(),TRUE), because all security rely in the session id, not in the session name. We only need here a different id for each session we open. Even getmypid() is enough to be used for this, but I don't know if this may post a treat to the web server. I don't think so.

output_add_rewrite_var() will add automatically a pair of 'SESSION_NAME=SESSxxxxx' to each link and web form in your website. But to work properly, you will need to add it manually to any header('location') and Javascript code you have, like this:

header('location: script.php?'.session_name().'='.session_id()
. '&SESSION_NAME='.session_name());

<input type="image" src="button.gif" onClick="javascript:open_popup('script.php?<?php
echo session_name(); ?>=<?php echo session_id(); ?>&SESSION_NAME=<?php echo session_name(); ?>')" />

The last function, session_name() will define the name of the actual session that the script will use.

So, every link, form, header() and Javascript code will forward the SESSION_NAME value to the next script and it will know which is the session it must use. If none is given, it will generate a new one (and so, create a new session to a new tab).

May you are asking why not use a cookie to pass the SESSION_NAME along with the session id instead. Well, the problem with cookie is that all tabs will share the same cookie to do it, and the sessions will mix anyway. Cookies will work partially if you set them in different paths and each cookie will be available in their own directories. But this will not make sessions in each tab completly separated from each other. Passing the session name through URL via GET and POST is the best way, I think.I did not yet try it out, i thought maybe you did notice this problem already.

Cheers
QenQ

Brian
10-29-2007, 10:01 PM
The Admin Managers weren't designed to be used along with another PHP session. I have had this asked once before that I remember, and they worked around it. However, it's been some time ago and I don't remember what they did.

The only think I can think (which I don't event know if it's possible) is use a separate sessions paths (set in the setup/sessionsPath.php file) for the calendar.