part of my php code is displayed in the browser when I do not open with localhost. How do I resolve?

Asked

Viewed 91 times

1

o erro

if ($validacao) {
$pdo = Banco::conectar();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO perguntas (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) VALUES (?,?,?,?,?,?,?,?,?,?)";
$q = $pdo->prepare($sql);
$q->execute(array($p1, $p2, $p3, $p4, $p5, $p6, $p7, $p8, $p9, $p10));
Banco::desconectar();
header("Location: index.php");

}

  • review your code, see where they close and open php tags, something must be escaping

  • I’ve checked it several times, but I can’t identify it. I’m new to the language

  • that your code snippet is too small... your error may be in another snippet or file

  • this is the only part of the code that appears in the browser. I believe the rest is right

  • Can reveal your complete code? So I can get an idea of what to do to help you (because I hardly know PHP).

2 answers

1


Hello.

This is the expected behavior. A PHP file is a common text file, and should be understood as such by the browser. The fact that part of your code was interpreted when accessed as a file shows that something is not right. If all were correct, your website should only work through http://localhost/ or 127.0.0.1 (since, thus, XAMPP interprets PHP and sends only HTML, CSS and JS to the browser) and display the entire PHP code when accessing file:///<...>. For example:

Veja um código de exemplo que é exibido por completo quando aberto no navegador via <code>file:///</code>

So try to clear your cache and refresh the page, or access from another browser. Displayed content should be plain text.

If the problem persists, send your complete code.

  • 1

    you were right. Thank you

0

This is easy to identify, can be two things:

  1. You have inserted your PHP code outside the PHP block. It ends up being sent as HTML and is shown on the screen.
  2. You do not have the PHP server on your machine, or it is not enabled.
  3. You accessed the file directly instead of "navigating" to it.

In your case, it is the third option, because there in the address bar is written "file:///C:/..." This occurs when someone tries to open the source file. By default, it displays HTML, CSS and JS normally, as they are already automatically interpreted by the browser, since they already have the built-in configuration, which is not the case for PHP, which needs a server installed the part on your computer or server (just like Java, C#, React etc).

You must have made two clicks on the file, or dragged it into the browser.

How do you fix it? Just enable your PHP server and navigate the URL to your page, using the "localhost" or "127.0.0.1".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.