体系结构MVC脚本

I write social network with MVC architecture it work localhost (wampserver) correctly but when i upload it on real host i get this error in http://example.com/

Warning: require_once(views/index/index.php): failed to open stream: No such file or directory in /home3/farazenc/public_html/fb/views/index.php on line 14

Fatal error: require_once(): Failed opening required 'views/index/index.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home3/farazenc/public_html/fb/views/index.php on line 14

and my method not work for example when i go http://example.com/index/register must show register form but show 404

my main file: .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

index.php

<?php
session_start();
require_once './config.php';
require_once './lib/database.php';
require_once ('./lib/function.php');
$connect = database::connect();

function __autoload($classname) {
    $directrey = array('controllers', 'models');
    for ($i = 0; $i < sizeof($directrey); $i++) {
        if (file_exists($directrey[$i] . '/' . $classname . '.php')) {
            require_once ($directrey[$i] . '/' . $classname . '.php');
        }
    }
}

if (isset($_GET['url'])) {
    $url = $_GET['url'];
    $split = preg_split('/[\/\\\]/', $url);
    if (sizeof($split) == 1) {
        if (!file_exists('controllers/' . $split[0] . 'Controller.php')) {
            require_once ('./controllers/CheckPage.php');
            CheckPageController::check($split[0]);
        } else {

            $classname = $split[0] . 'Controller';
            $Controller = new $classname();
        }
    } elseif (sizeof($split) == 2) {
        if (file_exists('controllers/' . $split[0] . 'Controller.php')) {
            if (empty($split[1])) {

                $classname = $split[0] . 'Controller';
                $Controller = new $classname();
            } else {

                $classname = $split[0] . 'Controller';
                $Controller = new $classname();
                if (method_exists($Controller, $split[1])) {
                    $Controller->$split[1]();
                } else {
                    require_once ('./views/404.php');
                }
            }
        } else {
            require_once ('./views/404.php');
        }
    }
} else {
    require_once ('./controllers/IndexController.php');
    $IndexController = new IndexController();
}
if (isset($_POST['Action'])) {
    $ajax = new Ajax();
    $ajax->$_POST['Action']();
}

Controller.php

<?php

abstract class Controller {

    public function render($file) {
        if (!isset($_POST['Action'])) {
            global $split;
            $d = empty($split) ? 'index' : $split[0];
            $content = "views/$d/$file.php";
            require_once ('views/index.php');
        }
    }
    public function user_views($file){
        if (isset($_SESSION['user'])){
            require_once ('views/user/'.$file.'.php');
        }  else {
            require_once ('views/index.php');
        }
    }
    public function admin_views($file){
        if (isset($_SESSION['admin'])){
            global $split;
            $d = empty($split) ? 'index' : $split[0];
            $content = "views/$d/$file.php";
            require_once ("views/admin/admin.php");
        }  else {
            require_once ('views/admin/login.php');
        }
    }
}

IndexController.php

class IndexController extends Controller {

    public function __construct() {
        global $split;
        if (empty($split[1])) {
            $this->render('index');
        }
    }

    public function register() {
        $this->render('register');
    }
}

views/index.php:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>social network</title>
    <link href="<?php echo URL ?>/css/style.css" rel="stylesheet">
    <script type="text/javascript" src="<?php echo URL ?>/js/ajax.js"></script>
    <script type="text/javascript" src="<?php echo URL ?>/js/jquery.js"></script>
</head>
<body>
<div style="width:1000px;height:650px;margin:50px auto;">
    <div id="content">
        <?php
        require_once($content);
        ?>
    </div>
    <div id="ads" style="margin-right:15px;">
        <img src="<?php echo URL ?>/images/ads-120.jpg">
        <img src="<?php echo URL ?>/images/ads-120.jpg">
    </div>
</div>
</body>
</html>

what's the problem and how can i fix it ?

php
dongtun1209
dongtun1209 还在htaccess中设置目录root作为条件,还要注意wamp(windows)解释./不同然后linux这样做
5 年多之前 回复
douju3911
douju3911 如果要从Windows迁移,请检查文件名的大小写
5 年多之前 回复
dongluzhi5208
dongluzhi5208 是权限,重写mod是好的
5 年多之前 回复
dongnai8013
dongnai8013 检查服务器文件权限。检查是否安装了mod_rewrite?只有htaccess才有效
5 年多之前 回复
doushou5761
doushou5761 检查服务器上的权限。目录为0755,至少为您包含的文件为0644。
5 年多之前 回复

1个回答



我过去遇到过类似的问题。 如果文件权限是正确的(755/644),我建议您检查SELinux(对于RHEL / CentOS等)。 SELinux默认配置为允许httpd使用/ var / www / html目录中的文件但不在此目录之外。</ p>

如果此类软件正常工作(尝试“sestatus”命令),则可以 通过打开SELinux配置文件(通常是/ etc / selinux / config)并更改行来禁用它:</ p>

  SELINUX = enforcing 
</ code> </ pre>
\ n

to:</ p>

  SELINUX = disabled 
</ code> </ pre>

然后重启系统。</ p> \ n

假设“sestatus”命令必须显示类似的内容:</ p>

  SELinux status:disabled 
</ code> </ pre>

现在再次测试代码(但请确保在此测试后激活SELinux)。
如果有帮助,您只需使用命令进行相应配置:</ p>

  $ chcon -R --type = httpd_sys_content_t / path_to_document_root_of_you_great_site 
</ code> </ pre>

或更永久的解决方案(更好的方式):</ p>

 < 代码> $ semanage fcontext -a -t httpd_sys_content_t'/ path_to_document_root_of_you_great_site(/.*)?'nn $ r  estorecon -R -v / path_to_document_root_of_you_great_site 
</ code> </ pre>

如果您需要httpd可以更改文件,请使用“httpd_sys_rw_content_t”而不是“httpd_sys_content_t”</ p>

PS 我为我的英语道歉</ p>

参考:</ p>

展开原文

原文

I had similar problem in the past. If file permission is correct (755/644), i advice you to check SELinux (for RHEL/CentOS etc.). SELinux by default configured to permit httpd to work with files in /var/www/html directory but not outside this directory.

If such software working (try "sestatus" command), you can disable it by opening SELinux config file (usually /etc/selinux/config) and change line:

SELINUX=enforcing

to:

SELINUX=disabled

and then reboot system.

Afted that "sestatus" command must show something like that:

SELinux status:             disabled

and now test you code again (but make sure you activate SELinux after this tests). if it helps, you just need to config it accordingly by using command:

$ chcon -R --type=httpd_sys_content_t /path_to_document_root_of_you_great_site

or more permanent solution (better way):

$ semanage fcontext -a -t httpd_sys_content_t '/path_to_document_root_of_you_great_site(/.*)?'
$ restorecon -R -v /path_to_document_root_of_you_great_site

if you need that httpd can change files use "httpd_sys_rw_content_t" instead of "httpd_sys_content_t"

P.S. I'm apologize for my English

Reference:

dtp791357
dtp791357 固定......我希望
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问