Solved

How to properly add PHP code to blocks in Drupal 7

Posted on 2013-01-21
24
1,326 Views
Last Modified: 2013-01-22
<?php
$inittime=time();
$dateatota=strtotime('2013-04-29 18:00:00');
$timediff = $dateatota - $inittime; $days=intval($timediff/86400);$remaining=$timediff%86400;
echo $days;?>

Open in new window

Hello.
I am trying to add the attached code to a block in D7.
It isn't working at all. I get the following error:

Parse error: syntax error, unexpected '>' in /home/username/public_html/drupal/modules/php/php.module(80) : eval()'d code on line 1

I have php filtering module enabled. I have the input type on the block set as 'PHP Code'.

This php seems to be working fine on it's own, it does not work when embedded in Drupal. I can get it to work in an IFrame in a block, but I'd sooner not do that.

Any idea on how I can fix it? I haver very little PHP knowledge.

thank you
0
Comment
Question by:itkadmin
  • 8
  • 5
  • 5
  • +3
24 Comments
 
LVL 16

Expert Comment

by:HagayMandel
Comment Utility
Try removing the php closing tag.
If not, remove the opening one as well.
0
 
LVL 2

Author Comment

by:itkadmin
Comment Utility
That didn't work.

Removing the closing tag didn't change anything, still got the error. Removing the opening tag renders the code as plain text.

the instructions for PHP text format type say:
You may post PHP code. You should include <?php ?> tags.

I've used PHP in blocks before, mostly for linking things relative to the front page and I didn't have issues. Used opening and closing tags there.
0
 
LVL 16

Expert Comment

by:HagayMandel
Comment Utility
I know about the php insertion instructions. In early versions, php tags were not allowed, so this was a shot in the dark.

The only thing I can think of is to check in php.module (modules/php/php.module) file, whether line #80 is the following:
print eval('?>' . $code);

Open in new window

0
 
LVL 2

Author Comment

by:itkadmin
Comment Utility
yes it is. I dont know what it means though
0
 
LVL 13

Expert Comment

by:Arrow_1
Comment Utility
Is it processing code at all? Try something like this to see if it works:

<?php print 'test'; ?>
0
 
LVL 12

Expert Comment

by:junipllc
Comment Utility
Is the Input Format set to PHP? I'm sure it is, but it's good to check just in case something got wacky during your debugging.

Does the user in question have permissions set for PHP (if user 1/admin then this doesn't apply)?

The other way to do this is at the theme level is to use a .tpl.php file based on block.tpl.php. That way you can just use the code in a separate file that's not part of the database itself, and it gives you a lot more flexibility...including version controllability. If you need people to be able to change the PHP code in the interface, though, this won't work. And in .tpl.php files you do not put the closing ?> tag.

Just some random thoughts off the top of my head
0
 
LVL 2

Author Comment

by:itkadmin
Comment Utility
<?php print 'test'; ?>
works fine

user is 1/admin
0
 
LVL 12

Expert Comment

by:junipllc
Comment Utility
When you are entering the PHP, are you using a WYSIWYG editor, or just a straight text box? Or the View Source option of a WYSIWYG editor? Could be some sort of WYSIWYG ickiness...I see some of that with CKEditor a lot.
0
 
LVL 13

Expert Comment

by:Arrow_1
Comment Utility
You might be pasting in some funky invisible character or something that's breaking the code evaluation. Try copy/pasting directly from this:

<?php
$inittime=time();
$dateatota=strtotime('2013-04-29 18:00:00');
$timediff = $dateatota - $inittime; $days=intval($timediff/86400);$remaining=$timediff%86400;
echo $days;?>

Open in new window

0
 
LVL 2

Author Comment

by:itkadmin
Comment Utility
no joy. tried that and flushed caches
0
 
LVL 13

Accepted Solution

by:
Arrow_1 earned 250 total points
Comment Utility
Out of curiosity, can you try this code and see if you get a different line number on the error
<?php $inittime=time(); ?>
<?php $dateatota=strtotime('2013-04-29 18:00:00'); ?>
<?php $timediff = $dateatota - $inittime; $days=intval($timediff/86400);$remaining=$timediff%86400; ?>
<?php echo $days; ?>

Open in new window

0
 
LVL 12

Assisted Solution

by:junipllc
junipllc earned 250 total points
Comment Utility
You can try putting each item on its own line, including the ?> -- you have statements crowded up against each other and Drupal's eval() may not like that. If you space them out you might have happier times ahead?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 12

Expert Comment

by:junipllc
Comment Utility
Something like this (added a little bit of Drupal code standards spacing and format):

<?php
$inittime = time();
$dateatota = strtotime('2013-04-29 18:00:00');
$timediff = $dateatota - $inittime;
$days = intval($timediff / 86400);
$remaining = $timediff % 86400;
echo $days;
?>

Open in new window

0
 
LVL 2

Author Comment

by:itkadmin
Comment Utility
same problem.

Also double checked to make sure wywywig was disabled. thought it may be adding some invisible thing
0
 
LVL 17

Expert Comment

by:nanharbison
Comment Utility
What I do in situations like this is start with just the PHP tags, and then add one new line at a time and see where it throws an error. Also, copy your code onto a notepad document or some plain text program and copy it off of that.

Also maybe add this to report all errors in case something else is really going on. And make sure if you have added any html into this block that you all good html code there.
error_reporting(-1);
0
 
LVL 17

Expert Comment

by:nanharbison
Comment Utility
I just put this code in a D7 block on one of my websites, and I didn't get any errors at all, and it showed the number 97. So it's not your code on this page. Please ignore my last suggestion. When this has happened to me before, there is some PHP code somewhere on your site that is throwing the error and it's not necessary on the page where you are seeing it.
If this is a big complicated site, it might be faster to export the database and do a search for <?PHP in the table that holds the content of pages and blocks. If you don't know which tables to use, let me know, I have to go into one of my databases and check. I don't know offhand.
0
 
LVL 13

Expert Comment

by:Arrow_1
Comment Utility
This should be working. Your site is properly processing php in a block because you've tested it with other code. I tested this code myself and it worked fine on my setup. There seems to be something about this specific code and your setup. I wonder if something screwy is going on with one of the functions you call. Try this:

<?php
$inittime =  $_SERVER['REQUEST_TIME'];
$dateatota = 1367272800;
$timediff = $dateatota - $inittime;
$days = intval($timediff / 86400);
$remaining = $timediff % 86400;
echo $days;
?>

Open in new window

0
 
LVL 17

Expert Comment

by:nanharbison
Comment Utility
My money is on a bit of PHP that has been entered incorrectly somewhere else on the site, not something screwy with functions being called.
0
 
LVL 12

Expert Comment

by:junipllc
Comment Utility
My money is on Drupal Gremlins®
0
 
LVL 9

Expert Comment

by:rinfo
Comment Utility

As stated in the PHP documentation, PHP opening tags must not be present to assess a string. To fix the problem simply delete the opening tag of the string.
and this relates to /home/username/public_html/drupal/modules/php/php.module(80) and not the php code you have mentioned.
0
 
LVL 2

Author Comment

by:itkadmin
Comment Utility
it looks like junipllc's "Drupal gremlins" is the correct answer.  I tried the same code on some different drupal sites, some using the same code base. It worked on some, not on others.

I backed up the DB, thed disabled/uninstalled all non essential modules except php filter. Still wasn't working.

I copied the code from notepad, notepad++, gVim, Dreamweaver and Coda thinking I was getting wierd characters someplace. Still same error.

On a whim, I removed all the line returns so it looks like:

<?php $inittime =  $_SERVER['REQUEST_TIME']; $dateatota = 1367272800; $timediff = $dateatota - $inittime; $days = intval($timediff / 86400); $remaining = $timediff % 86400; echo $days; ?>

Voila. That worked. I have no idea why.
0
 
LVL 2

Author Closing Comment

by:itkadmin
Comment Utility
The solution of using <?php,?> for each line works, or you can all the code on the same line with no returns. It's not the prettiest solution, but it works.
0
 
LVL 2

Author Comment

by:itkadmin
Comment Utility
Additional Info.

seems it is somehow tied to Wysywig

If you move PHP Code to the Top of the list on the Text Formats page at admin/config/content/formats, you can then use PHP code, with line returns, and there are no issues
0
 
LVL 13

Expert Comment

by:Arrow_1
Comment Utility
That makes sense... There was probably a filter set up to add <br> tags for line breaks which would cause an unexpected '<' to be in your code.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now