Forum

Thread tagged as: Question, Docs

Admin cookie

Hi,

Is it possible to access the cookie planted when an admin logs in on the live site? I'm running through some ideas to allow the admin to show/hide pages or the entire site during development that would use some basic re-directs in the header but I want to be able to bypass the redirect if the visitor is an admin so they can (obviously) still view their site.

Or would there be an easier way to check if the visitor to the site is an admin?

Haydn Ward

Haydn Ward 4 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You could do this:

$Users = new PerchUsers;
$CurrentUser = $Users->get_current_user();

if (is_object($CurrentUser) && $CurrentUser->logged_in()) {

    // user is logged in to Perch

    if ($CurrentUser->userMasterAdmin()){
        // user is the *primary* admin
    }
}

Cheers Drew :)

I found this particularly useful!

I used it to build a fixed topbar to the content pages while you are logged in as an admin (similar to how other CMSs do), is there any reason that Perch hasn't implemented something like this as default?

Awesome - I've put this into a perch layout.

On the page (e.g. a sitemap with SEO texts for internal use)

     // exit if not logged into backend
     perch_layout('global/adminlogin');

And in said Layout:

<?php
$Users = new PerchUsers;
$CurrentUser = $Users->get_current_user();

if (!(is_object($CurrentUser) && $CurrentUser->logged_in())) {
    // user is logged in to Perch
    exit('Please log in to view this page.');
}
?>

Perfect!

Some clever uses for this! :D Wonder how many other hidden gems there in Perch :P

Drew McLellan

Drew McLellan 2638 points
Perch Support

This is what I do for staging sites. Check if we're not in production mode, and if so, require a login.

if (PERCH_PRODUCTION_MODE < PERCH_PRODUCTION){
 ... require login ...
}