Forum Moderators: coopster

Message Too Old, No Replies

Smarty cookie problem

         

alinush

11:04 am on Jun 18, 2005 (gmt 0)

10+ Year Member



Hi,

please help me if you know what could be the problem, I have no idea what is wrong here.

I have this code in the Santy template page:


{php}

if (!isset($_COOKIE["pageterms"])) {

$termscheck = "<script language=\"Javascript\" src=\"popup.js\"> </script>";

// print $termscheck;

print "hello";

}

{/php}

It always prints "hello", even if the cookie is there. If I place the same code in the main page, not the template, it will work, but I must run the javascript in the <body> tag. If I let the code run in the main page, not the template, it will display the $termscheck before or after <html></html>

I hope I was clear in my explanation. Can anybody help me please?

Yours truly,

Alin

mcibor

9:15 pm on Jun 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK. First of all the start of php is <?php and not {php}, and end is?> not {/php}

Then // (as in c and c++) means a comment

So

if (!isset($_COOKIE["pageterms"])) {

You check if the cookie is there.
$termscheck = "<script language=\"Javascript\" src=\"popup.js\"> </script>";

It's there, so you put some text to a variable termscheck
// print $termscheck;

This is just a comment - this line isn't parsed
print "hello";

you print hello to the screen.

As you see this script has to show hello. If you want it to write the javascript then uncomment that line and comment hello like this:

<?php
if (!isset($_COOKIE["pageterms"])) {
$termscheck = "<script language=\"Javascript\" src=\"popup.js\"> </script>";
print $termscheck;
}
?>

And that's all
Best regards and have a good time learning php
Michal Cibor

PS. Welcome to WebmasterWorld!

alinush

5:18 am on Jun 19, 2005 (gmt 0)

10+ Year Member



Hi Michal,

Thanks for answering and your welcome, I can see it's a great forum :)

I'm afraid though that you missunderstood me.

In the Smarty template system, embedding php code in a template page is done with {php} {/php}. This is not a regular php page, it's integrated in Smarty. What Smarty does is compile the pages and caches them, so when the user accesses the page a second time, if nothing was changed, the php will be speared of parsing the code again. I'm new to Smarty, I just have to integrate these few lines, I don't know much about it.

Also, I know I put that line as a comment, I just put "print "hello"" there as a test. The problem I'm having is that the code doesn't read the cookie - it will always print "hello", no matter if the cookie is set or not.

If I put the code in a regular page, it will read the cookie normally, so I'm guessing this is something from Smarty.

I seached the net for embedding php code in Smarty but didn't find anything that refers to handling cookies. It should work as any other php code embedded, but it doesnt.

Thanks again.

Alin

grandpa

8:28 am on Jun 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It always prints "hello", even if the cookie is there.

I'm curious, does the cookie contain a value? If it's set to NULL or an empty string that could be causing isset() to return a FALSE.

alinush

10:17 am on Jun 19, 2005 (gmt 0)

10+ Year Member



yes, the cookie contains a value.

I finally understood why I was having the problem. Smarty caches the parsed php code and only updates the page when there are changes (I think). So when I included the code it only worked for the first time, then nothing.

This saves lots of server resources but it seems it can cause problems to people that don't know how it works - like me :)

I think that's the answer to the problem, I'm currently trying to find a way around this :)

Cheers

PS: Just noticed I posted "Santy" instead of "Smarty" in the thread name. argh.

grandpa

12:17 pm on Jun 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



can cause problems to people that don't know how it works... argh...

Welcome to my world ;) ..and to WebmasterWorld.