Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2006-10-06 10:18:03

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

how could i set & read a session ID

hello,
i couldn’t find any thread on using php session ids with textpattern yet and fiddled with some scripting myself the last days, but i can’t get it to work properly (in r1866).

doesn anyone know of a proper implementation of this into textpattern or could point me in the right direction?

thanx very much in advance!


A hole turned upside down is a dome, when there’s also gravity.

Offline

#2 2006-10-06 13:56:32

Walker
Plugin Author
From: Boston, MA
Registered: 2004-02-24
Posts: 592
Website

Re: how could i set & read a session ID

Read this thread, it should help you out.

Offline

#3 2006-10-06 14:25:47

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

Re: how could i set & read a session ID

thanx much, walker,

only i need to store things independently from the use of cookies (i belong to the ones automatically having cookies deleted, when browswer quits for one thing) – and i think i meanwhile figured something out.
will post code here in a little while, <small>though
1. it is dedicated to a user decision about the language of articles they’d like to view, this option will also be storable in a cookie for the ones who like to use cookies (not that i generally dislike it…) /
2. i need to store more than just one value because i am trying to build myself a shop into textpattern aswell..</small>

thanx much, though!

Last edited by jayrope (2006-10-06 14:47:46)


A hole turned upside down is a dome, when there’s also gravity.

Offline

#4 2006-10-06 21:38:55

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

Re: how could i set & read a session ID

Here’s the promised code snippets.
I am using a php session id to store a user’s language preference.
At first i analyze the browser language with a borrowed script in a form, that returns a
variable $browserLang.
At the intitial start this is the browser language, which can later be manually set differently by the user with a drop down menu in a asidebar.

In a form “php_session” i have:
<code>
<txp:php>
session_start();
global $browserLang;
global $myLang;
if (!isset($myLang)) {
$myLang = $browserLang;
$_SESSION[‘myLang’] = $myLang;
}
if (isset($_POST[‘myLang’])) {
$myLang = $_POST[‘myLang’];
$_SESSION[‘myLang’] = $myLang;
}
</txp:php>
</code>

This writes the currently used language to the session.
Anytime the page is being called again, for instance by a form like:
<code>
<form action=”“ method=“post”>
<select class=“button” name=“myLang”>
<option value=“de”> Deutsch </option>
<option value=“en”> English </option>
<option value=“all”> MultiLang 8-) </option>
</select>
<input type=“submit” class=“button” value=“send off” />
</form>
</code>

the user preferred language is reset accordingly to display articles of a category containing a language, with this code above the head of the page
<code>
<txp:output_form form=“php_sessionj” />
</code>
and this code further in the body/content part of the page
<code>
<txp:php>
if ($_SESSION[‘myLang’] 'en') { echo '<txp:article_custom limit="2" category="English" sort="Posted desc" />'; } if ($_SESSION['myLang'] ‘de’) {
echo ‘<txp:article_custom limit=“2” category=“Deutsch” sort=“Posted desc” />’;
}
else
echo ‘
<txp:recent_articles limit=“10” category=“Deutsch” sort=“Posted desc” label=“Deutsch” labeltag=“h3” />
<br /><br />
<txp:recent_articles limit=“10” category=“English” sort=“Posted desc” label=“English” labeltag=“h3” />’;
</txp:php>
</code>
In advance of all this i had written some articles which are set to either “Deutsch” or English” in category2, erm of course,).

Please note, that the sorting within the article tags <code>sort=“Posted desc”</code> is already 4.0.4 compliant (unless this is gonna be changed again before the official release of it, using r1866 at the moment).

I didn’t yet work on making the user decision on the preferred language to be lasting longer, than until the browser quits, which should under normal circumstances kill the session ID and therefore also loose the user preference before his next visit. The preference could be stored in a cookie, but i didn’t implement this yet.

I hope this helps anyone and hell, yeah: improvements are very welcome!

Like for instance:
. cookie implementation
. a form, which selects on select instead of pressing the “send off” button or hitting enter… not to confuse a search option on the same page…

Toooot!

Last edited by jayrope (2006-10-06 21:45:07)


A hole turned upside down is a dome, when there’s also gravity.

Offline

#5 2006-10-07 10:04:07

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,536
Website GitHub Twitter

Re: how could i set & read a session ID

Thanks for sharing your code jayrope It s useful for me.

Offline

Board footer

Powered by FluxBB