简体   繁体   English

PHP包含动态网址中的文件

[英]PHP include file in dynamic url

<?php include("data/url-last-path.php"); ?>

I am new to Php. 我是Php的新手。 Browser given last path should be displayed dynamic as mentioned in the above link. 浏览器给定的最后路径应如上述链接中所述动态显示。 How to accomplish this? 如何做到这一点?

If you want to preserve data through pages(files) you should use super global variables like $_SESSION 如果要通过页面(文件)保留数据,则应使用$ _SESSION之类的超级全局变量

http://www.php.net/manual/en/features.sessions.php http://www.php.net/manual/zh/features.sessions.php

Or in order to get information of paths or locations use $_SERVER 或者为了获取路径或位置的信息,请使用$ _SERVER

http://www.php.net/manual/en/reserved.variables.server.php http://www.php.net/manual/en/reserved.variables.server.php

$_SERVER['HTTP_REFERER'] Contains the address of the page (if any) which referred the user agent to the current page. $ _SERVER ['HTTP_REFERER']包含页面的地址(如果有),该页面将用户代理引向当前页面。 This is set by the user agent. 这是由用户代理设置的。 Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. 并非所有的用户代理都将设置此功能,有些用户代理提供了将HTTP_REFERER修改为功能的功能。 In short, it cannot really be trusted. 简而言之,它不能真正被信任。

Pass that value to another page . 将该值传递给另一页。 So many mechanisms were there. 那里有很多机制。 Its up to you to decide. 由您决定。 Since i am not sure about what are you going to do. 由于我不确定您要做什么。 Let me give you some possibilities for sending data over pages. 让我给您一些通过页面发送数据的可能性。

  1. POST (to next page) 开机自检(下一页)
  2. GET (to next page) GET(转到下一页)
  3. COOKIE ( site level) COOKIE(网站级别)
  4. SESSION ( site level) 会话(网站级别)

Passing value to a same page. 将值传递到同一页面。

Example page1.php : 示例page1.php

<form method="post" action="page1.php">
 <input type="text" name="lastpath">
  <input type="submit" >
</form>
<?php
if(isset($_POST['lastpath']))
 {
  require_once("data/".$_POST['lastpath']); 
 }
?>

Use require_once as Mr @humphrey said. 使用@humphrey先生所说的require_once。

This is really simple. 这真的很简单。 First, create an index.php file and place the following code: 首先,创建一个index.php文件并放置以下代码:

<html>
<head>
<title>One Page Template</title>
</head>
<body>

<p><a href="?page=">index</a> <a href="?page=page1">Page 1</a> <a href="?page=page2">Page 2</a></p>

<?php

$pageView   = 'index';

if(isset($_REQUEST['page']) && $_REQUEST['page'] != null){

    $pageView   = $_REQUEST['page'];

    include_once 'data/'.$pageView.'.php';

}else{

    include_once 'data/'.$pageView.'.php';
}
?>

</body>
</html>

Next, create a folder named "data" and create inside 3 files: 接下来,创建一个名为“ data”的文件夹并创建3个文件:

index.php, page1.php, page2.php index.php,page1.php,page2.php

Place the following code on correspond file: 将以下代码放在对应文件中:

index.php: index.php:

<p>This is index <strong>Welcome!</strong></p>

page1.php: page1.php:

<p>This is page 1</p>

page2.php: page2.php:

<p>This is page 2</p>

Really simple! 真的很简单!

@vengets Answered your question but I also detected that you might get errors in future . @vengets回答了您的问题,但我还发现您将来可能会出错。 I suggest that you also use this require_once because also in future when you will try to include a lot of files you will also get problems so this will solve them . 我建议您也使用此require_once,因为将来在尝试包含很多文件时,也会遇到问题,因此可以解决它们。 this does not primarily answer your question but saves you from future errors 这主要不是回答您的问题,而是使您免于以后的错误

<?php require_once('data/url-last-path.php'); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM