2015-10-03 36 views
1

我真的有这个问题在我的脑海里,真的没有得到任何答案。 任何人都可以告诉我,因为我是一个初学PHP开发者..我真的在脚本的一些开发工作,但我真的需要知道如何在登录系统中绑定大量的PHP页面,而我知道这是通过PHP会话,但我真的需要知道如何有不同的访问他们的链接..! 例如,我有一个网站,它有以下文件通过PHP会话相互链接,但我真的不需要公开PHP文件的文件名,也是公共目录,所以我怎样才能真正地调用它或访问这个为我我现在做这样的:如何获得没有扩展的链接?

http://localhost/script/dashboard.php 
http://localhost/script/invoice.php 
http://localhost/script/settings.php 

相反,我希望这些链接像这样的:

http://localhost/script/dashboard/ 
http://localhost/script/index=?dashboard 

或页面的ID,如:

http://localhost/script/page-56/ 

所以我真的需要知道它..! 如果任何人都可以回答的话,会很棒!

回答

1

的结构让我们假设这个目录结构

Directory structure


创建一个名为router.php的文件,它将包含所有其他文件。

文件:/router.php

<?php 

if(isset($_GET['file'])) { 

    // Sanitize file name 
    $file = preg_replace('/[^a-z]+/i', '', $_GET['file']); 

    if($file != '') { 

     $file_location = './phpfiles/' . $file . '.php'; 

     if(is_file($file_location)) { 

      // Avoid direct access to secure files 
      define('__SECURE_ACCESS__', 1); 

      require_once ($file_location); 
      exit; 

     } 

    } 

} 
echo '<h1>Sorry, no such file in here!</h1>'; 

?> 

你需要国防部重写以隐藏真实文件的位置,并删除文件扩展名。

示例网址设置:http://localhost/script/settings

文件:/.htaccess

RewriteEngine on 
RewriteRule ^script/([a-z]+)$ router.php?file=$1 [L,NC] 

这些都是基本的PHP文件你提到。请务必在所有这些文件中包含该常量检查。

文件:/phpfiles/settings.php

<?php 

// No direct access to this file! 
defined('__SECURE_ACCESS__') or die('Hack attempt!'); 

echo '<h3>', $file, '</h3>'; 
echo '<p>This is the "', $file, '.php" included file!</p>'; 

?> 

这是拒绝访问文件的文件夹phpfiles

文件:/phpfiles/.htaccess

order deny,allow 
deny from all 
+0

嗯,我需要知道我会调用这样的链接来访问它们吗? 'http://localhost/script/router.php?file = dashboard' 'http://localhost/script/router.php?file = invoice' 'http://localhost/script/router.php?文件=设置' –

+0

不,Apache服务器执行它,你和你的用户不。 您的URI将如下所示:http:// localhost/script/dashboard http:// localhost/script/invoice http:// localhost/script/settings等等... – BornKillaz

1

您使用preg_replace

$myLink = "http://localhost/script/dashboard.php"; 
$myLink = preg_replace('/\.php$/i', '/', $myLink); 
echo $myLink; 
//http://localhost/script/dashboard/ 

OR

$myLink = "http://localhost/script/dashboard.php"; 
$myLink = preg_replace('%http://localhost/script/(.*?)\.php%i', 'http://localhost/script/index?page=$1', $myLink); 
echo $myLink; 
//http://localhost/script/index?page=dashboard 
+0

确实是完美的..但是如何ld我真的能够通过该链接访问一个PHP文件将给我一个404找不到错误,而不是..! cuz文件名将像'someting.php'一样,并且我正在与其他链接访问它..! –

+0

使用[mod_rewrite](https://httpd.apache.org/docs/current/mod/mod_rewrite.html)。 http://www.sitepoint.com/apache-mod_rewrite-examples/ –

+0

上的一些例子除了'mod_rewrite'之外,没有别的办法可以做到这一点吗? –

1

一旦网站被引用特定页面,它使用类似于

http://localhost/script/dashboard.php

但是如果你想要的链接是没有mypage.php,你可以做以下。您可以使用名为dashboard的目录设置,并在其中包含index.php。见下面的结构。

这是你所拥有的前

-script 
    -dashboard.php 

如果你想链接到刚刚结束在像http://localhost/script/dashboard/

使用这样

-scripts (folder) 
    -dashboard (folder) 
     -index.php 
+0

但我真的不想结束目录..我真的知道这...这是非常简单的解决方案..但我真的不需要有一个目录为每个PHP文件只是不公开它和的确,这是一个糟糕的主意因为每个人都会知道那显然它是'index.php'文件在那个目录里面..这不是个好主意..! –

+0

虽然仍然是你的大拇指..! –

相关问题