heres one for you clever guys out there, error404.html help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

heres one for you clever guys out there, error404.html help

Post by ianhull »

Hi guys,

I got a little issue,

I have a custom error404.html

I only have one other page on my site, index.html

Please note .html extention is enabled to use php

All my pages come from the database, depending on the url.

Here is what I have,

Code: Select all


<?php session_start();

    include_once($_SERVER['DOCUMENT_ROOT'].'/includes/connection.html');

    $domain_name = $_SERVER['HTTP_HOST'];
    
	$page_name = str_replace('/', '', $_SERVER['SCRIPT_NAME']);

    $getPage = mysql_query("SELECT
	id,
	company_name,
	company_email,
	domain_name,
	page_name,
	page_details,
	page_template,
	create_date,
	modify_date FROM website_pages WHERE domain_name = '$domain_name' AND page_name = '$page_name' LIMIT 1")or die(mysql_error());

    if(mysql_affected_rows() == 0){
	
	$page_template = $_SERVER['DOCUMENT_ROOT'].'/css/standard/';
    include_once($_SERVER['DOCUMENT_ROOT'].'/includes/404.html');
	exit();
	
    };
	
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
';
	
	while($pageRecieved = mysql_fetch_array($getPage)){
	extract($pageRecieved);
	}
	
	$page_title = substr($page_details,0,30);
	
	$page_keywords = substr($page_details,0,600);
	
	$page_description = substr($page_details,0,100);
	
	echo '<base href="http://'.$_SERVER['HTTP_HOST'].'/" />
<link href="css/'.$page_template.'.css" rel="stylesheet" type="text/css" />
<title>'.$page_title.'</title>
<meta http-equiv="imagetoolbar" content="no" />
<meta name="keywords" content="'.$page_keywords.'" />
<meta name="description" content="'.$page_description.'" />
<meta name="copyright" content="'.$company_name.'" />
<meta name="owner" content="'.$company_email.'" />
<meta name="language" content="en-gb" />
<meta name="rating" content="general" />
<meta name="robots" content="index,follow" />
<meta name="revisit-after" content="7 days" />
<meta name="author" content="ian@domain" />
<meta name="expires" content="never" />
</head>

<body>
<div id="logo"><img src="company_logos/'.$company_name.'" alt="'.$company_name.'" /></div>

<div id="content">'.$page_details.'</div>

<div id="navigation">
<img src="images_'.$page_template.'/navigation_top.jpg" alt="Website Navigation" width="200" height="42" />
';

include_once($_SERVER['DOCUMENT_ROOT'].'/includes/navigation.php');
echo '
</div>

</body>
</html>';

?>

this works just fine, for the index.html,

here is my navigation file.

Code: Select all


<?php

    $getNavigation = mysql_query("SELECT
	domain_name,
	page_name FROM website_pages WHERE domain_name = '$domain_name'")or die(mysql_error());
	
	while($navigationRecieved = mysql_fetch_array($getNavigation)){
	extract($navigationRecieved);
	
	$page_name = str_replace('.php', '', $page_name);
	
	if($page_name == 'index'){
	$page_name = 'Home';
	}
	
	echo '<p><a href="'.$page_name = strtolower(str_replace(' ', '-', $page_name)).'.php">'.$page_name.'</a></p>
';
}
?>

This also works, but it creates link to pages that do not exist, for example.

home.php
contact us.php

This is why I would like my custom error404.html page to retrieve the requested filename.

for example,

http://www.domain.com/get-me-this-page.html

but god knows how to do it.

I am hoping one of you guys can help me achieve this,

All I am recieving at the moment is, /index.html when the error404.html is included.
I really need to recieve the /what-ever-page.html so that I can query the database and get the needed info.

I am not interested in mod_rewrite or anything like that so suggestions for that are out of the window :)

My main objective is to build a website using 3 pages.

index.html
connection.html
error404.html

Thanks guys
nyfael
Forum Commoner
Posts: 32
Joined: Thu Sep 21, 2006 2:28 am

Post by nyfael »

I was going to suggest mod_rewrite so I'll just stop there.

I'm mainly curious. Why?
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Why i dont want to use mod_rewrite or why I would like to make a 3 page website?

Well, mod_rewrite I find complicated, I tried it so many times and never got it working.

I would like to make a 3 page website, and then get it to a 1 page website which supports, multiple companies and domains, shopping carts, and any new module I make.

I have some spare time now so this is what I would like to do, once it's done there's no messing when i take on board a new company.

I just simply login to my control panel, add a new domain/company etc, give them access to a website or a shopping cart, calendar etc.

Ian
mattkenefick
Forum Newbie
Posts: 12
Joined: Tue May 29, 2007 1:01 pm

Post by mattkenefick »

have you tried javascript to get the window location? or what was put in the address bar?
mattkenefick
Forum Newbie
Posts: 12
Joined: Tue May 29, 2007 1:01 pm

Post by mattkenefick »

if the javascript works


option1:
you could have it redirect to like..

http://whatever.com/fake-page.html

=

http://whatever.com/?s=fake-page



option2:
or set it to a cookie and have it reload the page. then when the PHP loads and sees the cookie is set, it'll display whatever is set in the cookie.



theres actually plenty options you could do if the JS works.


-------------------------
If it doesnt work...

Could you get the HTTP Referrer on the error page to see where they came from? Maybe that'll give you the link they tried to use. Then put that through whatever Switch statement or DB Query you have setup to include the correct information.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Thanks Matt,

this is now startig to be a pain, maybe I should look at mod_rewrtie again :)

Your suggestion for page?var=whatever etc would work, but I need it to be seo friendly and do not want to use ?var=etc

_________________________

I have just had a look at mod_rewrite again but this is what I have

RewriteEngine on
RewriteRule ^/([^/\.]+).html$ index.php [L]

All I want to do is redirect all links to index.php or .html

If I do this do my links still have to contain ?var=etc

What I mean by this is <a href="'.$page_name.'>'.$page_name.'</a>
I need to look like <a href="home.html>Home</a>

Not<a href="index.html?page=home>Home</a>

thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Get rid of the assignment in the str_replace call.

Edit | now that I think about it, you could do:

Code: Select all

<?php
$getNavigation = mysql_query("SELECT
        domain_name,
        page_name FROM website_pages WHERE domain_name = '$domain_name'")or die(mysql_error());
       
    while ($row = mysql_fetch_array($getNavigation)) {
        $page_link = $row['page_name'];       
        $page_name = str_replace('.php', '', $page_link);
       
        if ($page_name == 'index') {
            $page_name = 'Home';
        }
       
        echo '<p><a href="'.strtolower($page_link).'">'.$page_name.'</a></p>';
}
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I would prefer using basename() over str_replace in this case
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Probably not a bad idea Jcart.
nyfael
Forum Commoner
Posts: 32
Joined: Thu Sep 21, 2006 2:28 am

Post by nyfael »

That will make any page not found be directed to the one you specified. I use it on pretty much every website I make.

.htaccess

Code: Select all

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /error404.php
This PHP code will tell you what page you're on:

Code: Select all

$_SERVER["REQUEST_URI"]
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

ianhull, could you explain (without using code) what it is you are trying to achieve and how 404 fits into this.
Post Reply