可能重复:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
我正在做youtube mvc教程http://www.youtube.com/watch?v=Aw28-krO7ZM并停止了最好的第一步:
我的index.php文件:
<?php
$url = $_GET['url'];
require 'controllers/' . $url . '.php';
我的.htaccess文件:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
所以,接下来的问题是:当我继续使用除“索引”之外的任何其他网址时,它的效果很好。那么'index'url有什么问题?
Notice: Undefined index: url in /var/www/sadman/index.php on line 2
Warning: require(controllers/.php) [<a href='function.require'>function.require</a>]:
failed to open stream: No such file or directory in /var/www/sadman/index.php on line 4
顺便说一句,我有LAMP,我认为我的设置是对的。
答案 0 :(得分:1)
通知告诉您url
中不存在键$_GET
- 可能是index.php直接调用(如http://yourdomain.com/index.php - 在这种情况下,重写不会因文件存在而无效。)< / p>
警告是因为文件不存在。
要修复两者,请执行以下操作:
$url = 'default'; // set default value (you need to have also existing file controllers/default.php)
// check if `url` exists in $_GET
// check if it is string
// check if it match proper pattern (here it has to be built from letters a-z, A-Z and/or _ characters - you can change it to match your requirements)
// check if file exists
if (isset($_GET['url']) && is_string($_GET['url']) && preg_match('/^[a-z_]+$/i',$_GET['url']) && file_exists('controllers/'.$_GET['url'].'.php')) {
$url = $_GET['url']; // name in $_GET['url'] is ok, so you can set it
}
require('controllers/'.$url.'.php');