Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Matthias on May 16, 2019, 07:01:22 AM

Title: Undefined index: action error
Post by: Matthias on May 16, 2019, 07:01:22 AM
Used SMF Version 2.0.15

I have added a custom action "welcome" an set it as homepage.

index.php

// Action and board are both empty... BoardIndex!
if (empty($board) && empty($topic))
{
require_once($sourcedir . '/Welcome.php');
return 'WelcomeMain';
}


For the linktree I did the following:
Sources/Load.php

// Start the linktree off empty..
if (!empty($board) OR !empty($topic) OR $_REQUEST['action'] == 'forum' OR $_REQUEST['action'] == 'collapse' OR $_REQUEST['action'] == 'unread' OR $_REQUEST['action'] == 'recent')
$context['linktree'] = array(array('url' => $scripturl . '?action=forum', 'name' => isset($txt['forum_name']) ? $txt['forum_name'] : 'Community'));
else
$context['linktree'] = array();


Everything works as it should, but when I call the homepage (domain.tld or domain.tld/index.php) I get the following error message:

https://www.domain.tld/index.php?https://www.domain.tld/
8: Undefined index: action
File: /var/www/vhosts/domain.tld/httpdocs/Sources/Load.php
Line: 607


Line 607 is that, what I have changed

if (!empty($board) OR !empty($topic) OR $_REQUEST['action'] == 'forum' OR $_REQUEST['action'] == 'collapse' OR $_REQUEST['action'] == 'unread' OR $_REQUEST['action'] == 'recent')


I think the error comes up, because at domain.tld or domain.tld/index.php there is nothing attached, e.g.
domain.tld/index.php?board=1.0
domain.tld/index.php?action=something

Does anyone have any idea how to fix the error?
Title: Re: Undefined index: action error
Post by: SychO on May 16, 2019, 07:06:43 AM
check if $_REQUEST['action'] is set before checking its value
Title: Re: Undefined index: action error
Post by: Matthias on May 16, 2019, 07:51:16 AM
Do you have an example?
Title: Re: Undefined index: action error
Post by: SychO on May 16, 2019, 08:07:40 AM
instead of directly checking if: $_REQUEST['action']=='something'
check that it is set first: isset($_REQUEST['action']) && $_REQUEST['action']=='something'
Title: Re: Undefined index: action error
Post by: Matthias on May 16, 2019, 09:23:55 AM
I have tried this and get a white page

if isset($_REQUEST['action']) && ($_REQUEST['action'] == 'forum' OR $_REQUEST['action'] == 'collapse' OR $_REQUEST['action'] == 'unread' OR $_REQUEST['action'] == 'recent')
$context['linktree'] = array(array('url' => $scripturl . '?action=forum', 'name' => isset($txt['forum_name']) ? $txt['forum_name'] : 'Community'));
else
$context['linktree'] = array();


What could be wrong?
Title: Re: Undefined index: action error
Post by: SychO on May 16, 2019, 09:32:44 AM
conditions have to be between parenthesis: "if (conditions)" not "if conditions"
Title: Re: Undefined index: action error
Post by: Kindred on May 16, 2019, 10:11:29 AM
incidentally, with that logic, the welcome page will NOT show on index.php (with no argument)

if that is your intention, then OK...   but, from your description, it seemed like you want it to show on the base index page as well as recent and unread.
Title: Re: Undefined index: action error
Post by: Matthias on May 16, 2019, 10:21:24 AM
Thats how it works

// Start the linktree off empty..
if ((!empty($board) OR !empty($topic) OR (isset($_REQUEST['action']) && $_REQUEST['action'] == 'forum' OR $_REQUEST['action'] == 'collapse' OR $_REQUEST['action'] == 'unread' OR $_REQUEST['action'] == 'recent')))
$context['linktree'] = array(array('url' => $scripturl . '?action=forum', 'name' => isset($txt['forum_name']) ? $txt['forum_name'] : 'Community'));
else
$context['linktree'] = array();
Title: Re: Undefined index: action error
Post by: Arantor on May 16, 2019, 10:25:19 AM
I'd have just installed a portal to do this for me, personally, I don't feel the need to write *everything* from scratch ;)
Title: Re: Undefined index: action error
Post by: Matthias on May 16, 2019, 02:27:27 PM
Quote from: Kindred on May 16, 2019, 10:11:29 AM
incidentally, with that logic, the welcome page will NOT show on index.php (with no argument)

What do you mean exactly?
When I call domain.tld or domain.tld/index.php there are shown the action=welcome page.
Title: Re: Undefined index: action error
Post by: Matthias on May 16, 2019, 02:38:30 PM
Quote from: Arantor on May 16, 2019, 10:25:19 AM
I'd have just installed a portal to do this for me, personally, I don't feel the need to write *everything* from scratch ;)

Ok, but I'm comming up from YaBBSE with my active Sites.
They are still running SMF 1.1.21. It was easier then to use custom actions. Now I'm going to adjust that for 2.0.x