I will list two existing errors:
1. Print():
First in this case the print
is not correct, just remove.
<--php-->
$nomeusuario = $_SESSION['nome'];
$html = str_replace('#NOMEUSUARIO#', $nomeusuario, $html);
<--html-->
Olá, <strong>#NOMEUSUARIO#</strong> seja bem vindo
The reason for this is simple.
When using the print
will be literally displaying the content.
Don’t you get it?
Take this example:
$qualquer_coisa = print('meu_nome');
This will display meu_nome
in the file, usually at the beginning once the html is after this function. But, the result will be displayed where it is called.
And the variable $qualquer_coisa
? This will hold the value 1
, If it was working perfectly, the #NOMEUSUARIO#
would be replaced by 1
.
2. $html?!
There is a detail, I do not know if it was created by you when posting the question, but I will be considering a mistake.
What the variable $html
does it have? Nothing.
To solve I advise making two files, for example:
A file to save purely HTML:
/public_html/html_index.html ~ Example, but leave before public_html!
Olá, <strong>#NOMEUSUARIO#</strong> seja bem vindo
/public_html/index.php
$nomeusuario = $_SESSION['nome'];
$arquivo = 'html_index.html';
// Caminho de exemplo!
$html = file_get_contents( $arquivo );
// Carrega o HTML, do outro arquivo.
$html = str_replace('#NOMEUSUARIO#', $nomeusuario, $html);
echo $html;
// Exibe o HTML, já alterado.
Why is that?
The file_get_contents()
will get the HTML of the other file, which is purely HTML, so the str_replace()
will just replace the content that was loaded earlier.
I don’t understand. What’s in $html before str_replace? And the correct way to assign the value to the $username variable is: $username = $_SESSION['name'];
– Ivan Nack