The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. March 19, 2024, 04:02:32 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: How to make the header generate a 404 not found...via php file.  (Read 9511 times)
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« on: August 12, 2012, 07:08:21 PM »

I have the below php in my php file and if $cnt==1000 then it correctly sends out the header 301 Moved Permanently... however if the else part is activated when $cnt!=1000occurs, then it sends out a 302 file found... instead of the 404 as the code suggests it will. Have I programmed it wrong and is this an incorrect way to send out the 404 in the header.  It simply shows the index page of the blog and sends out a header 302....
The top part works fine, it sends out a 301 as it should.

if ($cnt==1000) {
header( "HTTP/1.1 301 Moved Permanently" );
header("Location: http://mydomain.com/idx/$url");
} else {
header( "HTTP/1.1 404 Not Found" );
header("Location: http://mydomain.com/index.php?404.php");
}

What I want it to do is when the file is not found and the $cnt then would not be 1000 send out the header 404 Not found but I am getting a header of 302 instead..

Am I doing something wrong?

Thanks
Logged
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #1 on: August 12, 2012, 09:41:24 PM »

Afaik, found or not found does not depend on your variable ($cnt==1000),
it depends on if the file is found or not.  And although we can set headers
with our code, Apache has the last word and may alter them.





Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« Reply #2 on: August 13, 2012, 06:50:21 AM »

Thanks Bompa,
But of course you could not know without seeing previous code that $cnt does determine found or not found as a loop prior to the listed code went thru a seach for the particular URL and if not found in the data base then it would not be on the server either.. so at that point the fail to find in the data base search if found sets $cnt equal to 1000 else not found in the data base sets $cnt to 0. so the $cnt <> 1000 should light of the lower part of the if equation and send the header a 404 simulating a not found on the server the same as it does when a 310 Permanenlty Moved is sent.  Instead its sending a 302 Temporarily Moved when I tell it to send a 404... so I am assuming I am not using the correct command to send a 404 to the header.
I will try posting on the an htaccess forum to see if they have any ideas...  I just figured you experts would know everything.. as you have for most of my questions in the past... but I am aware its probably a better place to post is in htaccess than php... but you did not have a htaccess protal... but I had to try anyway... Thanks..

Basically all I was asking is what is the php command to send a 404 not found to the header same as the code I show does send a 301 Permanently Moved to the header.


Logged
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #3 on: August 13, 2012, 08:40:44 AM »

header( "HTTP/1.1 404 Not Found" );
header("Location: http://mydomain.com/index.php?404.php");

What I meant was index.php?404.php was found, but i dunno really
Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #4 on: August 15, 2012, 12:02:20 AM »

I'm actually interested in this one, because Apache shouldn't be changing the header code.

The deal is that Apache will only spit out a Not Found if the page / handler is truly not found. But since it's running your script, YOU are in charge. So when you send back that header, Apache is simply forwarding your information back to the caller.

I'm curious if you're actually receiving the 404 and redirect, but the index.php?404.php is what's not found.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #5 on: August 15, 2012, 01:31:38 AM »

I'm actually interested in this one, because Apache shouldn't be changing the header code.


Quote
The deal is that Apache will only spit out a Not Found if the page / handler is truly not found.

Not found by whom?  Apache or tommyx?


Quote
But since it's running your script, YOU are in charge. So when you send back that header, Apache is simply forwarding your information back to the caller.

Actually Apache parses the header on every request. If the header is incomplete, Apache
will add a line. If the header is not valid code, it will discard it and generate it's own error
header.  The only way for us to have 100% control and bypass Apache parsing headers is
with 'Non Parsed Headers' (NPH).



Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #6 on: August 15, 2012, 01:44:55 AM »

Ok, I am not an expert on this shit, but when you run this code...

Code:
header( "HTTP/1.1 404 Not Found" );
header("Location: http://mydomain.com/index.php?404.php");
you are not doing a 404, you are doing a redirect.


A 404 is (not exact) more like this:
Code:
header( "HTTP/1.1 404 Not Found" );
echo "Sorry the page you are looking for was not found."


So, in my opinion, Apache is ignoring your 404 header line and
respecting the redirect, which it finds, so it gives the 302 status. So,
in effect, Apache is replacing your header line, imo.

but i dunno really Cheesy
« Last Edit: August 15, 2012, 01:54:16 AM by Bompa » Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« Reply #7 on: August 15, 2012, 07:20:30 AM »

<quote>
Not found by whom?  Apache or tommyx?
But since it's running your script, YOU are in charge. So when you send back that header, Apache is simply forwarding your information back to the caller.
Actually Apache parses the header on every request. If the header is incomplete, Apache
will add a line. If the header is not valid code, it will discard it and generate it's own error
header.  The only way for us to have 100% control and bypass Apache parsing headers is
with 'Non Parsed Headers' (NPH).
</quote>

Thanks Perkiset... here it the whole story and I am sticking to it...
1. First the reason for all this sh**t...... to begin with...
As we know real estate has a ton of stuff sold daily.  So when the IDX notes that a property is sold it gets rid of it... so of course when google or anyone comes along an looks for that url it is Not Found so a normal 404 header would be sent to google and all.. and then auto redirected to a 404.php page in the case of a blog...and if you have a theme would be sent to the 404.php of the theme allowing you to mod your little heart out to effect the look and feel of the 404 results....
2. So along come tommytx and wants to tweak with the entire process....
3. So I hide in the htaccess file and look for any url with the following /mls-1234567-propaddress-propzip-and other shit.htm
4. When I see the url with "/mls-" I know its a mls request for a very specific property.
5. I have a file each 24 hours that updates with all the properties that have sold in the last 90 days... by MLS number.
6. I jump out from behind my hiding place and throw the visitior. into my index-test.php where I compare the mls nr in the url to the number in the sold.txt file on my hard drive.
7. IF sold then I can process it in any way I like.... currently just sending it to my regular 404.php handler file.
8. The way it works is that if found it sets $cnt to 1000 if not it sets  $cnt to 0.  I know I should have set $sold="Yes" or $sold="No" for simplification but that is my business... $cnt=1000 means sold... my call.... Thank you....
9. Now code re-explantion since we are all now on same page..

Rem.... IF $cnt = 1000 then I want to tell Google 301 moved permanently and it does every time... and then sends them on their merry way to the url that I have listed below... that all works perfect..

Rem.. Second part.. if $cnt = 0 then the file is not on my server since I know for a fact its in the sold.txt file so no need to have google or humans even bother to look for it...  so the else is supposed to send a 404 Not Found header but it does not.. It sends a 302 I thing Temp moved... and then as Bompa says it now forwards to the local 404.php file which it does... But I do not want to send google or anyone a 302 I want a 404... sent in the header just like a normal 404 does... sends out header 404 then smartly shows the client the 404 page and does not send a freaking 302.



if ($cnt==1000) {
header( "HTTP/1.1 301 Moved Permanently" );
header("Location: http://mydomain.com/mynewlocation/mynewfile.htm");
} else {
header( "HTTP/1.1 404 Not Found" );
header("Location: http://mydomain.com/index.php?404.php");
}

When I get a chance later today... I will remove the bottom line and see if the header then gets a 404... but if that works then I will need to find a way to get the system to forward to the 404 page at the end without changing the header...  blogs will find the 404.php normally when you forward them to
http://mydomain.com/index.php?404.php  or I can send them directly to my 404.php page but that give a 302 also...

Ok.. don't ask me why I don't just use the normal 404 process... instead of all this... I have my reasons.....
And this should work... when I give it a 301 command it sends out a 301 it does not change it to something else.... so the 404 switch to 302 makes no sense unless I am not addressing the 404 header command correctly... Looks good to me...

Thanks for the input Bompa....but so far the nail has not been hit on the head yet... Hopefully this gives you more to analyze....
I do like Perkiset commet... "The freaking header code should not just switch arbitrairly to whatever the f***k it want to...


Answer to Perkiset.... Where is the not found coming from system or tommtyx.
From tommytxt... when I search my sold.txt file and cannot find the mls number I know that it will also be missing from the server... even though I do not check... so I don't even let the system look for it.. I just say 404 Not Found in my little mind and then tell php to process it as not found instead of 301.... So If the $cnt is set to 0 means not found.. if set to $cnt = 1000 the file will be found on the drive if you look.. but in either case I don't look, I jsut look in my sold.txt file to decide....

Logged
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« Reply #8 on: August 15, 2012, 07:28:52 AM »

Here is an idea I will try today that might work....if sending the one header only with 404 then do not follow with a redirect Location: command it might just work...


Code:
header( "HTTP/1.1 404 Not Found" );
echo "Sorry the page you are looking for was not found.";

Code:
header( "HTTP/1.1 404 Not Found" );
include("/filelocation/404.php");

If that sends the 404 in the header.. and I follow with an include vice a Location: then no redirect has occurred so maybe I will see the 404 in the header and I will still get forwarded to the 404.php but via include no one knows I have jumped into the 404.php which is where I wanted to go anyway.
Kinda sneaking in the back door and getting rid of the Location;  which suggests I have done a redirect....

so the header sees a 404 as the last event... while I sneak the 404.php to the viewer on the screen and the header knows nothing about my antics... so it cannot say 301 Temp moved...make sense???

Will report the results later today...
Thanks.

Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #9 on: August 15, 2012, 08:55:30 AM »

Actually Apache parses the header on every request. If the header is incomplete, Apache
will add a line. If the header is not valid code, it will discard it and generate it's own error
header.  The only way for us to have 100% control and bypass Apache parsing headers is
with 'Non Parsed Headers' (NPH).

Hadn't thought that through, Bomps, but that makes total sense. Andyour explanation (and a big  D'oh!) about him providing a redirection rather than another found is strong.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #10 on: August 15, 2012, 08:59:09 AM »

I think you're on it Tommy ... apache allows for custom not found pages as does the HTML spec. So providing content on a 404 rather than redirecting which, I believe, will follow Bompa's notion and trigger a recode in Apache, will get you where you want to go.

Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #11 on: August 15, 2012, 05:59:31 PM »


Why not just;

header( "HTTP/1.1 404 Not Found" );
echo meta refresh here?

not seamless enough?




Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #12 on: August 15, 2012, 07:56:44 PM »

It looks like he's trying to provide custom content on the 404 page.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« Reply #13 on: August 16, 2012, 08:29:14 PM »

thanks for the suggestions guys... all is well it works perfectly like I want it to now...
Even using the include to make it show my 404.php it kept failing... however when I did exit after passing the 404 to google and humans via the header, the exit sent back to the htaccess file from whence it came and as we know without other directions the htaccess will then continue to process the file incoming just as if I had not tampered with it....
So if I have it on file I send it on its way to the new location.. however if its a 404 I send the 404 then jump back into the htaccess using php exit and the sucker now tries to find that file and issues a 404.... just the normal process.. I may remove my header sent 404 and then a 404 should be sent by the htaccess when it also realizes the file cannot be found... but in any case a double 404 is not being issued or when header view is done it seems like a normal 404... but again I may remove the else part completely and I believe I will still get the 404 issued now by the normal htaccess process... but it appears to be perfect now.... Thanks again.


Code:
if ($cnt==1000) {
header( "HTTP/1.1 301 Moved Permanently" );
header("Location: http://mydomain.com/mynewlocation/mynewfile.htm");
} else {
header( "HTTP/1.1 404 Not Found" );
exit;
}
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!