handling session variables when the same page is opened

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
daybreaker
New php-forum User
New php-forum User
Posts: 17
Joined: Wed Aug 26, 2020 9:46 pm

Mon Oct 05, 2020 12:51 pm

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.
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 65
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Tue Oct 06, 2020 1:24 am

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.
daybreaker
New php-forum User
New php-forum User
Posts: 17
Joined: Wed Aug 26, 2020 9:46 pm

Tue Oct 06, 2020 4:26 pm

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?
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 65
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Thu Oct 08, 2020 11:29 pm

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.
daybreaker
New php-forum User
New php-forum User
Posts: 17
Joined: Wed Aug 26, 2020 9:46 pm

Fri Oct 09, 2020 5:02 pm

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
daybreaker
New php-forum User
New php-forum User
Posts: 17
Joined: Wed Aug 26, 2020 9:46 pm

Fri Oct 09, 2020 5:08 pm

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.
daybreaker
New php-forum User
New php-forum User
Posts: 17
Joined: Wed Aug 26, 2020 9:46 pm

Sat Oct 10, 2020 1:10 pm

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.
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 65
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Tue Oct 13, 2020 12:59 am

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?
daybreaker
New php-forum User
New php-forum User
Posts: 17
Joined: Wed Aug 26, 2020 9:46 pm

Fri Nov 06, 2020 8:04 pm

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.
daybreaker
New php-forum User
New php-forum User
Posts: 17
Joined: Wed Aug 26, 2020 9:46 pm

Wed Nov 25, 2020 8:44 am

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>
daybreaker
New php-forum User
New php-forum User
Posts: 17
Joined: Wed Aug 26, 2020 9:46 pm

Wed Nov 25, 2020 9:01 am

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;";
}
chorn
php-forum GURU
php-forum GURU
Posts: 688
Joined: Fri Apr 01, 2016 2:18 am

Thu Nov 26, 2020 1:03 am

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.
daybreaker
New php-forum User
New php-forum User
Posts: 17
Joined: Wed Aug 26, 2020 9:46 pm

Tue Jan 12, 2021 1:26 pm

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