Solved

How to create cookies using Smarty Template Engine?

Posted on 2009-04-03
5
2,682 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
  • 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Button Click 11 25
How do I access property of nested object in PHP? 3 24
Filter wordpress query 3 11
masking php string 4 0
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now