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:

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'];

echo "$language";
If I put a $_POST['languageset']; query at the top to see if it is getting posted through it echos to show. But sticks to the current session only.
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 »

You've called session_start before this code? You've checked that $_SESSION['language'] is not already set? The logic in that bit of code you posted is fine -- redundant $language = $_SESSION['language']; notwithstanding -- so something else is at play here.
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

<?php
session_start(); 

function curHomeURL()
{
 $pagehomeURL = (isset($_SERVER['HTTPS']) && ($_SERVER["HTTPS"] == "on")) ? 'https://' : 'http://';
 if ($_SERVER["SERVER_PORT"] != "80") 
 {
  $pagehomeURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  } 
  else 
 {
  $pagehomeURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pagehomeURL;
}
$urlhome = curHomeURL();

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'];

echo "$language";
This is it right from the top of the template page.
The page I am testing it on is a page where users have to register or login.
The registration page uses nothing that says "language" in the page at all.
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 »

When someone visits the site from a foreign site, that URL needs to post to this page with the language in the URL.

ie. www.website.com/login&lang=es

Then that tells a $_GET to put "es" into the $language session.
While they are on the site, "es" is in that session, unless they change the language at the top, in a <select> dropdown, which they changes the language to de, fr or whatever I show.
The problem is it won't change anyway, right now.
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

$langurl = $_GET['langurl'];
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 if (isset($_GET['langurl'])) {
        // If not, did we get something from a form?
        $language = $_GET['langurl'];
} 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'];
Ignoring my poor _GET get at the top, this is in theory how it should work.
But isn't.
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 »

Bit more info..... if I clear the session at the bottom all the time, I can change the dropdown and it will set everything, but not of course in a session, but at least the _POST is working.

What's happening is that what's stored, is refusing to "change" to a new session.
I did wonder about clearly the session and redoing it, but of course setting the session is time, does exactly that.

It just won't "change" the session... that's the problem.
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 »

Celauran wrote:You've checked that $_SESSION['language'] is not already set?
You've still not answered this. This branch gets evaluated before checking $_POST['languageset']. If $_SESSION['language'] is already set, values set in the form will be ignored.
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 »

Oh so I really ought to be running the _POST and _GET FIRST!!!!! And then that bit.
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 »

What a numb-skull - that was it. Obvious when I look at it now. It's hardly gona reset it, if it's first port of call is "is there a session already? Yes? Fine, leave it at that.

Thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply