Page 1 of 1

How to set and read a session variable?

Posted: Fri Aug 03, 2012 12:35 pm
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

Re: How to set and read a session variable?

Posted: Sun Aug 05, 2012 11:01 am
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

Re: How to set and read a session variable?

Posted: Sun Aug 05, 2012 7:41 pm
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.

Re: How to set and read a session variable?

Posted: Mon Aug 06, 2012 4:00 am
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;