D
D
dosh932015-11-02 19:17:43
PHP
dosh93, 2015-11-02 19:17:43

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";
?>

header.php file
<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>

class test_class.php
<?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;
        }
    }
}

and the session.php file, I go to it when I click "About the site" and I want to display the session
<?php
echo $_SESSION['test'];
?>

but I see a blank page.
I inspect the page, I see that it is commented out
<!--?php

echo $_SESSION['test'];?-->

How to solve the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Khomenko, 2015-11-02
@dosh93

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");
}

In order for session.php to be executed, it needs to be run using include\include_once or require\require_once
UPD: Here's an option for you:
public function renderPhpFile($_file_, $_params_ = [])
    {
        ob_start();
        ob_implicit_flush(false);
        extract($_params_, EXTR_OVERWRITE);
        require($_file_);
        return ob_get_clean();
    }

I
Ivanq, 2015-11-02
@Ivanq

To create a parameter, you need not only write to an array. You also need f-yu session_register .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question