使用 PHP url 变量/php 包含
我想做的是创建一个 URL,例如:
article.php?00001
然后使用以下代码,这将包括 00001
作为 article.php
中的文章
if(isset($_GET['00001'])){
include('00001.php');
}else if(isset($_GET['00002'])){
include('00002.php');
} else {
include('noarticle.php');
}
现在,这可以工作,如果我只是继续添加 00003
-00010
等,那么将适合多篇文章,但是如果我打算添加更多文章,是否有更好的编码方式而无需必须手动插入商品编号?
What I'm trying to do is to create a URL, example:
article.php?00001
Then using the following code this will include 00001
as an article within article.php
if(isset($_GET['00001'])){
include('00001.php');
}else if(isset($_GET['00002'])){
include('00002.php');
} else {
include('noarticle.php');
}
Now, this works, and would be suitable for several articles if I just keep adding 00003
-00010
etc, but if I intend to add MANY more articles, is there a better way of coding this without having to manually insert article numbers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用数据库来存储您的文章。查看 http://www.freewebmasterhelp.com/tutorials/phpmysql 获取有关如何将 MySQL 与 PHP 结合使用。
对于您的网址,请使用
article.php?id=###
,然后使用$_GET['id']
来确定正在查看哪篇文章。通过包含基于用户提供的数据的文件,如果用户转到
article.php?article
,它会尝试加载article.php
,而article.php
会尝试加载Article.php
试图...你明白了。Use a database to store your articles. Have a look at http://www.freewebmasterhelp.com/tutorials/phpmysql for a guide on how to use MySQL with PHP.
With regards to your URLs, use
article.php?id=###
then use$_GET['id']
to determine which article is being viewed.By including files based on user-supplied data, what if the user goes to
article.php?article
- it tries to loadarticle.php
which tries to loadarticle.php
which tries to ... you get the idea.只要让它充满活力!
我会做这样的事情:
Just make it dynamic!
I would do something like this:
首先您需要知道仅基于 url 包含文件是不安全的。正如 @Joe 和 @Angelo Cavallini 所写,还有其他更好的方法可以做到这一点。
但如果您很清楚后果并决心这样做,那么您可以尝试:
$id = 当前( $_GET );
$id && $id=intval($id);
如果( $id ){
包含( $id.'php' );
}
First you need to know that it's insecure to include files simply based on url. There are other better means of doing so, as @Joe and @Angelo Cavallini wrote.
But if you are well aware of the consequences and determined to do so, you man try:
$id = current( $_GET );
$id && $id=intval($id);
if( $id ){
include( $id.'php' );
}