How to set and read a session variable?

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
Andrew Prior
Forum Members
Forum Members
Posts: 248
Joined: Sun Oct 28, 2007 4:14 am

How to set and read a session variable?

Post by Andrew Prior »

I have the following template working:

Code: Select all

{content assign='freddy'}
{mobile}{capture assign='mobiletemp'}{$mobile_detect}{/capture} {* UDT from Goran Ilic *}
{if $mobiletemp}
{cms_stylesheet name='mobile'}
{$freddy}
{else}
{cms_stylesheet name='full'}
{various menus and columns etc}
{$freddy}
{/if}
You can see this at http://onemansweb.org/mobs

What I would like to do is to add a link or button in the content block I have assigned as 'freddy' which would assign a session variable I would call 'forcefullsite'. Then I could use the following logic.

Code: Select all

{content assign='freddy' NOW INCLUDES THE BUTTON}

{[mock code] if session variable equals forcefullsite [/mockcode]}
    {cms_stylesheet name='full'}
    {various menus and columns etc}
    {$freddy}
{else}
    {mobile}{capture assign='mobiletemp'}{$mobile_detect}     {/capture} {* UDT from Goran Ilic *}
    {if $mobiletemp}
    {cms_stylesheet name='mobile'}
    {$freddy}
{else}
    {cms_stylesheet name='full'}
    {various menus and columns etc}
    {$freddy}
{/if}
This would allow mobile users to force the full site layout. I am retrofitting a couple of very large websites to allow a better mobile experience and this logic would minimise my rewriting task.

Question: Can I set a variable as I propose? Can anyone give me the three or four lines of code I would need? I am already way outside my limited skill level :-(

Many thanks
Andrew Prior
applejack
Power Poster
Power Poster
Posts: 1014
Joined: Fri Mar 30, 2007 2:28 am

Re: How to set and read a session variable?

Post by applejack »

You can either just use a UDT

Code: Select all

setcookie('name_of_cookie','value_of_cookie);
$getcookie = $_COOKIE["name_of_cookie"];
if ($getcookie) {
$smarty->assign('cookieexists', "$getcookie");
}

You can then use the Smarty variable cookieexists in your template
Or use the module CGSimpleSmarty
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: How to set and read a session variable?

Post by calguy1000 »

To read session/cookie values

Code: Select all

{$smarty.session.name_of_var}
or

Code: Select all

{$smarty.cookie.name_of_cookie}
to create/write session vars you need CGSimpleSmarty or a UDT
to create/write a cookie you will need a UDT.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Andrew Prior
Forum Members
Forum Members
Posts: 248
Joined: Sun Oct 28, 2007 4:14 am

Re: How to set and read a session variable?

Post by Andrew Prior »

Thankyou both.
I have worked out the syntax to set a session variable, and to read it. I will use

Code: Select all

{session_put var='readerstate' value='forcefull'}
I have also a link in the mobile landing part of the template that will reload the page if clicked. This uses

Code: Select all

<p><a href="{mypage}">Use the full site</a> 
where mypage is a udt from the Tips and Tricks, which I have put at the bottom of this post.

This means that a mobile browser will be sent to a version of the page which also has the link Use the Full Site (using the code from my first post). That link will reload the page if it is clicked.

What I am unable to figure out is this: How do I get this code:

Code: Select all

{session_put var='readerstate' value='forcefull'}
to fire only as the page reloads from the link. If I can do this, I can then test for the presence of readerstate = forcfull.. or not as the page reloads.

Any ideas?


udt to get page URL

Code: Select all

/////// real url
global $gCms;
$urlBASE = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
if (!empty($_SERVER["QUERY_STRING"]))
$urlBASE .= "?".$_SERVER['QUERY_STRING'];
if (isset($gCms->config['assume_mod_rewrite']) && ($gCms->config['assume_mod_rewrite']==true)) {
$urls = str_replace("index.php?page=", "", $urlBASE);
$urls = str_replace("index.php", "", $urls);
}else{
$urls = $urlBASE;
}
 echo $urls;
Post Reply

Return to “The Lounge”