MetaDark Posted July 23, 2009 Share Posted July 23, 2009 Is there a way to display the default 404 error of a browser? I have php file that requires a password to run and when the password is incorrect I want it to display a 404 error so it seems like the page doesn't exist. Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/ Share on other sites More sharing options...
stickynote427 Posted July 24, 2009 Share Posted July 24, 2009 Correct me if I'm wrong, but I don't think browsers have default 404 error pages. Servers might, but browsers...no, I don't think so. You should be able to create your own 404 pages if you would like, or display different ones depending on browser type. Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881571 Share on other sites More sharing options...
MetaDark Posted July 24, 2009 Author Share Posted July 24, 2009 Well whenever I get a 404 error, it displays the same thing every time unless they have a custom 404 page on the site. Try typing in something like "936534bv78049bq94.b9v3w5y" in your address bar and then "njmkrzy3a689w3.9n4860wv34" It displays the same 404 error message on both pages. But on different browser they are different. Edit: Oh lol, oops I guess there is no one for browsers, well how would you get the default 404 error of the server? Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881574 Share on other sites More sharing options...
stickynote427 Posted July 24, 2009 Share Posted July 24, 2009 When you say, "I get a 404 error", are you talking about on your site or just Web sites in general? Are you sure these are 404 sites and not "server not found" sort of pages? For example, if you go to a page on a working Web site that does not exist (such as http://www.yahoo.com/asdf/asdf/asdf), you should get that site's 404 error page. However, if you visit a site on a server that does not exist (such as www.thissiteissocoolandthenameisverylongthatmakesitacoolsite.com), you will probably get some sort of "server not found" error, which varies in appearance from browser to browser (it also appears that DNS settings can affect how this appears as well. I have OpenDNS set up on my network, so instead of my browsers' error pages, I get pages that are from OpenDNS). EDIT: I apologize. I wrote this reply before you edited your post, so I did not get to see it. Whenever you try to visit a page on a Web site that does not exist, you should automatically get a 404 error from the site. If the Web site does not have a custom one specified, you usually get a plain "404 Not Found, /whateveryoutriedtorequest was not found on this server"-type of page. Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881576 Share on other sites More sharing options...
MetaDark Posted July 24, 2009 Author Share Posted July 24, 2009 Well now I am just talking about my website. Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881577 Share on other sites More sharing options...
Philip Posted July 24, 2009 Share Posted July 24, 2009 Make sure you aren't echo'ing anything before this, but the following will force a browser to think it is a non-existent page (404 error) header("HTTP/1.0 404 Not Found"); exit; a side note: this will force a browser specific 404 (each browser's 404 error page will look different) - if you want to provide a custom 404 look (a default 404 for your whole site) you need to include/print the page before the exit call Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881578 Share on other sites More sharing options...
kristofferlc Posted July 24, 2009 Share Posted July 24, 2009 Browsers do have default 404 messages... Andi have an idea, if the password isnt set, redirect to a non-exsisting page, like this. header("Location: this/file/doesnt/exsist.php"); Or do you require the URL to stay the same?.. Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881580 Share on other sites More sharing options...
MetaDark Posted July 24, 2009 Author Share Posted July 24, 2009 I have tried using header("HTTP/1.0 404 Not Found") before I posted here. I will show my code. <?php $password = $_POST['password']; if ($password != "TEST") { header("HTTP/1.0 404 Not Found"); } else { $name = $_POST["name"]; echo "&name=" . $name; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881585 Share on other sites More sharing options...
Philip Posted July 24, 2009 Share Posted July 24, 2009 Umm, because if I enter the password TEST it will show a 404. If I enter anything but TEST it echos the name. Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881588 Share on other sites More sharing options...
MetaDark Posted July 24, 2009 Author Share Posted July 24, 2009 That's not the actual code I just changed it up and took out the ! and replaced it with a = by accident. Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881590 Share on other sites More sharing options...
Philip Posted July 24, 2009 Share Posted July 24, 2009 <?php if (!isset($_POST['password']) || $_POST['password'] != "TEST") { header("HTTP/1.0 404 Not Found"); exit; } else { $name = $_POST["name"]; echo "&name=" . $name; } ?> Works just fine for me. What exactly is it doing on your end? Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881593 Share on other sites More sharing options...
MetaDark Posted July 24, 2009 Author Share Posted July 24, 2009 In Firefox nothing happens and it shows the browser's default 404 error screen not the website's. Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881603 Share on other sites More sharing options...
Philip Posted July 24, 2009 Share Posted July 24, 2009 a side note: this will force a browser specific 404 (each browser's 404 error page will look different) - if you want to provide a custom 404 look (a default 404 for your whole site) you need to include/print the page before the exit call Firefox will show the response headers a 404, but you need to show a 404 page otherwise it will be blank. I should have mentioned that earlier, sorry. As for the other part of the question, read the above quote from earlier. Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881605 Share on other sites More sharing options...
MetaDark Posted July 24, 2009 Author Share Posted July 24, 2009 Well I will just redirect it to a non-existing page for now because it displays my websites 404 error message. Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881609 Share on other sites More sharing options...
Philip Posted July 24, 2009 Share Posted July 24, 2009 Well I will just redirect it to a non-existing page for now because it displays my websites 404 error message. By doing that, you're showing the user you didn't want them to see that something 'valuable' is at the location they just tried to visit, but hey... it's your site Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881612 Share on other sites More sharing options...
kristofferlc Posted July 24, 2009 Share Posted July 24, 2009 Well I will just redirect it to a non-existing page for now because it displays my websites 404 error message. If you still want to "hide" your files that you dont want people to see, make your 404 document redirect to a 404 file, like your .htaccess file directs to 404.php wich redirects to 404.html... Then on the password protected pages, redirect to the 404.html file as well? (or 404.php, if you have any intrest of that.) Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881626 Share on other sites More sharing options...
MetaDark Posted July 24, 2009 Author Share Posted July 24, 2009 Well first off it would take them a while to even find the file and second it is better than saying "Sorry you are not allowed to access this page." Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881640 Share on other sites More sharing options...
MetaDark Posted July 24, 2009 Author Share Posted July 24, 2009 I decided to redirect it to a 404 page I just made. This is for the people that want to do that same thing as I wanted to and don't know how to redirect it to a custom page. First go to your website and enter a page that doesn't exist, such as www.yourwebsite.com/thispagedoesnotexist A 404 error will pop up, then open a new tab or window and go to www.thisisnotawebsite.nothing If the same error pops up then you will have to make your own 404 error page, if it is different right click the page of "www.yourwebsite.com/thispagedoesnotexist" and look for something like view source, view info, source... click on it and then copy the code that appears, then create page on your website called 404Error.html and paste in the code that you copied. That is just to make it look more like the other error pages your server displays. Now open the .htaccess file in your website and put in ErrorDocument 404 http://www.yourwebsite.com/404Error.html at the end of the file. If you have something like Dreamweaver to create your website you may be wondering "Where is the .htaccess file?". Well it is on your website so go into remote view and you will find it, then download it. If you can't find it at all just create a new file called .htaccess and put the code I gave you above into it. If something bad happens to your website if you create a new .htaccess file it is not my fault, it means it was on your website the whole time and you couldn't find it. Once you got that all set up use the code header("Location: /404Error.html"); exit; Quote Link to comment https://forums.phpfreaks.com/topic/167201-solved-display-a-404-error/#findComment-881998 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.