What page sent 'em to 404.php?

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

User avatar
vinyl-junkie
Forum Newbie
Posts: 23
Joined: Sat Nov 22, 2003 2:52 pm
Location: Seattle, WA area (USA)

What page sent 'em to 404.php?

Post by vinyl-junkie »

I've searched LOTS of places, including here, and haven't found what I'm looking for. The saga continues of my conversion from ASP to PHP, with this question. On my custom 404 page in ASP, I have something like "the page you were trying to access - http://www.somepage.com - was not found or is no longer here." and so forth. In PHP, how do I do the same thing - capture what sent them to my 404 page? HTTP_REFERER doesn't work for that purpose, so what does?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

REQUEST_URI or PHP_SELF
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

you can use either of those in the $_SERVER global.

Example:

Code: Select all

$_SERVERї'PHP_SELF']
$_SERVERї'REQUEST_URI']
User avatar
vinyl-junkie
Forum Newbie
Posts: 23
Joined: Sat Nov 22, 2003 2:52 pm
Location: Seattle, WA area (USA)

Post by vinyl-junkie »

Thanks for the suggestions, but none of them worked. All those server variables return the name of the 404 page. :(
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

how did you register you custom 404 page?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Create a file with the following content:

ErrorDocument 404 /404.php

Name it as .htaccess
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I didn't want to know how it is possible but how it has been done.
But thx ;)
User avatar
vinyl-junkie
Forum Newbie
Posts: 23
Joined: Sat Nov 22, 2003 2:52 pm
Location: Seattle, WA area (USA)

Post by vinyl-junkie »

vigge89 wrote:Create a file with the following content:

ErrorDocument 404 /404.php

Name it as .htaccess
At the risk of being picky, doing a .htaccess file only works on a Unix system. If you have a website that's on a Windows server, you can still have a custom 404 error page, but your web host will probably have to set that up for you.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

no, apache/win32 can do just the same.
But how did you do it?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

sorry if i am hijacking this post but i also tried useing

Code: Select all

ErrorDocument 404 /404.php
but it didt work at all, i am runing XP with Apache/1.3.27 (Win32) PHP/4.3.3RC1 :?. so maybe the above code doest work? 8O
User avatar
vinyl-junkie
Forum Newbie
Posts: 23
Joined: Sat Nov 22, 2003 2:52 pm
Location: Seattle, WA area (USA)

Post by vinyl-junkie »

qads wrote:sorry if i am hijacking this post but i also tried useing

Code: Select all

ErrorDocument 404 /404.php
but it didt work at all, i am runing XP with Apache/1.3.27 (Win32) PHP/4.3.3RC1 :?. so maybe the above code doest work? 8O
No problem with the hijacking. This is an important topic after all. :D

The above code should work, provided you have your 404 page named 404.php, and it is in your root directory.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

nope, just tried it agian, no go :?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

and again...
vinyl-junkie: How and where did you set up your 404.php page?
with ErrorDocument in your httpd.conf?
is php installed as module or as cgi?

Both, $_SERVER['REDIRECT_URL'] and $_SERVER['REQUEST_URI'], should give you the actually requested url.
User avatar
vinyl-junkie
Forum Newbie
Posts: 23
Joined: Sat Nov 22, 2003 2:52 pm
Location: Seattle, WA area (USA)

Post by vinyl-junkie »

volka wrote:and again...
vinyl-junkie: How and where did you set up your 404.php page?
404.php is a web page, just like any other on your website. Just set it up with whatever message you want and visitors will get automatically redirected to it if you have a page that moved, they hand-type an incorrect URL, or whatever. Go here to see how mine looks on my Windows hosted site:
http://www.napathon.net/xx.asp
Notice the part that shows you how you ended up there. Somehow, there's gotta be a way of doing the same thing in PHP.
Both, $_SERVER['REDIRECT_URL'] and $_SERVER['REQUEST_URI'], should give you the actually requested url.
Still no joy. One of these didn't return any value at all. The other still returned /404.php as the URL. *sigh*
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Oops. Sorry for misreading your inquery. ;)

Check out: http://httpd.apache.org/docs-2.0/custom-error.html

It might have some details that can help you.
Apache will define REDIRECT_URL and REDIRECT_STATUS to help the script trace its origin. Both the original URL and the URL being redirected to can be logged in the access log.
Post Reply