Page 2 of 2
Re: Can I override a Session if session is empty?
Posted: Mon Jun 02, 2014 8:09 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'];
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.
Re: Can I override a Session if session is empty?
Posted: Mon Jun 02, 2014 8:41 am
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.
Re: Can I override a Session if session is empty?
Posted: Mon Jun 02, 2014 8:55 am
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.
Re: Can I override a Session if session is empty?
Posted: Mon Jun 02, 2014 9:02 am
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.
Re: Can I override a Session if session is empty?
Posted: Mon Jun 02, 2014 9:07 am
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.
Re: Can I override a Session if session is empty?
Posted: Mon Jun 02, 2014 9:19 am
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.
Re: Can I override a Session if session is empty?
Posted: Mon Jun 02, 2014 9:23 am
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.
Re: Can I override a Session if session is empty?
Posted: Mon Jun 02, 2014 9:26 am
by simonmlewis
Oh so I really ought to be running the _POST and _GET FIRST!!!!! And then that bit.
Re: Can I override a Session if session is empty?
Posted: Mon Jun 02, 2014 9:27 am
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.