Solved

How to create cookies using Smarty Template Engine?

Posted on 2009-04-03
5
2,868 Views
Last Modified: 2013-11-10
Hi,

I have been working with PHP for a decent amount of time, but I now have to work with Smarty at my office. What I am trying to do is create a cookie when someone first comes to the site and then use that variable to display a phone number throughout the site for their entire visit.

I want to use a cookie so that if they close the window and come back later, I can still display the correct phone number targeted for them.

Unfortunately, I don't know anything about Smarty and can't figure out how to create a cookie or access it either. Should I create the cookie using PHP? Can Smarty access cookie data? Can someone help me?

Thanks!


0
Comment
Question by:aseem1234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:prokvk
ID: 24062547
Smarty is a template engine which allows to separate application logic from the output data. Due to this fact you can do really just a few basic operations with Smarty, for anything more complex Smarty allows you to use {php}{/php} tags to implement your logic. So, to your question - COOKIEs via Smarty - definately not. Use pure PHP ;)
0
 

Author Comment

by:aseem1234
ID: 24062569
AHHHH So I can use PHP in my Smarty templates by using these {php}{/php} tags? Can Smarty tags be nested inside the PHP tags? And also vice versa?

Thanks!
0
 

Author Comment

by:aseem1234
ID: 24063687
Also, is there any way to set a cookie in PHP and retrieve it on the same page if the cookie never existed before? I am trying to set a new cookie and have it displayed when a page loads, but it shows up as blank. I have to refresh the page and then the value shows up.

Thanks
0
 
LVL 5

Accepted Solution

by:
prokvk earned 300 total points
ID: 24064981
I'm going to react on your first response. No, you cant use each other tags as you described .. {php}{/php} closure tells smarty to leave this block intact and treat it as a pure php ...

Your second response .. read up on COOKIE manuals, you'll find tons of materials about them over the internet, just google .. ;)
Using COOKIEs is as easy as setting them via setcookie PHP function and later on you can work with them via superglobals $_COOKIE variable ...
0
 
LVL 14

Assisted Solution

by:shobinsun
shobinsun earned 200 total points
ID: 24066190
Hello,

You can use sessions with smarty.

{* display the php session variable "id" ($_SESSION['id']) *}
{$smarty.session.id}

For more about use the following link:

http://cirofeitosa.com/post/smarty-php-always-together

Regards.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question