【问题标题】:How to get access of other pages inside of index.php by PHP?如何通过 PHP 访问 index.php 中的其他页面?
【发布时间】:2015-11-24 21:26:57
【问题描述】:

我在我的电脑上安装了 PHP 和 Apache 服务器。 所以在“htdocs”中我创建了2个文件(index.php,Contact.php)和一个目录(MyClass),之后在“MyClass”中我创建了一个文件(class.php)..

在网络浏览器中,当我使用 url“http://localhost/MyClass/class.php”时,结果是:“class.php”向网络浏览器发送数据。

在同样的情况下,PHP/Apache 有什么方法可以从“index.php”中控制它吗??

或者

我想知道“index.php”中所有来自网络浏览器的请求,有可能吗????

但我不想使用任何 GET 变量,例如“http://localhost/index.php?class=page2..

为我糟糕的英语道歉。

谢谢..

【问题讨论】:

    标签: php apache


    【解决方案1】:

    你应该使用include,在你的情况下你会使用

    include 'MyClass/class.php';
    

    更多关于include的信息可以在here找到

    【讨论】:

      【解决方案2】:

      我不确定我是否理解正确,但是一种不使用的方法?class=page2

      就是创建一个.htaccess文件

      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
      RewriteRule ^index\.php$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
      </IfModule>
      

      这会将所有对不存在的文件或文件夹的请求重写到您的 index.php 使用 $_SERVER['REQUEST_URI'] 进行导航。 例如你可以使用http://localhost/class/page/2

      $_SERVER['REQUEST_URI'] 将是 class/page/2

      如果您的网站位于 htdocs 的子文件夹中,请务必编辑

      RewriteBase /dir/here/
      [...]
      RewriteRule . /dir/here/index.php [L]
      

      匹配它

      【讨论】:

        【解决方案3】:

        我的问题解决了。

        我做了哪些更改如下:

        在“C:\Apache24\conf”需要更改文件“httpd.conf”

        刚刚活跃:

        1)

        LoadModule rewrite_module modules/mod_rewrite.so
        

        2)

        <Directory />
              #AllowOverride none
              AllowOverride All
              Require all denied
           </Directory>
        

        3) 我使用的是“虚拟主机”,所以在“C:\Apache24\conf\extra”中需要更改文件“httpd-vhosts.conf”

        NameVirtualHost *:80
        <VirtualHost *:80>
            ServerAdmin webmaster@test.com
            DocumentRoot "E:/TEST"
            <Directory "E:/TEST">
                Allow From All
                RewriteEngine On
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteRule . /index.php [L]
            </Directory>
            ServerName test.com
            ServerAlias www.test.com
            ErrorLog "logs/test.com-error.log"
            CustomLog "logs/test.com-access.log" common
        </VirtualHost>
        

        如果您没有使用“虚拟主机”,那么我认为您需要在“httpd.conf”内的“目录”中添加一些行!

        <Directory>
            Allow From All
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule . /index.php [L]
        </Directory>
        

        【讨论】:

          【解决方案4】:

          使用 PHP include 函数。您可以在索引文件中包含您的 MyClass/class.php。然后,您可以添加一个 htaccess 文件来限制 MyClass 目录中的文件被直接查看。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-06-28
            • 1970-01-01
            • 2020-05-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-08-19
            • 2013-08-29
            相关资源
            最近更新 更多