syrkull
syrkull

Reputation: 2344

creating error page on a certain php dictinary

I am trying to create an error page but I want to specify it on a certain dictionary. the problem is that it is a php file. for example if I have a page with a title called page1 only and do not have anything else, I want to display an error for every other page that does not exist like page2. http://example.com/showpage.php?title=page1 http://example.com/showpage.php?title=page2 I do not want to use htaccess because there is other files in the same dictionary that I want to display another error message.

Upvotes: 0

Views: 70

Answers (2)

omasdg
omasdg

Reputation: 116

Do you query against a database or do you include real files like page1.php for example?

EDIT: If you are using MySQL, then you want to use mysql_num_rows() against your query and if that returns true, do all the echoing, but if it returns false, then include_once('error-for-non-existing-title.php'); Optionally if no $title variable is set, you could throw a 404 header. You can rewrite your 404 documents in .htaccess if you want to

I think for an online-reader, you should have more than one variable ($_GET['title']) AND $_GET['page']. Use $title to differentiate between different titles and $page to navigate through it's pages.

I guess you don't know how to use PDO yet, so..

$link = mysql_connect("localhost", "username", "password");
mysql_select_db("database_name", $link);

if(isset($_GET['title']))  {
$title = $_GET['title'];

$query = mysql_query("SELECT * FROM pages WHERE title='$title'", $link);

$num_rows = mysql_num_rows($query);

//if there is a page
if($num_rows!=0) {

//fetch the data as associative array
$result = mysql_fetch_assoc($query);

echo $result['title'],'<br />';
}
else include_once('error.php');
}

//optionally if you want to, you could throw a 404 header if there is no $title
//variable set. Just remove the double slashes from the next line of code.
//else header("HTTP/1.1 404 Not Found");  

Upvotes: 1

Corbin
Corbin

Reputation: 33457

When the page name is the same, and only the parameters are different, I would avoid sending a non-200 header.

However, you could of course just do:

if( doesnt exist) { some error message } else { render it }

If you want to send a 404 error, I would use a rewrite rule to have a URL like:

http://blah/page/page1

Then you could send a 404 status with a lower chance of confusing browsers.

The rewrite, assuming you're using Apache would be something like:

RewriteEngine On RewriteRule ^page/page([0-9])/?$ /showpage.php?title=page$1

Note: untested rewrite rule, but if you are interested in going that path, between that and reading the Apache docs, you should be able to get it.

Upvotes: 1

Related Questions