Neil MacDonald
Neil MacDonald

Reputation: 3

Using PHP url variables/php includes

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?

Upvotes: 0

Views: 350

Answers (3)

adamsmith
adamsmith

Reputation: 5999

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' );
}

Upvotes: 0

4ng3l0
4ng3l0

Reputation: 98

Just make it dynamic! I would do something like this:

article.php?id=id_of_my_article

if(isset($_GET['id'])) include($_GET['id'].".php");
else include('noarticle.php');

Upvotes: 0

Joe
Joe

Reputation: 15802

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 load article.php which tries to load article.php which tries to ... you get the idea.

Upvotes: 1

Related Questions