tags:

views:

1272

answers:

3

I'm new to drupal. Its a very dumb question I guess. I have enabled PHP filter module so that I can create a new page with my own PHP code in it. This particular page I want to be accessible to only authenticated users, not visible to anonymous users. How can I achieve this? Can I set permissions for individual pages in drupal? Or is it possible to identify that an anonymous user is trying to access the current page through PHP code?

+2  A: 

Since you've mentioned you're new to Drupal, a word of caution - using PHP filter for your custom code is considered bad practice. It's a hack and it's a security problem.

It's much better to write your custom module, and it can implement its own custom permissions, that you can check for, etc. etc. There's a nice sample module that you can check out to see how it should look. Obviously, check out the handbook as well. It's really not as hard as it sounds.

But back to your question. You can put the following line in your PHP-filtered page:

global $user;

This will give you access to an object that represents the current user. $user->roles is an array that represents the user's roles. You can just check if it has authenticated user (or slightly better - a custom role you create).

Eli Krupitsky
Thanks Eli :) Sorry, i'm thanking you quite late. I took your word of caution quite seriously :) Your suggestions and Jordi's code made it very easy! Although I soon replaced it with the "simple access" module. It is really as simple as it states and it matched my requirements perfectly. I'll be using your link for sample module for creating own modules in future. Its a good starter!
Nikhil Patil
+1  A: 

To prevent anonymous users accessing a page you can use the following php code:

<?php
// we have to access the content of the global-scope variable $user
global $user; 

//Checking if the user is registered
if (in_array('authenticated user', $user->roles)==FALSE) {
header('Location:/content/restrictedaccess') ;
}
?>

//Page content

-- More info on how I created my site with Drupal here: http://modeling-languages.com/content/how-i-created-site-drupal

Jordi Cabot
+3  A: 

Having read above answers, you may also consider the readily available simple access module. It suited my requirements and if you're to avoid PHP filtering and don't want to create your own module (for whatever reasons), you can check out simple access.

Nikhil Patil
Thanks! This helped me out.
BigJoe714

related questions