English 中文(简体)
开始PHP(加载DIV)
原标题:Beginning PHP (loading DIV)
  • 时间:2011-05-28 21:48:51
  •  标签:
  • php

我刚开始学习PHP,一直在阅读和玩——下面的代码,我正在尝试加载页面(正如你所看到的)——我遇到了解析错误。语法错误,index.php第7行出现意外T_VARIABLE

我索引上的第7行

<?php include(  $id  ) ?>

我被告知应该是针对变量而不是($id)

当我在当前编码中按原样使用($id)或($nav_menu)时,它会导致我无法打开流:没有这样的文件或目录

我可能错过了一些显而易见的东西,但有人能为我指明正确的方向吗?

谢谢

<?php
if (is_numeric($_GET[ id ])) //prevent hacking
{
switch ($_GET[ id ]) //get the id of menu needed for this page
{
case 0:
    $nav_menu = "content.php"; //if the variable is 0 or not set
    break;
case 1:
    $nav_menu = "bio.php"; //if the variable is 1, get category 1 links
    break;
case 2:
    $nav_menu = "contact.php"; //if the variable is 2, get category 2 links
    break;
default:
    $nav_menu = "content.php"; //redundant for defaulting if the id is greater than 2
}
}
?>
问题回答
<?php include($id); ?>

没有引号。

此外,请确保您检查了$id的值,您不希望有人包含“../../path/to/my/passwords.txt”或同样令人不安的内容;)

您为<code>$nav_menu</code>提供的switch case语句很粗糙,但很有效,而且肯定是必需的。

此外,我非常确信开关是安全的,因此无需在$_GET[id]上检查is_numeric

Edit: Jared mentioned relative vs absolute paths. I have no idea what he s saying but this is what I think about it: Always always always always always always always always always always always always always always always always always always always always always always always always always use:

include (dirname(__FILE__) . "/thefile.ext");

毫无疑问,这将使你的道路变得绝对。

变量周围不需要使用引号,但如果需要,请确保使用而不是。您可以尝试:

<?php
include($id);
include($nav_menu);
?>

17行代码可以缩短为两行。您可以使用它:

<?php
$list = array("content.php", "bio.php", "contact.php");
$navmenu = (isset($_GET[ id ]) && isset($list[$_GET[ id ]]) ? $list[$_GET[ id ]] :  content.php ;
?>

你在这里做错了四件事。

首先,在每条语句的末尾都需要分号。

<?php include(  $id  ); ?> // Note semicolon

内联字符串语句的任一端都应该只有一个引号,而另一端应该转义,即:

<?php include(  $id  ); ?> // Valid string

现在,如果要扩展该变量,则需要使用双引号字符串或使用连接器。在这种情况下,您不需要引用它:

<?php include("$id"); ?>
<?php include($id); ?>

最后,您应该给出要包含的文件的相对/绝对路径。

<?php

$path = "myinclude.php";
$path = "../includes/myinclude.php";
$path = "/web/my_site_com/includes/myinclude.php";

include($path);

?>

如果使用开关激活特定文件,可以使用双引号字符串执行此操作:

<?php

$path = "../includes/$id";

include($path);

?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...