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?
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With