php代码或我用来访问多个页面的内容的方法存在一些问题,这导致index.php内的内容显示失败。
我的index.php代码逻辑是:
<div>
<ul>
<li><a href="index.php?pg=alta_usuari" title="Alta Usuaris">Alta Usuaris</a></li>
<li><a href="index.php?pg=gestio_usuaris" title="Gestio Usuaris">Gestio Usuaris</a></li>
<
</ul>
</div>
<div id="contentarea">
<article id="article"><?php echo file_get_contents('pages/'. $pgname. '.php'); ?></article>
</div>
页面上的代码为:
This text is displayed in main page after click on link
<?php
echo "this text is not displayed after click on link. !";
?>
我用来访问多个页面内容的方法有什么问题?
问题在于:
<article id="article"><?php echo file_get_contents('pages/'. $pgname. '.php'); ?></article>
index.php的完整代码为:
<?php
// create an array with data for title, and meta, for each page
//TODO: pgdata from config file
$pgdata = array();
$pgdata['index'] = array(
'title'=>' ',
'description'=>'Here add the description for Home page',
'keywords'=>'meta keywords, for, home page'
);
$pgdata['alta_usuari'] = array(
'title'=>'Alta Usuari',
'description'=>'Description for this page',
'keywords'=>'alta usuari'
);
$pgdata['estat_hardware'] = array(
'title'=>'Estat Hardware',
'description'=>'Here add the description for the page',
'keywords'=>'keyword 1,keyword 2'
);
// set the page name
//Then we set a variable with the name of the page. If there is $_GET['pg'],
// we get the name of the page from URL, otherwise we set the default name "index";
//and get its data from Array.
$pgname = isset($_GET['pg']) ? trim(strip_tags($_GET['pg'])) : 'index';
// get title, and meta data for current /accessed page
$title = $pgdata[$pgname]['title'];
$description = $pgdata[$pgname]['description'];
$keywords = $pgdata[$pgname]['keywords'];
// set header for utf-8 encode
header('Content-type: text/html; charset=utf-8');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
<!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<style> <?php include 'css/main.css'; ?> </style>
</head>
<body>
<header id="header">
<h3><?php echo $title; ?></h3>
</header>
<div id="line">
</div>
<div>
<ul>
<!-- <li><a href="index.php" title="Alta Usuari">Alta Usuari</a></li> -->
<li><a href="index.php?pg=alta_usuari" title="Alta Usuaris">Alta Usuaris</a></li>
<li><a href="index.php?pg=gestio_usuaris" title="Gestio Usuaris">Gestio Usuaris</a></li>
</div>
<div id="contentarea">
<article id="article"> <?php echo include('pages/'. $pgname. '.php'); ?></article>
</div>
<!--
<footer id="footer">
<p>© air-fi.es</p>
</footer>
-->
</body>
</html>
答案 0 :(得分:0)
此代码不像
一样执行php代码使用file_get_contents
。用include替换后,它成功执行了php代码。
<div id="contentarea">
<article id="article"> <?php echo include('pages/'. $pgname. '.php'); ?></article>
</div>
感谢支持。