Hello, we have a view/slideshow for our home page. Included in the view is a Global: PHP field that I wrote to randomly pull from a bank of images. The PHP code works well when I am logged in. However, as an anonymous user, the code is not being executed. To test this, I wrote a snippet of code to write to a file on the webserver. As a logged in user, the file is written to, as an anonymous user, the file is not written to.

Does anyone have any ideas? Is there a place that perhaps I just need to enable a permission for anonymous users?

Any help would be greatly appreciated.

Thanks!

-----------
EDIT SOLUTION:
It looks like drupal's caching system was at play here. Since the slideshow is a view, it was being cached. I cleared the cache and a new set of images popped up, and my file was written to. To get around the caching issue, I added the following line to the top of my PHP code which disables caching for the page:

drupal_page_is_cacheable(FALSE);

Comments

shyamsukhamit’s picture

try providing permission to anonymous user for that view, Please revert back if it works...

fender177’s picture

Thanks for the reply - That doesn't seem to make a difference. Anonymous viewers can view the view/slideshow. However, there is a snippet of PHP code that randomizes which image to display. They only see the default set of images. And as I mentioned in my previous post, the PHP code isn't executing for the anonymous user - based on the file I'm writing to.

fender177’s picture

Anyone have any thoughts on this?

goofus’s picture

Hello,
Is your "php code" housed in a Drupal module (I.E. a custom module)?

It's much easier for folks to help you if you post your code :) In your case, it would help if you could specify exactly how you are integrating your code to be run from within Drupal.

Looking forward to your code and answer to how you code is integrated in to Drupal :)

fender177’s picture

Hi there -

The code is within a field, Global: PHP, inside of a view. The code is a little bit convoluted, because of the way the view is setup. The view is reading from a list of images from a couple of different pages (I didn't design this, I'm just implementing PHP to randomize the image selection).

Again, when I'm logged in, the code works as expected (images are randomized and the file in /tmp is written to). However, when I'm not logged in, the code is never executed (the file in /tmp is never written to).

// set node id
$nid = $row->field_ss_slide;

// create an array to store node ids
$nids = array();

// populate node ids
$nids[] = $data->nid;

// open a temp file to test if code is being executed
$fh=fopen('/tmp/anon','a');
fwrite($fh, "works");
fclose($fh);

// get list of image files from the database
$result = db_query("SELECT field_ss_slide_fid FROM {field_data_field_ss_slide} WHERE entity_id = :nid", array(':nid' => $nid));

// loop through the list
foreach($result as $record){
	// grab uri of individual image
	$file = db_query("SELECT uri FROM {file_managed} WHERE fid= :fid", array(':fid' => $record->field_ss_slide_fid ));
	$x=0;
	
        // store uris
	foreach($file as $filename){
		$uri = $filename->uri;
		$uris[$nid][] = $filename->uri;
		
		$x++;
	}

}

$cnt = count($uris[$nid]);
$num = rand(0, $cnt-1);
$uri = $uris[$nid][$num];
$file = file_create_url($uri);

Thanks for any help that you can provide!

goofus’s picture

Hello,
Thank you for posting the code :)

Sorry if this is dumb, but I don't know what you mean by "view"? Are you referring to content type view like "full' versus "teaser"?

Same thing for "field"? Are you referring to a "form field"?, a database field?, a Drupal 7 fields API field?

I guess related, is you code housed in a custom module? Did you write a module and place the above code in it. The reason I am asking is, it may shed some light as to how/or if your code is being called (or has a lack of permission to run). Sorry, does that make sense :)

Finally, you comment says "open a temp file to test if code is being executed". Does that mean you don't need to open a file to perform your solution. What are you testing?? Why do you need to write a file?

Sorry, a lot of questions :(. Maybe someone else can be more helpful. Good luck though :)

You should probably check your server logs and see if there is file permission issue. That may solve the immediate issue, however I just felt remiss if I didn't ask why write a file in the first place :( ?

fender177’s picture

No worries about all of the questions - you're doing me a favor by helping me to trouble shoot.

When I say view, I mean a content view. Similar to the drupal 6 module, views. It is a nice way to display nodes of certain content types. When I say field, I'm referring to a field on the custom content type (used to be called CCK back in drupal 6). In this case, we installed a module called views_PHP, which allows us to place PHP code inside of a view.

That said, the code is within the views_PHP field within the view - not a custom module.

I'm opening the test file to see if the PHP code is being called. For example - if the temp file is not written to, the code is not being executed. If the temp file is written to, the code is being executed. Once I figure out why it is not being executed by anonymous users, I will remove that piece of code.

goofus’s picture

Hello,
Thanks for your patience and great answer :) Now I understand.

Sorry, since I am not familiar with the particular stack of contributed modules, I don't have a specific suggestion. You've probably done this. however I'll suggest it anyway. Each module in your stack (I.E. views.module, the views.module plugin views_php) may have a permission settings (by user role). Perhaps php_views requires the core php module, maybe that role permission must to be set for anon users?

You could also just try commenting out the file statements and re-test. The file write permission could be an issue. You can use the watchdog function if you can't use a debugger. http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/watchdog/7. As an aside, I always use xdebug displayed through an IDE like netbeans or eclipse (all free). That way, you don't have to write "debug statements". That's just a suggestion for the long haul. Watchdog will give the message you need right now (just remember to remove the call when you have a fix, you don't want the overhead of database writes for an extraneous debug statement ;) Another reason I use a debugger, I've seen a lot of modules release with debug statement left in that kills performance.

Finally, if you can't get a resolution through this forum. Perhaps the maintainers of views_php might have some ideas. In other words. post a question if that projects issue queue.

Good Luck :) Again, good explanation :). I'm sure you'll get it working :)

fender177’s picture

I finally figured it out - it was drupal's cache that was causing the issue. I was able to put the following line in and disable the caching for the slideshow.

drupal_page_is_cacheable(FALSE);

Thanks for your help! I've removed my debugging code ;)

goofus’s picture

Hello,
Great work!!

You are welcome. Glad to have helped.

Thanks again for your patient explanations :)

Bertjuh’s picture

Thanks for posting the solution. Helped me out.