Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

cache_id not working, probably the flaw on my PHP logic

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
waro
Smarty Regular


Joined: 12 Jun 2008
Posts: 38

PostPosted: Wed Sep 21, 2011 10:35 pm    Post subject: cache_id not working, probably the flaw on my PHP logic Reply with quote

Hi,

I've been setup my site using Smarty cache using different cache_id for different modules, and most of them working fine, but one particular peculiar module/cache_id.

In this module there are 4 different URLs or actions, i.e:
1. ./?mod=blog&cat=1
2. ./?mod=blog&cat=1&page=2 => pagination of URL#1
3. ./?mod=blog&cat=1&year=2005, and
4. ./?mod=blog&cat=1&year=2005&page=2 => pagination of URL#3

The first two were working fine, but not the last two.

Here my snippet to generate cache_id, for that module:
Code:
...
elseif (isset($_GET['mod']) == 'blog' && isset($_GET['cat']))
{
    if (!empty($_GET['year']) && !empty($_GET['page']))
    {
        $my_cache_id = $_GET['mod'].$_GET['cat'].$_GET['year'].$_GET['page'];
    }
    elseif (!empty($_GET['year']))
    {
        $my_cache_id = $_GET['mod'].$_GET['cat'].$_GET['year'];
    }
    elseif (!empty($_GET['page']))
    {
        $my_cache_id = $_GET['mod'].$_GET['cat'].$_GET['page'];
    }
    else
    {
        $my_cache_id = $_GET['mod'].$_GET['cat'];
    }
}
...


From the HTTP headers if I go to URL#3, it would read :
Code:
GET /?mod=blog&cat=1&year=2005 HTTP/1.1


But it would display like URL#1 or display correctly if URL#1's cache not generated the first time. It wass as if it didn't GET 'year'.

Maybe this is not Smarty problem but will someone point out where's the flaw of my code here?

Thanks.
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Wed Sep 21, 2011 10:54 pm    Post subject: Reply with quote

Code:
elseif (isset($_GET['mod']) == 'blog' && isset($_GET['cat']))

isset($_GET['mod') returns a boolean, 'blog' evaluates to the boolean true. see
Code:
$_GET['mod'] = 'foo';
if (isset($_GET['mod']) == 'blog') {
  echo 'but blog is not foo!';
}


---

your cache_id segments are not delimited. you should put | between the elements. To make things a bit simpler, try something like
Code:
$cache_id = array(
  $_GET['mod'],
  $_GET['cat'],
  isset($_GET['year']) ? $_GET['year'] : 'no-year',
  isset($_GET['page']) ? $_GET['page'] : 'no-page',
);
$cache_id = join('|', $cache_id);
var_dump($cache_id);

_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
waro
Smarty Regular


Joined: 12 Jun 2008
Posts: 38

PostPosted: Thu Sep 22, 2011 2:56 am    Post subject: Reply with quote

Thank you, your code make easy in my eyes.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP