Drupal: How to Run PHP-Code
Attention:
You should immediately delete unused code because this could be dangerously. Do not leave back door for hacker!
You can run the code (snippet) in the following ways:
- In block
- In Execute PHP block of Devel module
- On page
- IN a separate file
In block
Block allows flexible restricting access for different roles and output block to specified page(s).
Disadvantages of this method are:
- You should save the block to view code working. If you need to make some changes you should reopen the block and save it again.
- Code in block with unspecified visibility limits can make the whole site unavailable. In this case you can disable the block using SQL-snippet.
To run the code, you should first set input format to: PHP.
On page
Disadvantages:
- You cannot set permissions and scope flexible.
The advantages of this methods are:
- Pages have preview which allows get work result of the code (snippet) without saving the document. So you can debug and correct your code and not save the document if you don’t need this code anymore.
- If page code contains error, site works as well as admin area. And you can just delete this page through admin area. In the case of the block, the whole site will be unavailable.
Setting:
- To run the code, you should insert it to the body of a block with required tags:
//code to run… - Set input format to: PHP
- Fill in the required fields
- Click "Preview" if you don’t wish to use this code constantly or “Save” to save the node.
In Execute PHP block of Devel module
You should install Devel module to use this method. It works similar to inserting the code to block (see above).
In a separate file
You need access to site files to use this method.
Setting:
- Create file with ".php" extension in a folder visible for web-server
- Insert code to the new file:
//If file is not located in the root of web-server, you should correct the path:
require_once ‘./includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
//code to run… - Save file
- Launch file through browser: http://example.com/my_code.php
- Remove file or restrict access to it in.htaccess file (if you are going to use it in future)
Summary
- It is better to insert code to page for code debugging.
- If you need to restrict access to the code – run the code in block.
- You can run code in a separate file if you have access to files only.
Hello Aleksey,
I read your blog ticket and found it very helpful. Especially on the part about a PHP code in a external file.
I have a problems with my drupal version (6.2) because I can’t update my database with a php code inside an external file.
I put the bootstrap.inc file like you said in your ticket. But it doesn’t work. I think that comes from the second part : the htaccess file.
I removed the correct line but the result is the same.
My PHP code doesn’t working. My question is: Do you know the correct syntax to allow or deny access on my specific PHP file ?
I hope you could give me few minutes of your time.
Thank You very Much
Best regards,
Thierry
great info man