Page 1 of 2
Can I override a Session if session is empty?
Posted: Thu May 29, 2014 10:19 am
by simonmlewis
We are updating a web site where the support guys will Sessions to know what web site they are looking after.
At the moment, they are not using Sessions, and as such, no sessions are on their systems.
So when I make this new system live, all their links to say "we are working on this site", will be zero.
So if there is no session, how do I enable it to BE a session for a particular word?
Code: Select all
if (isset($_SESSION['website']))
{
$website = $_SESSION['website'];
}
$websitesession = isset($_POST['websitesession']) ? $_POST['websitesession'] : null;
if (isset($_POST['websitesession']))
{
$_SESSION['website'] = $_POST['websitesession'];
}
else if (isset($_SESSION['website']))
{
$website = $_SESSION['website'];
}
elseif ($website = NULL) { $_SESSION['website'] = "websitename";}
This looks very complicated and probably completely wrong.
Re: Can I override a Session if session is empty?
Posted: Thu May 29, 2014 10:39 am
by Celauran
$_SESSION is just an array. Treat it the same way you would any other array. Are you looking to do something like this?
Code: Select all
session_start();
if (isset($_SESSION['website'])) {
// If there's already something saved in sessions, let's use that
$website = $_SESSION['website'];
} else if (isset($_POST['website'])) {
// If not, did we get something from a form?
$website = $_POST['website'];
} else {
// No? Default it is, then
$website = 'default.co.uk';
}
// Now make sure we're tracking it in session data going forward
$_SESSION['website'] = $website;
Re: Can I override a Session if session is empty?
Posted: Thu May 29, 2014 10:44 am
by simonmlewis
Brilliant. I tried and tried and my script got longer and longer. lol.
BTW - the other thread about finding the last row only if a Customer - I took your advice and went down the PHP route, following a query. Works a treat.
Means for that one type of page (pending tickets), I have to do a copy of the whole code, but it's not that long anyway.
Re: Can I override a Session if session is empty?
Posted: Thu May 29, 2014 10:58 am
by Celauran
simonmlewis wrote:Means for that one type of page (pending tickets), I have to do a copy of the whole code, but it's not that long anyway.
You shouldn't have to be copy/pasting anything but the most trivial code. If you're using the same logic elsewhere in your app, abstract it away into a function/class.
DRY principle.
Re: Can I override a Session if session is empty?
Posted: Fri May 30, 2014 7:16 am
by simonmlewis
This isn't actually working.
Code: Select all
if (isset($_SESSION['website'])) {
// If there's already something saved in sessions, let's use that
$website = $_SESSION['website'];
} else if (isset($_POST['websitesession'])) {
// If not, did we get something from a form?
$website = $_POST['websitesession'];
}
// Now make sure we're tracking it in session data going forward
$_SESSION['website'] = $website;
$website = $_SESSION['website'];;
If I then echo "$website", it's showing nothing.
The form to submit the website is <select name='websitesession'>.
The Session is called $website.
And while it is showing the web site correctly for users, as it has stored the web site name in a login script, the Change from one to another isn't working, and it's now showing $website when echoed.
Re: Can I override a Session if session is empty?
Posted: Fri May 30, 2014 7:27 am
by Celauran
It's not working because you have removed the third (default) branch of the conditional. Logic was as follows: first check sessions, then check post, then resort to default. You've changed it to: first check sessions, then check post, then leave undefined. That's why it's not working.
Also:
Code: Select all
$_SESSION['website'] = $website;
$website = $_SESSION['website'];
What's going on here? This is basically the same as $website = $website.
Re: Can I override a Session if session is empty?
Posted: Fri May 30, 2014 7:30 am
by simonmlewis
Code: Select all
if (isset($_SESSION['website'])) {
// If there's already something saved in sessions, let's use that
$website = $_SESSION['website'];
} else if (isset($_POST['websitesession'])) {
// If not, did we get something from a form?
$_SESSION['website'] = $_POST['websitesession'];
$website = $_SESSION['website'];
}
// Now make sure we're tracking it in session data going forward
$website = $_SESSION['website'];
This isn't wokring either.
Re: Can I override a Session if session is empty?
Posted: Fri May 30, 2014 8:14 am
by simonmlewis
To be clear, I don't need it to override it now.
I just need to capture the data from websitesession form, if it is submitted, else, just the $website session that was stored when they logged in.
Re: Can I override a Session if session is empty?
Posted: Fri May 30, 2014 8:35 am
by Celauran
So if the form wasn't submitted and there's nothing in session data?
Re: Can I override a Session if session is empty?
Posted: Fri May 30, 2014 8:40 am
by simonmlewis
When the user logs in, a session for $website is created with the default domain.
But they change it from a <select> dropdown, which then runs thru this query we are looking at, to change that session for the other domain they have chosen from <select>.
Trouble is, it's not changing anything. The session is remaining at the default set one.
So if you logged in and the $website session was "celauran", doesn't matter if you chose "fred" from the dropdown, it would stay at "celauran".
This is obviously wrong!
Re: Can I override a Session if session is empty?
Posted: Fri May 30, 2014 8:46 am
by Celauran
Based on that description, it sounds like you'd only need
Code: Select all
<?php
session_start();
if (isset($_POST['website'])) {
$_SESSION['website'] = $_POST['website'];
}
You could add $website = $_SESSION['website'] after that if your comparisons are already against $website. Otherwise, that seems unnecessary.
Re: Can I override a Session if session is empty?
Posted: Fri May 30, 2014 9:36 am
by simonmlewis
That's got it. Simple when you know how.
Had one of those days when my brain is scrambled., and the simpler code just isn't there in my head!!
Re: Can I override a Session if session is empty?
Posted: Mon Jun 02, 2014 5:51 am
by simonmlewis
Code: Select all
if (isset($_SESSION['language'])) {
// If there's already something saved in sessions, let's use that
$language = $_SESSION['language'];
} else if (isset($_POST['languageset'])) {
// If not, did we get something from a form?
$language = $_POST['languageset'];
} else {
// No? Default it is, then
$language = 'uk';
}
// Now make sure we're tracking it in session data going forward
$_SESSION['language'] = $language;
$language = $_SESSION['language'];
I'm now trying to get languages working.
It's works in the same way as that other script, just popping it into a session.
This keeps defaulting it back to "uk", even if "es" or "de" is POSTed.
If I clear the SESSION at the bottom of the template, it works, but obviously keeps losing the session, but what is posted, remains in the session.
Re: Can I override a Session if session is empty?
Posted: Mon Jun 02, 2014 6:47 am
by Celauran
Have you already set the session elsewhere in your code? That would make it skip the $_POST branch of the conditional.
Re: Can I override a Session if session is empty?
Posted: Mon Jun 02, 2014 7:13 am
by simonmlewis
Nowhere at all. As this is brand new - only just started it.