I
I
Ivan2015-05-09 07:21:42
PHP
Ivan, 2015-05-09 07:21:42

How to add article with PHP code to MYSQL database?

I'm learning PHP and I'm new to this stuff. I decided to make a small blog for myself, for my own self-development, and for tracking my progress in learning PHP.
But something incomprehensible happened (I'm a beginner), when I try to add an article with PHP code to the MYSQL database, I get an error 403 Access denied.
Tried escaping article text with htmlspecialchars and mysqli_real_escape_string but nothing changes. Why is this happening? For example, I added the simplest <?php $a=1;
as a php code to the article ; echo($a); ?> But still 403 Access denied. Articles without PHP code are added to the database and displayed on the site normally. Now I noticed that the server starts to swear only if there are characters <?
these two and in that order

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan, 2015-05-10
@iwqn

I wrote to the hosting support - the answer came
Requests are blocked by ModSecurity
We removed this rule from mod_security, which blocked attempts to send code directly through forms.

@
@Quty, 2015-05-09
_

403 Access denied - not a PHP or MySQL error, but a web server error. Most likely, you have an incorrectly configured virtual host, or you do not have enough rights to the directory where the scripts are located.
As for writing text, then everything is in order, the following code quite successfully writes a line containing PHP code:

$stmt = $mysqli->prepare("INSERT INTO code_examples(code_text) VALUES (?)") or die;  
$stmt->bind_param("s", htmlspecialchars('<?php $i = 5; echo $i; ?>')) or die;
$stmt->execute() or die;

I
infastin, 2015-05-09
@infastin

I personally have never done that.
For example, try adding this to the database:
{a},
and then process it in php:

<?php
  $a = 1;
  $text = str_replace("{a}", $a, "Здесь получаем статью из БД");
  return $text;
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question