Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to properly add PHP code to blocks in Drupal 7

Posted on 2013-01-21
24
Medium Priority
?
1,396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 5
  • +3
24 Comments
 
LVL 16

Expert Comment

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

Author Comment

by:itkadmin
ID: 38801692
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
ID: 38802206
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

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

Expert Comment

by:Aaron Feledy
ID: 38802404
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
ID: 38802555
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
ID: 38802656
<?php print 'test'; ?>
works fine

user is 1/admin
0
 
LVL 12

Expert Comment

by:junipllc
ID: 38802685
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:Aaron Feledy
ID: 38802697
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
ID: 38802714
no joy. tried that and flushed caches
0
 
LVL 13

Accepted Solution

by:
Aaron Feledy earned 1000 total points
ID: 38802750
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 1000 total points
ID: 38802795
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
 
LVL 12

Expert Comment

by:junipllc
ID: 38802827
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
ID: 38802895
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
ID: 38803386
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
ID: 38803403
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:Aaron Feledy
ID: 38803495
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
ID: 38803595
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
ID: 38803879
My money is on Drupal Gremlins®
0
 
LVL 9

Expert Comment

by:rinfo
ID: 38805008

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
ID: 38805522
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
ID: 38805640
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
ID: 38805746
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:Aaron Feledy
ID: 38806421
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

604 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