1
double1
Why can't I read this Xoops Cookie with PHP in a block?
  • 2004/1/24 12:58

  • double1

  • Just popping in

  • Posts: 8

  • Since: 2003/4/26


Hi,

This is slowly driving me nuts, so I hope someone understands this problem.

I'm trying to read the contents of the "newbb_topic_lastread" cookie from the Recent Topics block, but I tried everything and it always return empty, while the cookie *does* exist.

I've added this code to the "newbb_new.php" code (it's in dir newbb/blocks):

$topic_lastread = !empty($HTTP_COOKIE_VARS['newbb_topic_lastread']) ? unserialize($HTTP_COOKIE_VARS['newbb_topic_lastread']) : array();

and

$topic['cookiecontent'] = $topic_lastread[$arr['topic_id']];

so that I can use the topic last read date in the Smarty template (newbb_block_new.html). I have tested other variables in the template and these DO show.

To be sure I defined $HTTP_COOKIE_VARS as Global within the newbb_new.php

The code then looks like this (added lines are in bold typeface):

Quote:
function b_newbb_new_show($options) {
global $HTTP_COOKIE_VARS;

$topic_lastread = !empty($HTTP_COOKIE_VARS['newbb_topic_lastread']) ? unserialize($HTTP_COOKIE_VARS['newbb_topic_lastread']) : array();

$db =& Database::getInstance();
$myts =& MyTextSanitizer::getInstance();
$block = array();
switch($options[2]) {
case 'views':
$order = 't.topic_views';
break;
case 'replies':
$order = 't.topic_replies';
break;
case 'time':
default:
$order = 't.topic_time';
break;
}
$query='SELECT t.topic_id, t.topic_title, t.topic_last_post_id, t.topic_time, t.topic_views, t.topic_replies, t.forum_id, f.forum_name FROM '.$db->prefix('bb_topics').' t, '.$db->prefix('bb_forums').' f WHERE f.forum_id=t.forum_id AND f.forum_type <> 1 ORDER BY '.$order.' DESC';
if (!$result = $db->query($query,$options[0],0)) {
return false;
}
if ( $options[1] != 0 ) {
$block['full_view'] = true;
} else {
$block['full_view'] = false;
}
$block['lang_forum'] = _MB_NEWBB_FORUM;
$block['lang_topic'] = _MB_NEWBB_TOPIC;
$block['lang_replies'] = _MB_NEWBB_RPLS;
$block['lang_views'] = _MB_NEWBB_VIEWS;
$block['lang_lastpost'] = _MB_NEWBB_LPOST;
$block['lang_visitforums'] = _MB_NEWBB_VSTFRMS;
while ($arr = $db->fetchArray($result)) {
$topic['forum_id'] = $arr['forum_id'];
$topic['forum_name'] = $myts->makeTboxData4Show($arr['forum_name']);
$topic['id'] = $arr['topic_id'];
$topic['title'] = $myts->makeTboxData4Show($arr['topic_title']);
$topic['replies'] = $arr['topic_replies'];
$topic['views'] = $arr['topic_views'];
$topic['cookiecontent'] = $topic_lastread[$arr['topic_id']];
$topic['post_id'] = $arr['topic_last_post_id'];
$lastpostername = $db->query("SELECT post_id, uid FROM ".$db->prefix("bb_posts")." WHERE post_id = ".$topic['post_id']);
while ($tmpdb=$db->fetchArray($lastpostername)) {
$tmpuser = XoopsUser::getUnameFromId($tmpdb['uid']);
if ( $options[1] != 0 ) {
$topic['time'] = formatTimestamp($arr['topic_time'],'m')." $tmpuser";
}
}
$block['topics'][] =& $topic;
unset($topic);
}
return $block;
}


Next I added "<{$topic.cookiecontent}>" to the Smarty Template of the Recent Topics Block, but whatever I do, the variable always show up empty!
Now I've used this method with other (global) variables in other blocks and it always worked well, but somehow reading the cookiecontent into a var is different from normal global variables as it just doesn't seem to get read!
I've checked if the cookie is available in my browser cache and it is (with a string of numbers as content), it also works when marking topics read in the newbb "viewtopics" module.

I've tried a number of other options (reordering the lines, defining as global, not defining as global, etc etc, but it's all to no avail)

What am I doing wrong??? It's driving me nuts spending hours trying to get this to work

Now I'm very new to PHP so I sincerely hope someone with experience in PHP can point me to the problem.

Thanks for any help,

Ross

2
Dave_L
Re: Why can't I read this Xoops Cookie with PHP in a block?
  • 2004/1/24 13:36

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


The problem may be that that cookie is set with the path modules/newbb, so it will only be visible to HTTP requests that contain that path.

related topic

3
double1
Re: Why can't I read this Xoops Cookie with PHP in a block?
  • 2004/1/24 16:07

  • double1

  • Just popping in

  • Posts: 8

  • Since: 2003/4/26


Thanks for your reply, that would certainly be a plausible explanation. If I understand you correctly: you mean that only .php files physically in the modules/newbb dir can access the cookie contents through the HTTP requests?

I've searched through the forum and Onokazu commented somewhere that the $bbCookie['path'], which viewtopic.php uses to write the cookie, is indeed modules/newbb.

I tried to test your assumption by moving the newbb_new.php one dir up, so from:
- modules/newbb/blocks/newbb_new.php
to
- modules/newbb/newbb_new.php

I then modified xoops_version.php to reflect the new location of the php file for the block, updated the forums module, tested if it took new variables from the newbb_new.php in the new location, which it did, and then added the cookie code.

Unfortunately it didn't change the situation, the cookiecontent var still doesn't return any value

On the assumption that I understood you correctly and that my testcase reflects that, we can then conclude that this isn't the cause of the problem?

This is really bugging me, hopefully a solution will pop up for this.

Anyway, thanks again for trying to help out.



Quote:
Dave_L wrote:
The problem may be that that cookie is set with the path modules/newbb, so it will only be visible to HTTP requests that contain that path.

related topic

4
Dave_L
Re: Why can't I read this Xoops Cookie with PHP in a block?
  • 2004/1/24 16:50

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


The location of the file doesn't matter. What matters is the path (in the URL) that results in the execution of the code that reads the cookie.

You could try changing the cookie's path to '/'. In modules/newbb/config.php,

change:

$bbCookie['path'] = str_replace(basename($HTTP_SERVER_VARS['PHP_SELF']),"",$HTTP_SERVER_VARS['PHP_SELF']);


to:

$bbCookie['path'] = '/';


I just did a quick test, and that appeared to work. You may want to clear the existing cookies to avoid confusion.

5
double1
Re: Why can't I read this Xoops Cookie with PHP in a block?
  • 2004/2/1 12:36

  • double1

  • Just popping in

  • Posts: 8

  • Since: 2003/4/26


I've been able to test your suggestion and it works perfect. Thank you sooo much for helping me out there! As a PHP newcomer I would have never figured it out otherwise... Luckily with these kind of steps forward my understanding will be improving

Is there any reason you know of why the XOOPS coders haven't set the $bbCookie path to '/' by default?

Thanks again, you made my weekend

Login

Who's Online

240 user(s) are online (152 user(s) are browsing Support Forums)


Members: 0


Guests: 240


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: May 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits