Answer the question
In order to leave comments, you need to log in
PHP code not being interpreted?
I just started to learn php and faced such problem.
there is an index.php file
<?php
session_start();
$_SESSION["test"]="adadadasd";
include_once "class/test_class.php";
$myclass=new test_class();
include_once "templates/header.php";
if ($myclass->getError()){
echo $myclass->getError();
}
echo $myclass->getContentPage();
include_once "templates/footer.php";
?>
<html>
<head>
<title>Тестовый сайт на PHP</title>
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.css" rel="stylesheet" media = "screen">
<link rel="stylesheet" type="text/css" href="styles/main.css"
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a href="?page=main" class="navbar-brand">Главная</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="?page=session">О сайте</a></li>
<li><a href="?page=ox2">Почему</a></li>
<li><a href="?page=sesion">zzz</a></li>
</ul>
</div>
</div>
</nav>
</body>
</html>
<?php
class test_class{
private $_page_file=null;
private $_error=null;
public function __construct() {
if(isset($_GET["page"])){
$this->_page_file=$_GET["page"];
$this->_page_file = str_replace(".", null, $_GET["page"]);
$this->_page_file = str_replace("/", null, $_GET["page"]);
$this->_page_file = str_replace("", null, $_GET["page"]);
if(!file_exists("templates/". $this->_page_file . ".php")){
$this->setError('<div class="alert alert-danger" role="alert">Шаблон не найден</div>');
$this->_page_file="main";
}
}
else $this->_page_file="main";
}
private function setError($error){
$this->_error=$error;
}
public function getError(){
return $this->_error;
}
public function getContentPage() {
return file_get_contents("templates/" . $this->_page_file . ".php");
}
public function print_session() {
return $_SESSION["test"];
}
public function getTitle() {
switch ($this->_page_file) {
case "main":
return "Главная страница сайта ";
break;
case "about":
return "О компании ";
break;
case "ox2":
return "Преимущества ";
break;
default:
break;
}
}
}
<?php
echo $_SESSION['test'];
?>
<!--?php
echo $_SESSION['test'];?-->
Answer the question
In order to leave comments, you need to log in
Well, that's right, you're reading the contents of the file, not executing it.
public function getContentPage() {
return file_get_contents("templates/" . $this->_page_file . ".php");
}
public function renderPhpFile($_file_, $_params_ = [])
{
ob_start();
ob_implicit_flush(false);
extract($_params_, EXTR_OVERWRITE);
require($_file_);
return ob_get_clean();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question