Can I override a Session if session is empty?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can I override a Session if session is empty?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can I override a Session if session is empty?

Post 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;
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can I override a Session if session is empty?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can I override a Session if session is empty?

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can I override a Session if session is empty?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can I override a Session if session is empty?

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can I override a Session if session is empty?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can I override a Session if session is empty?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can I override a Session if session is empty?

Post by Celauran »

So if the form wasn't submitted and there's nothing in session data?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can I override a Session if session is empty?

Post 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!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can I override a Session if session is empty?

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can I override a Session if session is empty?

Post 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!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can I override a Session if session is empty?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can I override a Session if session is empty?

Post by Celauran »

Have you already set the session elsewhere in your code? That would make it skip the $_POST branch of the conditional.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can I override a Session if session is empty?

Post by simonmlewis »

Nowhere at all. As this is brand new - only just started it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply