Forum

Thread tagged as: Question, Problem, Configuration

Template page reappearing in main navigation

I'm using Perch 2.8.18 to run a production site. There are (or should be) only three top-level items in the navigation menu: Home, About and Work.

Under the Work page I have several Projects, each of which uses the same content template (/perch/templates/pages/project.php) in a Project Details region. Recently, I've seen a new top-level menu item appear: Project. This links to a blank page, but causes an entry in the content admin menu where I can see that the Project Details page region is available to be filled in.

I've tried deleting the Project Details content region and the Project page, but that removes the template from the /perch/templates/pages/ folder on the server - fortunately I have a backup!

I think what's happening is that someone somewhere (possibly a search engine indexer robot?) is directly accessing the /perch/templates/pages/project.php file, which is triggering Perch into adding the Page as a top-level item with a Project Details region waiting to be completed.

Is there a way to deny direct access to this particular page (/perch/templates/pages/project.php) without breaking its functionality within Perch itself?

Mike Armstrong

Mike Armstrong 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Why not hide it from the navigation?

Sorry, I should have mentioned that I already did that (hide it from the main nav)...

I’m just wondering if there’s a better solution, as the install is used by clients who have expressed confusion as to why that page appears on the content editor lists - I don’t want them editing that page and wondering why they don’t see a change in the site, or deleting the page and breaking the project pages elsewhere (under the Work page).

I’ve looked at htaccess solutions, but I suspect it would break the other parts of the site.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Does the page have content regions on it? If so, remove them and the page won't show up in the list.

Hi Drew,

Sorry, it looks like my explanation hasn't been clear. I've tried deleting the region - the Project page still shows up in the list. If I delete the Project page as well then it deletes the template from the server (which breaks the 'Work' area of the site, as each Project within the 'Work' area uses this template). Just deleting the region doesn't solve the issue, as the next time the template page gets accessed directly then the region shows up in the list again.

It seems that a browser (or robot) is reaching the template page (https://domain.com/perch/templates/pages/project.php) directly which is adding the Project page and the Project Details region to the main list.

I'm trying to figure out if there's a way I can deflect external traffic from reaching this template page without affecting Perch's ability to use that template to format content elsewhere on the site.

Drew McLellan

Drew McLellan 2638 points
Perch Support

By deleting the region, I mean removing it from the file. That is if it's not needed. If you need the region then then page will display in the list.

I experienced a similar issue recently. It turned out if I 'previewed' the template (.php) page by opening it in a browser, Perch saw the regions and created a page in the site navigation (as it's designed to do). Deleting that instance from the site nav indeed deleted the template from the pages directory.

I now avoid previewing template pages directly, and only check updates through pages using said template.

Are you previewing the template page?

Tony Astley said: Are you previewing the template page?

I'm not doing it myself, but someone is, whether they realise it or not. I'm still investigating possible .htaccess solutions to stop direct access via a browser or search engine crawl, but allow Perch to use the template. I'll update this thread if I manage to get it working.

Maybe Drew and the team could put a failsafe in place to prevent template files creating instances when previewed in a future update?

I remember I lost an whole afternoon trying to work out what was happening.

Following the advice here: https://stackoverflow.com/questions/409496/prevent-direct-access-to-a-php-include-file

Adding the following code block to the top of the template before the Perch include stops the page from being accessed directly (and thus added to the Pages list) and redirects the person who's trying to reach it to the main 'Work' area of the site:

if(count(get_included_files()) == ((version_compare(PHP_VERSION, '5.0.0', '>='))?1:0)){
    // redirect to main Work page if someone tries to access this template directly
    header('location:../../../Work/index.php');
    exit;
}

(The directory traversal takes the user out of the 'pages' directory, out of the 'templates' directory, out of the 'Perch' directory, and into the 'Work' directory. Your usage may vary.)

Cool, will give it a try.