/    Sign up×
Community /Pin to ProfileBookmark

Returning a Status Code

Okay, I have a bunch of files in an /include directory, like my header and footer and db connection script and stuff.

I’ve protected them (sort of) by checking at the top if a constant is defined, and if it isn’t, redirecting to an error page.

But wouldn’t it be more elegant to send back a 403, and then use .htaccess to display a custom ‘Forbidden’ page? This should be possible, based on the PHP help for Header, but I can’t seem to get it working.

Thanks for any help.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@sciguyryanAug 30.2004 — Well on most servers you just create a file with a .shtml extension named with the error number, it would be easier to just do that probably.


RyanJ
Copy linkTweet thisAlerts:
@mikepurvisauthorAug 30.2004 — [i]Originally posted by sciguyryan [/i]

[B]Well on most servers you just create a file with a .shtml extension named with the error number, it would be easier to just do that probably.[/B][/QUOTE]


The htaccess error codes are working fine. If I try to access a non-existent document, up comes 404.

The issue is that I don't know how to terminate the current script by sending back a 403 or 401 to the browser, and then having it grab the custom error page.
Copy linkTweet thisAlerts:
@sciguyryanAug 30.2004 — Ah, sorry I miss-understood your question ?

You just do something like:

[code=php]
<?php
header("Status: 404 Not Found");
?>
[/code]


More Info here: http://uk2.php.net/header


Hope that helps,

RyanJ
Copy linkTweet thisAlerts:
@mikepurvisauthorAug 30.2004 — [i]Originally posted by sciguyryan [/i]

[B]Ah, sorry I miss-understood your question ?



You just do something like:



[code=php]
<?php
header("Status: 404 Not Found");
?>
[/code]


More Info here: http://uk2.php.net/header[/B][/QUOTE]


That's what I thought too, and I think it's sending that code, but it's not actually fetching the corresponding error page. It sends the current file -- with the status code in the header.
Copy linkTweet thisAlerts:
@sciguyryanAug 30.2004 — As far as I can find thats the only thing I can find to do it.


RyanJ
Copy linkTweet thisAlerts:
@MstrBobAug 30.2004 — Why not do this:
[code=php]
if($_SERVER['REQUEST_URI']==$_SERVER['PHP_SELF'])
{ header("Location: 403.shtml"); } else {
// Perform Script
}
[/code]
Copy linkTweet thisAlerts:
@mikepurvisauthorAug 30.2004 — [i]Originally posted by MstrBob [/i]

[B]Why not do this:

[code=php]
if($_SERVER['REQUEST_URI']==$_SERVER['PHP_SELF'])
{ header("Location: 403.shtml"); } else {
// Perform Script
}
[/code]
[/B][/QUOTE]


That's what I had already, I guess I'll stick to that. I thought there might be a more elegant solution.

Thanks for the posts everyone.
×

Success!

Help @mikepurvis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...