Page 1 of 1

handling session variables when the same page is opened

Posted: Mon Oct 05, 2020 12:51 pm
by daybreaker
Hi there,

RE: handling session variables in a page when that same page is open twice or more.

Just wondering what happens when you open the same page that uses the same session variable names?

If it is an issue,that is opening the same page a second time how do you avoid overwriting the session variables in the first instance of the page, by the second page?

I do however use mysql triggers to check the update timestamp so if the same record is opened at the same time you cant overwrite each other when saving the record back to the database.

Re: handling session variables when the same page is opened

Posted: Tue Oct 06, 2020 1:24 am
by simonbrahan
session variables are linked to a user's session - keyed by a cookie set in their browser. If you update it when a page loads, subsequent page loads will update the variable again unless you explicitly guard against it.

For this reason I tend to use session variables solely for identifying a user. Anything else can usually be stored on disk as a db or file entry, or passed around as a query parameter.

Re: handling session variables when the same page is opened

Posted: Tue Oct 06, 2020 4:26 pm
by daybreaker
Thanks for your reply.

so do you think that if there is just one instance of the page allowed to be opened at any time that this will avoid the session variables being overwritten? How would i test for that?

Or alternatively as you say we store the data being updated in a temporary table in mysql? But then how would you keep track of that record? using the users username as the identifier and the name of the table? what about more than one record being updated for the same table?

Re: handling session variables when the same page is opened

Posted: Thu Oct 08, 2020 11:29 pm
by simonbrahan
It's difficult to forbid the same page being opened twice; that happens in the client's browser, which you can't control. What are you trying to do? If you give us more information we can figure out an approach that will work.

Re: handling session variables when the same page is opened

Posted: Fri Oct 09, 2020 5:02 pm
by daybreaker
Hi,

yeah i am looking into code on this url that could do the trick but it isnt working out quite the way i expected. I was under the impression that if the webpage was the same name, it would stop it from opening. But this doesnt appear to be the case. I have tried opening 2 pages of different names and it has identified it as a duplicate window.

URL is ...

https://blog.nitinsawant.com/2018/06/ja ... -open.html

Re: handling session variables when the same page is opened

Posted: Fri Oct 09, 2020 5:08 pm
by daybreaker
yeah im guessing it is not as easy as just having different filenames for the web pages.

what i did to stop overwriting session variables was to use different names for session variables for each page. But this means I still have to stop opening multiple instances of the same page. So i was hoping this solution would stop that from happening by not allowing another instance of the page.

Re: handling session variables when the same page is opened

Posted: Sat Oct 10, 2020 1:10 pm
by daybreaker
I found out from the author of the code that it is not dependent on the filename of the page but the domain name of the url so unless i modify it, it wont suit my purposes. i.e. you cant have more than one page rendered from the same website, not the page itself.

Re: handling session variables when the same page is opened

Posted: Tue Oct 13, 2020 12:59 am
by simonbrahan
That's right; session variables are attached to the user's session on that site. So that URL, if you like. I'm not certain without seeing it, but it sounds like this code isn't using session variables as they're expected to be used. Are you trying to use session variables to choose which page to display in your site?

Re: handling session variables when the same page is opened

Posted: Fri Nov 06, 2020 8:04 pm
by daybreaker
Hi there,

I found some code that was useful - it used a javascript function call to open up a window. The trick that it does is if there is already an instance of that page open, then it opens the page on that page's window. I also made the session variable names different so that coupled with using the same window there is no chance of session variables being overwritten through other pages.

Re: handling session variables when the same page is opened

Posted: Wed Nov 25, 2020 8:44 am
by daybreaker
Hiya , the code that i referred to in my previous posting does not work for every page. Which is kind of annoying - I dont understand why it wont work with every page - if it did it would be the perfect solution .

Can anybody give me some idea why it wont always work. Here is the snippet of code -

<script>
var gmyWin=null;

function myOpenWindow(winURL, winName, winFeatures, winObj)
{
var theWin; // this will hold our opened window

// first check to see if the window already exists
if (winObj != null)
{
if (!winObj.closed)
{
winObj.close();
}
}
theWin = window.open(winURL, winName, winFeatures);
return theWin;
}

</script>

Re: handling session variables when the same page is opened

Posted: Wed Nov 25, 2020 9:01 am
by daybreaker
And here is the invocation of that code

if ($pa[$i]->single_instance_only == 'Y')
{
echo "<a href='javascript:;' onClick=\"javascript:gmyWin=myOpenWindow('".$pa[$i]->permission_file.".php', '".$pa[$i]->permission_file."','',gmyWin);return false\"><b><font color=orange style=\"font-size: 16pt;\" ><span class=\"underline\">". $pa[$i]->permission_name. "</span></font></b></a>&nbsp;&nbsp;";
}

Re: handling session variables when the same page is opened

Posted: Thu Nov 26, 2020 1:03 am
by chorn
what do all the terms "page", "instance" and "open" even mean? you have a sequence of request/responses that incorporate different data like URL, POST-Variables and Cookies that may contain one or more session-IDs that link to one user.

Re: handling session variables when the same page is opened

Posted: Tue Jan 12, 2021 1:26 pm
by daybreaker
Hi there,

I came up with a solution :

I created unique session variables for form data for each page and restricted only a single instance of a page to be opened at any time.

If anybody has a better solution I would like to know.

Thanks
daybreaker